difftreelog
Merge remote-tracking branch 'origin/master' into gcmodule
in: master
7 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth19pub struct ManifestJsonOptions<'s> {19pub struct ManifestJsonOptions<'s> {20 pub padding: &'s str,20 pub padding: &'s str,21 pub mtype: ManifestType,21 pub mtype: ManifestType,22 pub newline: &'s str,23 pub key_val_sep: &'s str,22}24}232524pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {26pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {49 buf.push('[');51 buf.push('[');50 if !items.is_empty() {52 if !items.is_empty() {51 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {53 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {52 buf.push('\n');54 buf.push_str(options.newline);53 }55 }545655 let old_len = cur_padding.len();57 let old_len = cur_padding.len();60 if mtype == ManifestType::ToString {62 if mtype == ManifestType::ToString {61 buf.push(' ');63 buf.push(' ');62 } else if mtype != ManifestType::Minify {64 } else if mtype != ManifestType::Minify {63 buf.push('\n');65 buf.push_str(options.newline);64 }66 }65 }67 }66 buf.push_str(cur_padding);68 buf.push_str(cur_padding);69 cur_padding.truncate(old_len);71 cur_padding.truncate(old_len);707271 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {73 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {72 buf.push('\n');74 buf.push_str(options.newline);73 buf.push_str(cur_padding);75 buf.push_str(cur_padding);74 }76 }75 } else if mtype == ManifestType::Std {77 } else if mtype == ManifestType::Std {86 let fields = obj.fields();88 let fields = obj.fields();87 if !fields.is_empty() {89 if !fields.is_empty() {88 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {90 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {89 buf.push('\n');91 buf.push_str(options.newline);90 }92 }919392 let old_len = cur_padding.len();94 let old_len = cur_padding.len();97 if mtype == ManifestType::ToString {99 if mtype == ManifestType::ToString {98 buf.push(' ');100 buf.push(' ');99 } else if mtype != ManifestType::Minify {101 } else if mtype != ManifestType::Minify {100 buf.push('\n');102 buf.push_str(options.newline);101 }103 }102 }104 }103 buf.push_str(cur_padding);105 buf.push_str(cur_padding);104 escape_string_json_buf(&field, buf);106 escape_string_json_buf(&field, buf);105 buf.push_str(": ");107 buf.push_str(options.key_val_sep);106 push_description_frame(108 push_description_frame(107 || format!("field <{}> manifestification", field.clone()),109 || format!("field <{}> manifestification", field.clone()),108 || {110 || {115 cur_padding.truncate(old_len);117 cur_padding.truncate(old_len);116118117 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {119 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {118 buf.push('\n');120 buf.push_str(options.newline);119 buf.push_str(cur_padding);121 buf.push_str(cur_padding);120 }122 }121 } else if mtype == ManifestType::Std {123 } 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
@@ -1,6 +1,5 @@
use crate::function::StaticBuiltin;
-use crate::typed::{Any, Null, PositiveF64, VecVal, M1};
-use crate::{self as jrsonnet_evaluator, Either, ObjValue};
+use crate::typed::{Any, PositiveF64, VecVal, M1};
use crate::{
builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
equals,
@@ -10,6 +9,7 @@
typed::{Either2, Either4},
with_state, ArrValue, Context, FuncVal, IndexableVal, Val,
};
+use crate::{Either, ObjValue};
use format::{format_arr, format_obj};
use gcmodule::Cc;
use jrsonnet_interner::IStr;
@@ -145,7 +145,7 @@
fn builtin_length(x: Either![IStr, VecVal, ObjValue, Cc<FuncVal>]) -> Result<usize> {
use Either4::*;
Ok(match x {
- A(x) => x.len(),
+ A(x) => x.chars().count(),
B(x) => x.0.len(),
C(x) => x
.fields_visibility()
@@ -566,12 +566,21 @@
}
#[jrsonnet_macros::builtin]
-fn builtin_manifest_json_ex(value: Any, indent: IStr) -> Result<String> {
+fn builtin_manifest_json_ex(
+ value: Any,
+ indent: IStr,
+ newline: Option<IStr>,
+ key_val_sep: Option<IStr>,
+) -> Result<String> {
+ let newline = newline.as_deref().unwrap_or("\n");
+ let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");
manifest_json_ex(
&value.0,
&ManifestJsonOptions {
padding: &indent,
mtype: ManifestType::Std,
+ newline,
+ key_val_sep,
},
)
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -5,6 +5,9 @@
clippy::ptr_arg
)]
+// For jrsonnet-macros
+extern crate self as jrsonnet_evaluator;
+
mod builtin;
mod ctx;
mod dynamic;
@@ -976,6 +979,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/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -19,7 +19,7 @@
($($ty:ty)*) => {$(
impl Typed for $ty {
const TYPE: &'static ComplexValType =
- &ComplexValType::BoundedNumber(Some(<$ty>::MIN as f64), Some(<$ty>::MAX as f64));
+ &ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));
}
impl TryFrom<Val> for $ty {
type Error = LocError;
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -400,6 +400,8 @@
&ManifestJsonOptions {
padding: "",
mtype: ManifestType::ToString,
+ newline: "\n",
+ key_val_sep: ": ",
},
)?
.into(),
@@ -484,6 +486,8 @@
} else {
ManifestType::Manifest
},
+ newline: "\n",
+ key_val_sep: ": ",
},
)
.map(|s| s.into())
@@ -496,6 +500,8 @@
&ManifestJsonOptions {
padding: &" ".repeat(padding),
mtype: ManifestType::Std,
+ newline: "\n",
+ key_val_sep: ": ",
},
)
.map(|s| s.into())
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -105,7 +105,7 @@
if let Some(opt_ty) = extract_type_from_option(&t.ty) {
quote! {{
if let Some(value) = parsed.get(#ident) {
- Some(jrsonnet_evaluator::push_description_frame(
+ Some(::jrsonnet_evaluator::push_description_frame(
|| format!("argument <{}> evaluation", #ident),
|| <#opt_ty>::try_from(value.evaluate()?),
)?)
@@ -117,7 +117,7 @@
quote! {{
let value = parsed.get(#ident).unwrap();
- jrsonnet_evaluator::push_description_frame(
+ ::jrsonnet_evaluator::push_description_frame(
|| format!("argument <{}> evaluation", #ident),
|| <#ty>::try_from(value.evaluate()?),
)?
@@ -136,7 +136,7 @@
#[derive(Clone, Copy, gcmodule::Trace)]
#vis struct #name {}
const _: () = {
- use jrsonnet_evaluator::function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike};
+ use ::jrsonnet_evaluator::function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike};
const PARAMS: &'static [BuiltinParam] = &[
#(#params),*
];
@@ -156,7 +156,7 @@
PARAMS
}
fn call(&self, context: Context, loc: Option<&ExprLocation>, args: &dyn ArgsLike) -> Result<Val> {
- let parsed = jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
+ let parsed = ::jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
let result: #result = #name(#(#args),*);
let result = result?;
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -373,6 +373,8 @@
manifestJson(value):: std.manifestJsonEx(value, ' ') tailstrict,
+ manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),
+
manifestJsonEx:: $intrinsic(manifestJsonEx),
manifestYamlDoc:: $intrinsic(manifestYamlDoc),
@@ -530,6 +532,9 @@
else
patch,
+ get(o, f, default = null, inc_hidden = true)::
+ if std.objectHasEx(o, f, inc_hidden) then o[f] else default,
+
objectFields(o)::
std.objectFieldsEx(o, false),