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

difftreelog

Merge pull request #74 from eagletmt/std-json-minified

Yaroslav Bolyukin2021-12-31parents: #99a6e6f #a02e7e9.patch.diff
in: master
Add std.manifestJsonMinified()

5 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth
18pub struct ManifestJsonOptions<'s> {18pub struct ManifestJsonOptions<'s> {
19 pub padding: &'s str,19 pub padding: &'s str,
20 pub mtype: ManifestType,20 pub mtype: ManifestType,
21 pub newline: &'s str,
22 pub key_val_sep: &'s str,
21}23}
2224
23pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {25pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {
48 buf.push('[');50 buf.push('[');
49 if !items.is_empty() {51 if !items.is_empty() {
50 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {52 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
51 buf.push('\n');53 buf.push_str(options.newline);
52 }54 }
5355
54 let old_len = cur_padding.len();56 let old_len = cur_padding.len();
59 if mtype == ManifestType::ToString {61 if mtype == ManifestType::ToString {
60 buf.push(' ');62 buf.push(' ');
61 } else if mtype != ManifestType::Minify {63 } else if mtype != ManifestType::Minify {
62 buf.push('\n');64 buf.push_str(options.newline);
63 }65 }
64 }66 }
65 buf.push_str(cur_padding);67 buf.push_str(cur_padding);
68 cur_padding.truncate(old_len);70 cur_padding.truncate(old_len);
6971
70 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {72 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
71 buf.push('\n');73 buf.push_str(options.newline);
72 buf.push_str(cur_padding);74 buf.push_str(cur_padding);
73 }75 }
74 } else if mtype == ManifestType::Std {76 } else if mtype == ManifestType::Std {
85 let fields = obj.fields();87 let fields = obj.fields();
86 if !fields.is_empty() {88 if !fields.is_empty() {
87 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {89 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
88 buf.push('\n');90 buf.push_str(options.newline);
89 }91 }
9092
91 let old_len = cur_padding.len();93 let old_len = cur_padding.len();
96 if mtype == ManifestType::ToString {98 if mtype == ManifestType::ToString {
97 buf.push(' ');99 buf.push(' ');
98 } else if mtype != ManifestType::Minify {100 } else if mtype != ManifestType::Minify {
99 buf.push('\n');101 buf.push_str(options.newline);
100 }102 }
101 }103 }
102 buf.push_str(cur_padding);104 buf.push_str(cur_padding);
103 escape_string_json_buf(&field, buf);105 escape_string_json_buf(&field, buf);
104 buf.push_str(": ");106 buf.push_str(options.key_val_sep);
105 crate::push(107 crate::push(
106 None,108 None,
107 || format!("field <{}> manifestification", field.clone()),109 || format!("field <{}> manifestification", field.clone()),
114 cur_padding.truncate(old_len);116 cur_padding.truncate(old_len);
115117
116 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {118 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
117 buf.push('\n');119 buf.push_str(options.newline);
118 buf.push_str(cur_padding);120 buf.push_str(cur_padding);
119 }121 }
120 } else if mtype == ManifestType::Std {122 } else if mtype == ManifestType::Std {
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
121 ("trace".into(), builtin_trace),121 ("trace".into(), builtin_trace),
122 ("join".into(), builtin_join),122 ("join".into(), builtin_join),
123 ("escapeStringJson".into(), builtin_escape_string_json),123 ("escapeStringJson".into(), builtin_escape_string_json),
124 ("manifestJsonEx".into(), builtin_manifest_json_ex),124 ("manifestJsonExImpl".into(), builtin_manifest_json_ex_impl),
125 ("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc),125 ("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc),
126 ("reverse".into(), builtin_reverse),126 ("reverse".into(), builtin_reverse),
127 ("id".into(), builtin_id),127 ("id".into(), builtin_id),
754 })754 })
755}755}
756756
757fn builtin_manifest_json_ex(757fn builtin_manifest_json_ex_impl(
758 context: Context,758 context: Context,
759 _loc: Option<&ExprLocation>,759 _loc: Option<&ExprLocation>,
760 args: &ArgsDesc,760 args: &ArgsDesc,
761) -> Result<Val> {761) -> Result<Val> {
762 parse_args!(context, "manifestJsonEx", args, 2, [762 parse_args!(context, "manifestJsonEx", args, 4, [
763 0, value: ty!(any);763 0, value: ty!(any);
764 1, indent: ty!(string) => Val::Str;764 1, indent: ty!(string) => Val::Str;
765 2, newline: ty!(string) => Val::Str;
766 3, key_val_sep: ty!(string) => Val::Str;
765 ], {767 ], {
766 Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {768 Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {
767 padding: &indent,769 padding: &indent,
768 mtype: ManifestType::Std,770 mtype: ManifestType::Std,
771 newline: &newline,
772 key_val_sep: &key_val_sep,
769 })?.into()))773 })?.into()))
770 })774 })
771}775}
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
809 );809 );
810 }810 }
811
812 #[test]
813 fn json_minified() {
814 assert_json!(
815 r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,
816 r#""{\"a\":3,\"b\":4,\"c\":6}""#
817 );
818 }
811819
812 #[test]820 #[test]
813 fn parse_json() {821 fn parse_json() {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
451 &ManifestJsonOptions {451 &ManifestJsonOptions {
452 padding: "",452 padding: "",
453 mtype: ManifestType::ToString,453 mtype: ManifestType::ToString,
454 newline: "\n",
455 key_val_sep: ": ",
454 },456 },
455 )?457 )?
456 .into(),458 .into(),
535 } else {537 } else {
536 ManifestType::Manifest538 ManifestType::Manifest
537 },539 },
540 newline: "\n",
541 key_val_sep: ": ",
538 },542 },
539 )543 )
540 .map(|s| s.into())544 .map(|s| s.into())
547 &ManifestJsonOptions {551 &ManifestJsonOptions {
548 padding: &" ".repeat(padding),552 padding: &" ".repeat(padding),
549 mtype: ManifestType::Std,553 mtype: ManifestType::Std,
554 newline: "\n",
555 key_val_sep: ": ",
550 },556 },
551 )557 )
552 .map(|s| s.into())558 .map(|s| s.into())
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
372372
373 manifestJson(value):: std.manifestJsonEx(value, ' '),373 manifestJson(value):: std.manifestJsonEx(value, ' '),
374374
375 manifestJsonEx:: $intrinsic(manifestJsonEx),375 manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),
376376
377 manifestJsonExImpl:: $intrinsic(manifestJsonExImpl),
378
379 manifestJsonEx(value, indent, newline='\n', key_val_sep=': '):: std.manifestJsonExImpl(value, indent, newline, key_val_sep),
380
377 manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),381 manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),
378382
379 manifestYamlDoc(value, indent_array_in_object=false):: std.manifestYamlDocImpl(value, indent_array_in_object),383 manifestYamlDoc(value, indent_array_in_object=false):: std.manifestYamlDocImpl(value, indent_array_in_object),