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

difftreelog

Add std.manifestJsonMinified()

Kohei Suzuki2021-12-31parent: #99a6e6f.patch.diff
in: master
It was added to jsonnet v0.18.0.

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
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -121,7 +121,7 @@
 			("trace".into(), builtin_trace),
 			("join".into(), builtin_join),
 			("escapeStringJson".into(), builtin_escape_string_json),
-			("manifestJsonEx".into(), builtin_manifest_json_ex),
+			("manifestJsonExImpl".into(), builtin_manifest_json_ex_impl),
 			("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc),
 			("reverse".into(), builtin_reverse),
 			("id".into(), builtin_id),
@@ -754,18 +754,22 @@
 	})
 }
 
-fn builtin_manifest_json_ex(
+fn builtin_manifest_json_ex_impl(
 	context: Context,
 	_loc: Option<&ExprLocation>,
 	args: &ArgsDesc,
 ) -> Result<Val> {
-	parse_args!(context, "manifestJsonEx", args, 2, [
+	parse_args!(context, "manifestJsonEx", args, 4, [
 		0, value: ty!(any);
 		1, indent: ty!(string) => Val::Str;
+		2, newline: ty!(string) => Val::Str;
+		3, key_val_sep: ty!(string) => Val::Str;
 	], {
 		Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {
 			padding: &indent,
 			mtype: ManifestType::Std,
+			newline: &newline,
+			key_val_sep: &key_val_sep,
 		})?.into()))
 	})
 }
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -810,6 +810,14 @@
 	}
 
 	#[test]
+	fn json_minified() {
+		assert_json!(
+			r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,
+			r#""{\"a\":3,\"b\":4,\"c\":6}""#
+		);
+	}
+
+	#[test]
 	fn parse_json() {
 		assert_json!(
 			r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -451,6 +451,8 @@
 				&ManifestJsonOptions {
 					padding: "",
 					mtype: ManifestType::ToString,
+					newline: "\n",
+					key_val_sep: ": ",
 				},
 			)?
 			.into(),
@@ -535,6 +537,8 @@
 				} else {
 					ManifestType::Manifest
 				},
+				newline: "\n",
+				key_val_sep: ": ",
 			},
 		)
 		.map(|s| s.into())
@@ -547,6 +551,8 @@
 			&ManifestJsonOptions {
 				padding: &" ".repeat(padding),
 				mtype: ManifestType::Std,
+				newline: "\n",
+				key_val_sep: ": ",
 			},
 		)
 		.map(|s| s.into())
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -372,7 +372,11 @@
 
   manifestJson(value):: std.manifestJsonEx(value, '    '),
 
-  manifestJsonEx:: $intrinsic(manifestJsonEx),
+  manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),
+
+  manifestJsonExImpl:: $intrinsic(manifestJsonExImpl),
+
+  manifestJsonEx(value, indent, newline='\n', key_val_sep=': '):: std.manifestJsonExImpl(value, indent, newline, key_val_sep),
 
   manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),