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.rsdiffbeforeafterboth451 &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::Manifest537 },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())crates/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),