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

difftreelog

feat std.parseYaml intrinsic

Yaroslav Bolyukin2021-11-27parent: #7c0b097.patch.diff
in: master

6 files changed

modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -7,11 +7,9 @@
 edition = "2018"
 
 [features]
-default = ["serialized-stdlib", "explaining-traces", "serde-json"]
+default = ["serialized-stdlib", "explaining-traces"]
 # Serializes standard library AST instead of parsing them every run
-serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"]
-# Allow to convert Val into serde_json::Value and backwards
-serde-json = ["serde", "serde_json"]
+serialized-stdlib = ["bincode", "jrsonnet-parser/deserialize"]
 # Rustc-like trace visualization
 explaining-traces = ["annotate-snippets"]
 # Allows library authors to throw custom errors
@@ -34,21 +32,17 @@
 thiserror = "1.0"
 gcmodule = { git = "https://github.com/CertainLach/gcmodule", branch = "jrsonnet" }
 
+serde = "1.0"
+serde_json = "1.0"
+serde_yaml = { git = "https://github.com/CertainLach/serde-yaml", branch = "feature/old-octals-quirk" }
+
 [dependencies.anyhow]
 version = "1.0"
 optional = true
 
 # Serialized stdlib
-[dependencies.serde]
-version = "1.0"
-optional = true
 [dependencies.bincode]
 version = "1.3.1"
-optional = true
-
-# Serde json
-[dependencies.serde_json]
-version = "1.0"
 optional = true
 
 # Explaining traces
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -3,15 +3,17 @@
 	equals,
 	error::{Error::*, Result},
 	operator::evaluate_mod_op,
-	parse_args, primitive_equals, push_frame, throw, with_state, ArrValue, Context,
-	EvaluationState, FuncVal, IndexableVal, LazyVal, Val,
+	parse_args, primitive_equals, push_frame, throw, with_state, ArrValue, Context, FuncVal,
+	IndexableVal, LazyVal, Val,
 };
 use format::{format_arr, format_obj};
 use gcmodule::Cc;
 use jrsonnet_interner::IStr;
 use jrsonnet_parser::{ArgsDesc, ExprLocation};
 use jrsonnet_types::ty;
-use std::{collections::HashMap, path::PathBuf, rc::Rc};
+use serde::Deserialize;
+use serde_yaml::DeserializingQuirks;
+use std::{collections::HashMap, convert::TryFrom, path::PathBuf, rc::Rc};
 
 pub mod stdlib;
 pub use stdlib::*;
@@ -128,6 +130,7 @@
 			("strReplace".into(), builtin_str_replace),
 			("splitLimit".into(), builtin_splitlimit),
 			("parseJson".into(), builtin_parse_json),
+			("parseYaml".into(), builtin_parse_yaml),
 			("asciiUpper".into(), builtin_ascii_upper),
 			("asciiLower".into(), builtin_ascii_lower),
 			("member".into(), builtin_member),
@@ -210,9 +213,30 @@
 	parse_args!(context, "parseJson", args, 1, [
 		0, s: ty!(string) => Val::Str;
 	], {
-		let state = EvaluationState::default();
-		let path = PathBuf::from("std.parseJson").into();
-		state.evaluate_snippet_raw(path ,s)
+		let value: serde_json::Value = serde_json::from_str(&s).map_err(|e| RuntimeError(format!("failed to parse json: {}", e).into()))?;
+		Ok(Val::try_from(&value)?)
+	})
+}
+
+fn builtin_parse_yaml(context: Context, _loc: &ExprLocation, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "parseYaml", args, 1, [
+		0, s: ty!(string) => Val::Str;
+	], {
+		let value = serde_yaml::Deserializer::from_str_with_quirks(&s, DeserializingQuirks { old_octals: true });
+		let mut out = vec![];
+		for item in value {
+			let value = serde_json::Value::deserialize(item)
+				.map_err(|e| RuntimeError(format!("failed to parse yaml: {}", e).into()))?;
+			let val = Val::try_from(&value)?;
+			out.push(val);
+		}
+		if out.is_empty() {
+			Ok(Val::Null)
+		} else if out.len() == 1 {
+			Ok(out.into_iter().next().unwrap())
+		} else {
+			Ok(Val::Arr(out.into()))
+		}
 	})
 }
 
