difftreelog
Merge pull request #74 from eagletmt/std-json-minified
in: master
Add std.manifestJsonMinified()
5 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -18,6 +18,8 @@
pub struct ManifestJsonOptions<'s> {
pub padding: &'s str,
pub mtype: ManifestType,
+ pub newline: &'s str,
+ pub key_val_sep: &'s str,
}
pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {
@@ -48,7 +50,7 @@
buf.push('[');
if !items.is_empty() {
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
let old_len = cur_padding.len();
@@ -59,7 +61,7 @@
if mtype == ManifestType::ToString {
buf.push(' ');
} else if mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
}
buf.push_str(cur_padding);
@@ -68,7 +70,7 @@
cur_padding.truncate(old_len);
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
buf.push_str(cur_padding);
}
} else if mtype == ManifestType::Std {
@@ -85,7 +87,7 @@
let fields = obj.fields();
if !fields.is_empty() {
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
let old_len = cur_padding.len();
@@ -96,12 +98,12 @@
if mtype == ManifestType::ToString {
buf.push(' ');
} else if mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
}
buf.push_str(cur_padding);
escape_string_json_buf(&field, buf);
- buf.push_str(": ");
+ buf.push_str(options.key_val_sep);
crate::push(
None,
|| format!("field <{}> manifestification", field.clone()),
@@ -114,7 +116,7 @@
cur_padding.truncate(old_len);
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
buf.push_str(cur_padding);
}
} else if mtype == ManifestType::Std {
crates/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()))
})
}
crates/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": []}')"#,
crates/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())
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth372372373 manifestJson(value):: std.manifestJsonEx(value, ' '),373 manifestJson(value):: std.manifestJsonEx(value, ' '),374374375 manifestJsonEx:: $intrinsic(manifestJsonEx),375 manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),376376377 manifestJsonExImpl:: $intrinsic(manifestJsonExImpl),378379 manifestJsonEx(value, indent, newline='\n', key_val_sep=': '):: std.manifestJsonExImpl(value, indent, newline, key_val_sep),380377 manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),381 manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),378382379 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),