git.delta.rocks / jrsonnet / refs/commits / e1f3eca2b5f5

difftreelog

feat(evaluator) adds std.parseJson builtin method

Christian Simon2021-05-18parent: #0be1df1.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
1use crate::{1use crate::{
2 equals,2 equals,
3 error::{Error::*, Result},3 error::{Error::*, Result},
4 parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, FuncVal, LazyVal,4 parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,
5 Val,5 FuncVal, LazyVal, Val,
6};6};
7use format::{format_arr, format_obj};7use format::{format_arr, format_obj};
74 ("reverse".into(), builtin_reverse),74 ("reverse".into(), builtin_reverse),
75 ("id".into(), builtin_id),75 ("id".into(), builtin_id),
76 ("strReplace".into(), builtin_str_replace),76 ("strReplace".into(), builtin_str_replace),
77 ("parseJson".into(), builtin_parse_json),
77 ].iter().cloned().collect()78 ].iter().cloned().collect()
78 };79 };
79}80}
164 })165 })
165}166}
167
168fn builtin_parse_json(
169 context: Context,
170 _loc: Option<&ExprLocation>,
171 args: &ArgsDesc,
172) -> Result<Val> {
173 parse_args!(context, "parseJson", args, 1, [
174 0, s: ty!(string) => Val::Str;
175 ], {
176 let state = EvaluationState::default();
177 let path = Rc::new(PathBuf::from("std.parseJson"));
178 state.evaluate_snippet_raw(path ,s)
179 })
180}
166181
167// faster182// faster
168fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {183fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
801 );801 );
802 }802 }
803
804 #[test]
805 fn parse_json() {
806 assert_json!(
807 r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,
808 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#
809 );
810 // TODO: this should in fact fail as is no proper JSON syntax
811 assert_json!(
812 r#"std.parseJson("{a:-1, b:1, c:3.141, d:[]}")"#,
813 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#
814 );
815 // TODO: this is also no valid JSON
816 assert_json!(r#"std.parseJson('local x = 2; x * x')"#, r#"4"#);
817 }
803818
804 #[test]819 #[test]
805 fn test() {820 fn test() {