modifiedcrates/jrsonnet-evaluator/src/integrations/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/integrations/mod.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/mod.rs
@@ -1,2 +1 @@
-#[cfg(feature = "serde-json")]
 pub mod serde;
modifiedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -15,7 +15,7 @@
 			Val::Num(n) => Self::Number(if n.fract() <= f64::EPSILON {
 				(*n as i64).into()
 			} else {
-				Number::from_f64(*n).expect("to json number")
+				Number::from_f64(*n).expect("jsonnet numbers can't be infinite or NaN")
 			}),
 			Val::Arr(a) => {
 				let mut out = Vec::with_capacity(a.len());
@@ -29,7 +29,9 @@
 				for key in o.fields() {
 					out.insert(
 						(&key as &str).into(),
-						(&o.get(key)?.expect("field exists")).try_into()?,
+						(&o.get(key)?
+							.expect("key is present in fields, so value should exist"))
+							.try_into()?,
 					);
 				}
 				Self::Object(out)
@@ -39,27 +41,30 @@
 	}
 }
 
-impl From<&Value> for Val {
-	fn from(v: &Value) -> Self {
-		match v {
+impl TryFrom<&Value> for Val {
+	type Error = LocError;
+	fn try_from(v: &Value) -> Result<Self> {
+		Ok(match v {
 			Value::Null => Self::Null,
 			Value::Bool(v) => Self::Bool(*v),
-			Value::Number(n) => Self::Num(n.as_f64().expect("as f64")),
+			Value::Number(n) => Self::Num(n.as_f64().ok_or_else(|| {
+				RuntimeError(format!("json number can't be represented as jsonnet: {}", n).into())
+			})?),
 			Value::String(s) => Self::Str((s as &str).into()),
 			Value::Array(a) => {
 				let mut out: Vec<Self> = Vec::with_capacity(a.len());
 				for v in a {
-					out.push(v.into());
+					out.push(v.try_into()?);
 				}
 				Self::Arr(out.into())
 			}
 			Value::Object(o) => {
 				let mut builder = ObjValueBuilder::with_capacity(o.len());
 				for (k, v) in o {
-					builder.member((k as &str).into()).value(v.into());
+					builder.member((k as &str).into()).value(v.try_into()?);
 				}
 				Self::Obj(builder.build())
 			}
-		}
+		})
 	}
 }
modifiedcrates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -98,7 +98,7 @@
 			let mut n = self.resolver.resolve(path);
 			let mut offset = error.location.offset;
 			let is_eof = if offset >= source_code.len() {
-				offset = source_code.len() - 1;
+				offset = source_code.len().saturating_sub(1);
 				true
 			} else {
 				false
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
after · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,3  local id = std.id,45  # Those functions aren't normally located in stdlib6  length:: $intrinsic(length),7  type:: $intrinsic(type),8  makeArray:: $intrinsic(makeArray),9  codepoint:: $intrinsic(codepoint),10  objectFieldsEx:: $intrinsic(objectFieldsEx),11  objectHasEx:: $intrinsic(objectHasEx),12  primitiveEquals:: $intrinsic(primitiveEquals),13  modulo:: $intrinsic(modulo),14  floor:: $intrinsic(floor),15  ceil:: $intrinsic(ceil),16  extVar:: $intrinsic(extVar),17  native:: $intrinsic(native),18  filter:: $intrinsic(filter),19  char:: $intrinsic(char),20  encodeUTF8:: $intrinsic(encodeUTF8),21  decodeUTF8:: $intrinsic(decodeUTF8),22  md5:: $intrinsic(md5),23  trace:: $intrinsic(trace),24  id:: $intrinsic(id),25  parseJson:: $intrinsic(parseJson),26  parseYaml:: $intrinsic(parseYaml),2728  log:: $intrinsic(log),29  pow:: $intrinsic(pow),30  sqrt:: $intrinsic(sqrt),3132  sin:: $intrinsic(sin),33  cos:: $intrinsic(cos),34  tan:: $intrinsic(tan),35  asin:: $intrinsic(asin),36  acos:: $intrinsic(acos),37  atan:: $intrinsic(atan),3839  exp:: $intrinsic(exp),40  mantissa:: $intrinsic(mantissa),41  exponent:: $intrinsic(exponent),4243  isString(v):: std.type(v) == 'string',44  isNumber(v):: std.type(v) == 'number',45  isBoolean(v):: std.type(v) == 'boolean',46  isObject(v):: std.type(v) == 'object',47  isArray(v):: std.type(v) == 'array',48  isFunction(v):: std.type(v) == 'function',4950  toString(a)::51    if std.type(a) == 'string' then a else '' + a,5253  substr:: $intrinsic(substr),5455  startsWith(a, b)::56    if std.length(a) < std.length(b) then57      false58    else59      std.substr(a, 0, std.length(b)) == b,6061  endsWith(a, b)::62    if std.length(a) < std.length(b) then63      false64    else65      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6667  lstripChars(str, chars)::68    if std.length(str) > 0 && std.member(chars, str[0]) then69      std.lstripChars(str[1:], chars)70    else71      str,7273  rstripChars(str, chars)::74    local len = std.length(str);75    if len > 0 && std.member(chars, str[len - 1]) then76      std.rstripChars(str[:len - 1], chars)77    else78      str,7980  stripChars(str, chars)::81    std.lstripChars(std.rstripChars(str, chars), chars),8283  stringChars(str)::84    std.makeArray(std.length(str), function(i) str[i]),8586  local parse_nat(str, base) =87    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;88    // These codepoints are in ascending order:89    local zero_code = std.codepoint('0');90    local upper_a_code = std.codepoint('A');91    local lower_a_code = std.codepoint('a');92    local addDigit(aggregate, char) =93      local code = std.codepoint(char);94      local digit = if code >= lower_a_code then95        code - lower_a_code + 1096      else if code >= upper_a_code then97        code - upper_a_code + 1098      else99        code - zero_code;100      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];101      base * aggregate + digit;102    std.foldl(addDigit, std.stringChars(str), 0),103104  parseInt(str)::105    assert std.isString(str) : 'Expected string, got ' + std.type(str);106    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];107    if str[0] == '-' then108      -parse_nat(str[1:], 10)109    else110      parse_nat(str, 10),111112  parseOctal(str)::113    assert std.isString(str) : 'Expected string, got ' + std.type(str);114    assert std.length(str) > 0 : 'Not an octal number: ""';115    parse_nat(str, 8),116117  parseHex(str)::118    assert std.isString(str) : 'Expected string, got ' + std.type(str);119    assert std.length(str) > 0 : 'Not hexadecimal: ""';120    parse_nat(str, 16),121122  split(str, c)::123    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);124    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);125    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);126    std.splitLimit(str, c, -1),127128  splitLimit:: $intrinsic(splitLimit),129130  strReplace:: $intrinsic(strReplace),131132  asciiUpper:: $intrinsic(asciiUpper),133134  asciiLower:: $intrinsic(asciiLower),135136  range:: $intrinsic(range),137138  repeat(what, count)::139    local joiner =140      if std.isString(what) then ''141      else if std.isArray(what) then []142      else error 'std.repeat first argument must be an array or a string';143    std.join(joiner, std.makeArray(count, function(i) what)),144145  slice:: $intrinsic(slice),146147  member:: $intrinsic(member),148149  count:: $intrinsic(count),150151  mod:: $intrinsic(mod),152153  map:: $intrinsic(map),154155  mapWithIndex(func, arr)::156    if !std.isFunction(func) then157      error ('std.mapWithIndex first param must be function, got ' + std.type(func))158    else if !std.isArray(arr) && !std.isString(arr) then159      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))160    else161      std.makeArray(std.length(arr), function(i) func(i, arr[i])),162163  mapWithKey(func, obj)::164    if !std.isFunction(func) then165      error ('std.mapWithKey first param must be function, got ' + std.type(func))166    else if !std.isObject(obj) then167      error ('std.mapWithKey second param must be object, got ' + std.type(obj))168    else169      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },170171  flatMap:: $intrinsic(flatMap),172173  join:: $intrinsic(join),174175  lines(arr)::176    std.join('\n', arr + ['']),177178  deepJoin(arr)::179    if std.isString(arr) then180      arr181    else if std.isArray(arr) then182      std.join('', [std.deepJoin(x) for x in arr])183    else184      error 'Expected string or array, got %s' % std.type(arr),185186187  format:: $intrinsic(format),188189  foldr:: $intrinsic(foldr),190191  foldl:: $intrinsic(foldl),192193  filterMap(filter_func, map_func, arr)::194    if !std.isFunction(filter_func) then195      error ('std.filterMap first param must be function, got ' + std.type(filter_func))196    else if !std.isFunction(map_func) then197      error ('std.filterMap second param must be function, got ' + std.type(map_func))198    else if !std.isArray(arr) then199      error ('std.filterMap third param must be array, got ' + std.type(arr))200    else201      std.map(map_func, std.filter(filter_func, arr)),202203  assertEqual(a, b)::204    if a == b then205      true206    else207      error 'Assertion failed. ' + a + ' != ' + b,208209  abs(n)::210    if !std.isNumber(n) then211      error 'std.abs expected number, got ' + std.type(n)212    else213      if n > 0 then n else -n,214215  sign(n)::216    if !std.isNumber(n) then217      error 'std.sign expected number, got ' + std.type(n)218    else219      if n > 0 then220        1221      else if n < 0 then222        -1223      else 0,224225  max(a, b)::226    if !std.isNumber(a) then227      error 'std.max first param expected number, got ' + std.type(a)228    else if !std.isNumber(b) then229      error 'std.max second param expected number, got ' + std.type(b)230    else231      if a > b then a else b,232233  min(a, b)::234    if !std.isNumber(a) then235      error 'std.min first param expected number, got ' + std.type(a)236    else if !std.isNumber(b) then237      error 'std.min second param expected number, got ' + std.type(b)238    else239      if a < b then a else b,240241  clamp(x, minVal, maxVal)::242    if x < minVal then minVal243    else if x > maxVal then maxVal244    else x,245246  flattenArrays(arrs)::247    std.foldl(function(a, b) a + b, arrs, []),248249  manifestIni(ini)::250    local body_lines(body) =251      std.join([], [252        local value_or_values = body[k];253        if std.isArray(value_or_values) then254          ['%s = %s' % [k, value] for value in value_or_values]255        else256          ['%s = %s' % [k, value_or_values]]257258        for k in std.objectFields(body)259      ]);260261    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),262          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],263          all_sections = [264      section_lines(k, ini.sections[k])265      for k in std.objectFields(ini.sections)266    ];267    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),268269  manifestToml(value):: std.manifestTomlEx(value, '  '),270271  manifestTomlEx(value, indent)::272    local273      escapeStringToml = std.escapeStringJson,274      escapeKeyToml(key) =275        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));276        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),277      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),278      isSection(v) = std.isObject(v) || isTableArray(v),279      renderValue(v, indexedPath, inline, cindent) =280        if v == true then281          'true'282        else if v == false then283          'false'284        else if v == null then285          error 'Tried to manifest "null" at ' + indexedPath286        else if std.isNumber(v) then287          '' + v288        else if std.isString(v) then289          escapeStringToml(v)290        else if std.isFunction(v) then291          error 'Tried to manifest function at ' + indexedPath292        else if std.isArray(v) then293          if std.length(v) == 0 then294            '[]'295          else296            local range = std.range(0, std.length(v) - 1);297            local new_indent = if inline then '' else cindent + indent;298            local separator = if inline then ' ' else '\n';299            local lines = ['[' + separator]300                          + std.join([',' + separator],301                                     [302                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]303                                       for i in range304                                     ])305                          + [separator + (if inline then '' else cindent) + ']'];306            std.join('', lines)307        else if std.isObject(v) then308          local lines = ['{ ']309                        + std.join([', '],310                                   [311                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]312                                     for k in std.objectFields(v)313                                   ])314                        + [' }'];315          std.join('', lines),316      renderTableInternal(v, path, indexedPath, cindent) =317        local kvp = std.flattenArrays([318          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]319          for k in std.objectFields(v)320          if !isSection(v[k])321        ]);322        local sections = [std.join('\n', kvp)] + [323          (324            if std.isObject(v[k]) then325              renderTable(v[k], path + [k], indexedPath + [k], cindent)326            else327              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)328          )329          for k in std.objectFields(v)330          if isSection(v[k])331        ];332        std.join('\n\n', sections),333      renderTable(v, path, indexedPath, cindent) =334        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'335        + (if v == {} then '' else '\n')336        + renderTableInternal(v, path, indexedPath, cindent + indent),337      renderTableArray(v, path, indexedPath, cindent) =338        local range = std.range(0, std.length(v) - 1);339        local sections = [340          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'341           + (if v[i] == {} then '' else '\n')342           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))343          for i in range344        ];345        std.join('\n\n', sections);346    if std.isObject(value) then347      renderTableInternal(value, [], [], '')348    else349      error 'TOML body must be an object. Got ' + std.type(value),350351  escapeStringJson:: $intrinsic(escapeStringJson),352353  escapeStringPython(str)::354    std.escapeStringJson(str),355356  escapeStringBash(str_)::357    local str = std.toString(str_);358    local trans(ch) =359      if ch == "'" then360        "'\"'\"'"361      else362        ch;363    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),364365  escapeStringDollars(str_)::366    local str = std.toString(str_);367    local trans(ch) =368      if ch == '$' then369        '$$'370      else371        ch;372    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),373374  manifestJson(value):: std.manifestJsonEx(value, '    ') tailstrict,375376  manifestJsonEx:: $intrinsic(manifestJsonEx),377378  manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),379380  manifestYamlDoc(value, indent_array_in_object=false):: std.manifestYamlDocImpl(value, indent_array_in_object),381382  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::383    if !std.isArray(value) then384      error 'manifestYamlStream only takes arrays, got ' + std.type(value)385    else386      '---\n' + std.join(387        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]388      ) + if c_document_end then '\n...\n' else '\n',389390391  manifestPython(v)::392    if std.isObject(v) then393      local fields = [394        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]395        for k in std.objectFields(v)396      ];397      '{%s}' % [std.join(', ', fields)]398    else if std.isArray(v) then399      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]400    else if std.isString(v) then401      '%s' % [std.escapeStringPython(v)]402    else if std.isFunction(v) then403      error 'cannot manifest function'404    else if std.isNumber(v) then405      std.toString(v)406    else if v == true then407      'True'408    else if v == false then409      'False'410    else if v == null then411      'None',412413  manifestPythonVars(conf)::414    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];415    std.join('\n', vars + ['']),416417  manifestXmlJsonml(value)::418    if !std.isArray(value) then419      error 'Expected a JSONML value (an array), got %s' % std.type(value)420    else421      local aux(v) =422        if std.isString(v) then423          v424        else425          local tag = v[0];426          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);427          local attrs = if has_attrs then v[1] else {};428          local children = if has_attrs then v[2:] else v[1:];429          local attrs_str =430            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);431          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);432433      aux(value),434435  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',436  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },437438  base64:: $intrinsic(base64),439440  base64DecodeBytes:: $intrinsic(base64DecodeBytes),441442  base64Decode:: $intrinsic(base64Decode),443444  reverse:: $intrinsic(reverse),445446  sortImpl:: $intrinsic(sortImpl),447448  sort(arr, keyF=id)::449    std.sortImpl(arr, keyF),450451  uniq(arr, keyF=id)::452    local f(a, b) =453      if std.length(a) == 0 then454        [b]455      else if keyF(a[std.length(a) - 1]) == keyF(b) then456        a457      else458        a + [b];459    std.foldl(f, arr, []),460461  set(arr, keyF=id)::462    std.uniq(std.sort(arr, keyF), keyF),463464  setMember(x, arr, keyF=id)::465    // TODO(dcunnin): Binary chop for O(log n) complexity466    std.length(std.setInter([x], arr, keyF)) > 0,467468  setUnion(a, b, keyF=id)::469    // NOTE: order matters, values in `a` win470    local aux(a, b, i, j, acc) =471      if i >= std.length(a) then472        acc + b[j:]473      else if j >= std.length(b) then474        acc + a[i:]475      else476        local ak = keyF(a[i]);477        local bk = keyF(b[j]);478        if ak == bk then479          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict480        else if ak < bk then481          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict482        else483          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;484    aux(a, b, 0, 0, []),485486  setInter(a, b, keyF=id)::487    local aux(a, b, i, j, acc) =488      if i >= std.length(a) || j >= std.length(b) then489        acc490      else491        if keyF(a[i]) == keyF(b[j]) then492          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict493        else if keyF(a[i]) < keyF(b[j]) then494          aux(a, b, i + 1, j, acc) tailstrict495        else496          aux(a, b, i, j + 1, acc) tailstrict;497    aux(a, b, 0, 0, []) tailstrict,498499  setDiff(a, b, keyF=id)::500    local aux(a, b, i, j, acc) =501      if i >= std.length(a) then502        acc503      else if j >= std.length(b) then504        acc + a[i:]505      else506        if keyF(a[i]) == keyF(b[j]) then507          aux(a, b, i + 1, j + 1, acc) tailstrict508        else if keyF(a[i]) < keyF(b[j]) then509          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict510        else511          aux(a, b, i, j + 1, acc) tailstrict;512    aux(a, b, 0, 0, []) tailstrict,513514  mergePatch(target, patch)::515    if std.isObject(patch) then516      local target_object =517        if std.isObject(target) then target else {};518519      local target_fields =520        if std.isObject(target_object) then std.objectFields(target_object) else [];521522      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];523      local both_fields = std.setUnion(target_fields, std.objectFields(patch));524525      {526        [k]:527          if !std.objectHas(patch, k) then528            target_object[k]529          else if !std.objectHas(target_object, k) then530            std.mergePatch(null, patch[k]) tailstrict531          else532            std.mergePatch(target_object[k], patch[k]) tailstrict533        for k in std.setDiff(both_fields, null_fields)534      }535    else536      patch,537538  objectFields(o)::539    std.objectFieldsEx(o, false),540541  objectFieldsAll(o)::542    std.objectFieldsEx(o, true),543544  objectHas(o, f)::545    std.objectHasEx(o, f, false),546547  objectHasAll(o, f)::548    std.objectHasEx(o, f, true),549550  objectValues(o)::551    [o[k] for k in std.objectFields(o)],552553  objectValuesAll(o)::554    [o[k] for k in std.objectFieldsAll(o)],555556  equals:: $intrinsic(equals),557558  resolvePath(f, r)::559    local arr = std.split(f, '/');560    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),561562  prune(a)::563    local isContent(b) =564      if b == null then565        false566      else if std.isArray(b) then567        std.length(b) > 0568      else if std.isObject(b) then569        std.length(b) > 0570      else571        true;572    if std.isArray(a) then573      [std.prune(x) for x in a if isContent($.prune(x))]574    else if std.isObject(a) then {575      [x]: $.prune(a[x])576      for x in std.objectFields(a)577      if isContent(std.prune(a[x]))578    } else579      a,580581  findSubstr(pat, str)::582    if !std.isString(pat) then583      error 'findSubstr first parameter should be a string, got ' + std.type(pat)584    else if !std.isString(str) then585      error 'findSubstr second parameter should be a string, got ' + std.type(str)586    else587      local pat_len = std.length(pat);588      local str_len = std.length(str);589      if pat_len == 0 || str_len == 0 || pat_len > str_len then590        []591      else592        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),593594  find(value, arr)::595    if !std.isArray(arr) then596      error 'find second parameter should be an array, got ' + std.type(arr)597    else598      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),599}