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

difftreelog

feat std.ceil builtin

Yaroslav Bolyukin2021-07-12parent: #46b4dd0.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -86,6 +86,7 @@
 			("modulo".into(), builtin_modulo),
 			("mod".into(), builtin_mod),
 			("floor".into(), builtin_floor),
+			("ceil".into(), builtin_ceil),
 			("log".into(), builtin_log),
 			("pow".into(), builtin_pow),
 			("extVar".into(), builtin_ext_var),
@@ -278,6 +279,14 @@
 	})
 }
 
+fn builtin_ceil(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "ceil", args, 1, [
+		0, x: ty!(number) => Val::Num;
+	], {
+		Ok(Val::Num(x.ceil()))
+	})
+}
+
 fn builtin_log(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "log", args, 1, [
 		0, n: ty!(number) => Val::Num;
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
before · 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  log:: $intrinsic(log),16  pow:: $intrinsic(pow),17  extVar:: $intrinsic(extVar),18  native:: $intrinsic(native),19  filter:: $intrinsic(filter),20  char:: $intrinsic(char),21  encodeUTF8:: $intrinsic(encodeUTF8),22  md5:: $intrinsic(md5),23  trace:: $intrinsic(trace),24  id:: $intrinsic(id),25  parseJson:: $intrinsic(parseJson),2627  isString(v):: std.type(v) == 'string',28  isNumber(v):: std.type(v) == 'number',29  isBoolean(v):: std.type(v) == 'boolean',30  isObject(v):: std.type(v) == 'object',31  isArray(v):: std.type(v) == 'array',32  isFunction(v):: std.type(v) == 'function',3334  toString(a)::35    if std.type(a) == 'string' then a else '' + a,3637  substr(str, from, len)::38    assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);39    assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);40    assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);41    assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;42    std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),4344  startsWith(a, b)::45    if std.length(a) < std.length(b) then46      false47    else48      std.substr(a, 0, std.length(b)) == b,4950  endsWith(a, b)::51    if std.length(a) < std.length(b) then52      false53    else54      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,5556  lstripChars(str, chars)::57    if std.length(str) > 0 && std.member(chars, str[0]) then58      std.lstripChars(str[1:], chars)59    else60      str,6162  rstripChars(str, chars)::63    local len = std.length(str);64    if len > 0 && std.member(chars, str[len - 1]) then65      std.rstripChars(str[:len - 1], chars)66    else67      str,6869  stripChars(str, chars)::70    std.lstripChars(std.rstripChars(str, chars), chars),7172  stringChars(str)::73    std.makeArray(std.length(str), function(i) str[i]),7475  local parse_nat(str, base) =76    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;77    // These codepoints are in ascending order:78    local zero_code = std.codepoint('0');79    local upper_a_code = std.codepoint('A');80    local lower_a_code = std.codepoint('a');81    local addDigit(aggregate, char) =82      local code = std.codepoint(char);83      local digit = if code >= lower_a_code then84        code - lower_a_code + 1085      else if code >= upper_a_code then86        code - upper_a_code + 1087      else88        code - zero_code;89      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];90      base * aggregate + digit;91    std.foldl(addDigit, std.stringChars(str), 0),9293  parseInt(str)::94    assert std.isString(str) : 'Expected string, got ' + std.type(str);95    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];96    if str[0] == '-' then97      -parse_nat(str[1:], 10)98    else99      parse_nat(str, 10),100101  parseOctal(str)::102    assert std.isString(str) : 'Expected string, got ' + std.type(str);103    assert std.length(str) > 0 : 'Not an octal number: ""';104    parse_nat(str, 8),105106  parseHex(str)::107    assert std.isString(str) : 'Expected string, got ' + std.type(str);108    assert std.length(str) > 0 : 'Not hexadecimal: ""';109    parse_nat(str, 16),110111  split(str, c)::112    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);113    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);114    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);115    std.splitLimit(str, c, -1),116117  splitLimit(str, c, maxsplits)::118    assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);119    assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);120    assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);121    assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);122    local aux(str, delim, i, arr, v) =123      local c = str[i];124      local i2 = i + 1;125      if i >= std.length(str) then126        arr + [v]127      else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then128        aux(str, delim, i2, arr + [v], '') tailstrict129      else130        aux(str, delim, i2, arr, v + c) tailstrict;131    aux(str, c, 0, [], ''),132133  strReplace:: $intrinsic(strReplace),134135  asciiUpper(str)::136    local cp = std.codepoint;137    local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then138      std.char(cp(c) - 32)139    else140      c;141    std.join('', std.map(up_letter, std.stringChars(str))),142143  asciiLower(str)::144    local cp = std.codepoint;145    local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then146      std.char(cp(c) + 32)147    else148      c;149    std.join('', std.map(down_letter, std.stringChars(str))),150151  range:: $intrinsic(range),152153  repeat(what, count)::154    local joiner =155      if std.isString(what) then ''156      else if std.isArray(what) then []157      else error 'std.repeat first argument must be an array or a string';158    std.join(joiner, std.makeArray(count, function(i) what)),159160  slice:: $intrinsic(slice),161162  member(arr, x)::163    if std.isArray(arr) then164      std.count(arr, x) > 0165    else if std.isString(arr) then166      std.length(std.findSubstr(x, arr)) > 0167    else error 'std.member first argument must be an array or a string',168169  count(arr, x):: std.length(std.filter(function(v) v == x, arr)),170171  mod:: $intrinsic(mod),172173  map:: $intrinsic(map),174175  mapWithIndex(func, arr)::176    if !std.isFunction(func) then177      error ('std.mapWithIndex first param must be function, got ' + std.type(func))178    else if !std.isArray(arr) && !std.isString(arr) then179      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))180    else181      std.makeArray(std.length(arr), function(i) func(i, arr[i])),182183  mapWithKey(func, obj)::184    if !std.isFunction(func) then185      error ('std.mapWithKey first param must be function, got ' + std.type(func))186    else if !std.isObject(obj) then187      error ('std.mapWithKey second param must be object, got ' + std.type(obj))188    else189      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },190191  flatMap:: $intrinsic(flatMap),192193  join:: $intrinsic(join),194195  lines(arr)::196    std.join('\n', arr + ['']),197198  deepJoin(arr)::199    if std.isString(arr) then200      arr201    else if std.isArray(arr) then202      std.join('', [std.deepJoin(x) for x in arr])203    else204      error 'Expected string or array, got %s' % std.type(arr),205206207  format:: $intrinsic(format),208209  foldr:: $intrinsic(foldr),210211  foldl:: $intrinsic(foldl),212213  filterMap(filter_func, map_func, arr)::214    if !std.isFunction(filter_func) then215      error ('std.filterMap first param must be function, got ' + std.type(filter_func))216    else if !std.isFunction(map_func) then217      error ('std.filterMap second param must be function, got ' + std.type(map_func))218    else if !std.isArray(arr) then219      error ('std.filterMap third param must be array, got ' + std.type(arr))220    else221      std.map(map_func, std.filter(filter_func, arr)),222223  assertEqual(a, b)::224    if a == b then225      true226    else227      error 'Assertion failed. ' + a + ' != ' + b,228229  abs(n)::230    if !std.isNumber(n) then231      error 'std.abs expected number, got ' + std.type(n)232    else233      if n > 0 then n else -n,234235  sign(n)::236    if !std.isNumber(n) then237      error 'std.sign expected number, got ' + std.type(n)238    else239      if n > 0 then240        1241      else if n < 0 then242        -1243      else 0,244245  max(a, b)::246    if !std.isNumber(a) then247      error 'std.max first param expected number, got ' + std.type(a)248    else if !std.isNumber(b) then249      error 'std.max second param expected number, got ' + std.type(b)250    else251      if a > b then a else b,252253  min(a, b)::254    if !std.isNumber(a) then255      error 'std.min first param expected number, got ' + std.type(a)256    else if !std.isNumber(b) then257      error 'std.min second param expected number, got ' + std.type(b)258    else259      if a < b then a else b,260261  clamp(x, minVal, maxVal)::262    if x < minVal then minVal263    else if x > maxVal then maxVal264    else x,265266  flattenArrays(arrs)::267    std.foldl(function(a, b) a + b, arrs, []),268269  manifestIni(ini)::270    local body_lines(body) =271      std.join([], [272        local value_or_values = body[k];273        if std.isArray(value_or_values) then274          ['%s = %s' % [k, value] for value in value_or_values]275        else276          ['%s = %s' % [k, value_or_values]]277278        for k in std.objectFields(body)279      ]);280281    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),282          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],283          all_sections = [284      section_lines(k, ini.sections[k])285      for k in std.objectFields(ini.sections)286    ];287    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),288289  manifestToml(value):: std.manifestTomlEx(value, '  '),290291  manifestTomlEx(value, indent)::292    local293      escapeStringToml = std.escapeStringJson,294      escapeKeyToml(key) =295        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));296        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),297      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),298      isSection(v) = std.isObject(v) || isTableArray(v),299      renderValue(v, indexedPath, inline, cindent) =300        if v == true then301          'true'302        else if v == false then303          'false'304        else if v == null then305          error 'Tried to manifest "null" at ' + indexedPath306        else if std.isNumber(v) then307          '' + v308        else if std.isString(v) then309          escapeStringToml(v)310        else if std.isFunction(v) then311          error 'Tried to manifest function at ' + indexedPath312        else if std.isArray(v) then313          if std.length(v) == 0 then314            '[]'315          else316            local range = std.range(0, std.length(v) - 1);317            local new_indent = if inline then '' else cindent + indent;318            local separator = if inline then ' ' else '\n';319            local lines = ['[' + separator]320                          + std.join([',' + separator],321                                     [322                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]323                                       for i in range324                                     ])325                          + [separator + (if inline then '' else cindent) + ']'];326            std.join('', lines)327        else if std.isObject(v) then328          local lines = ['{ ']329                        + std.join([', '],330                                   [331                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]332                                     for k in std.objectFields(v)333                                   ])334                        + [' }'];335          std.join('', lines),336      renderTableInternal(v, path, indexedPath, cindent) =337        local kvp = std.flattenArrays([338          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]339          for k in std.objectFields(v)340          if !isSection(v[k])341        ]);342        local sections = [std.join('\n', kvp)] + [343          (344            if std.isObject(v[k]) then345              renderTable(v[k], path + [k], indexedPath + [k], cindent)346            else347              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)348          )349          for k in std.objectFields(v)350          if isSection(v[k])351        ];352        std.join('\n\n', sections),353      renderTable(v, path, indexedPath, cindent) =354        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'355        + (if v == {} then '' else '\n')356        + renderTableInternal(v, path, indexedPath, cindent + indent),357      renderTableArray(v, path, indexedPath, cindent) =358        local range = std.range(0, std.length(v) - 1);359        local sections = [360          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'361           + (if v[i] == {} then '' else '\n')362           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))363          for i in range364        ];365        std.join('\n\n', sections);366    if std.isObject(value) then367      renderTableInternal(value, [], [], '')368    else369      error 'TOML body must be an object. Got ' + std.type(value),370371  escapeStringJson:: $intrinsic(escapeStringJson),372373  escapeStringPython(str)::374    std.escapeStringJson(str),375376  escapeStringBash(str_)::377    local str = std.toString(str_);378    local trans(ch) =379      if ch == "'" then380        "'\"'\"'"381      else382        ch;383    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),384385  escapeStringDollars(str_)::386    local str = std.toString(str_);387    local trans(ch) =388      if ch == '$' then389        '$$'390      else391        ch;392    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),393394  manifestJson(value):: std.manifestJsonEx(value, '    '),395396  manifestJsonEx:: $intrinsic(manifestJsonEx),397398  manifestYamlDoc(value, indent_array_in_object=false)::399    local aux(v, path, cindent) =400      if v == true then401        'true'402      else if v == false then403        'false'404      else if v == null then405        'null'406      else if std.isNumber(v) then407        '' + v408      else if std.isString(v) then409        local len = std.length(v);410        if len == 0 then411          '""'412        else if v[len - 1] == '\n' then413          local split = std.split(v, '\n');414          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])415        else416          std.escapeStringJson(v)417      else if std.isFunction(v) then418        error 'Tried to manifest function at ' + path419      else if std.isArray(v) then420        if std.length(v) == 0 then421          '[]'422        else423          local params(value) =424            if std.isArray(value) && std.length(value) > 0 then {425              // While we could avoid the new line, it yields YAML that is426              // hard to read, e.g.:427              // - - - 1428              //     - 2429              //   - - 3430              //     - 4431              new_indent: cindent + '  ',432              space: '\n' + self.new_indent,433            } else if std.isObject(value) && std.length(value) > 0 then {434              new_indent: cindent + '  ',435              // In this case we can start on the same line as the - because the indentation436              // matches up then.  The converse is not true, because fields are not always437              // 1 character long.438              space: ' ',439            } else {440              // In this case, new_indent is only used in the case of multi-line strings.441              new_indent: cindent,442              space: ' ',443            };444          local range = std.range(0, std.length(v) - 1);445          local parts = [446            '-' + param.space + aux(v[i], path + [i], param.new_indent)447            for i in range448            for param in [params(v[i])]449          ];450          std.join('\n' + cindent, parts)451      else if std.isObject(v) then452        if std.length(v) == 0 then453          '{}'454        else455          local params(value) =456            if std.isArray(value) && std.length(value) > 0 then {457              // Not indenting allows e.g.458              // ports:459              // - 80460              // instead of461              // ports:462              //   - 80463              new_indent: if indent_array_in_object then cindent + '  ' else cindent,464              space: '\n' + self.new_indent,465            } else if std.isObject(value) && std.length(value) > 0 then {466              new_indent: cindent + '  ',467              space: '\n' + self.new_indent,468            } else {469              // In this case, new_indent is only used in the case of multi-line strings.470              new_indent: cindent,471              space: ' ',472            };473          local lines = [474            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)475            for k in std.objectFields(v)476            for param in [params(v[k])]477          ];478          std.join('\n' + cindent, lines);479    aux(value, [], ''),480481  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::482    if !std.isArray(value) then483      error 'manifestYamlStream only takes arrays, got ' + std.type(value)484    else485      '---\n' + std.join(486        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]487      ) + if c_document_end then '\n...\n' else '\n',488489490  manifestPython(v)::491    if std.isObject(v) then492      local fields = [493        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]494        for k in std.objectFields(v)495      ];496      '{%s}' % [std.join(', ', fields)]497    else if std.isArray(v) then498      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]499    else if std.isString(v) then500      '%s' % [std.escapeStringPython(v)]501    else if std.isFunction(v) then502      error 'cannot manifest function'503    else if std.isNumber(v) then504      std.toString(v)505    else if v == true then506      'True'507    else if v == false then508      'False'509    else if v == null then510      'None',511512  manifestPythonVars(conf)::513    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];514    std.join('\n', vars + ['']),515516  manifestXmlJsonml(value)::517    if !std.isArray(value) then518      error 'Expected a JSONML value (an array), got %s' % std.type(value)519    else520      local aux(v) =521        if std.isString(v) then522          v523        else524          local tag = v[0];525          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);526          local attrs = if has_attrs then v[1] else {};527          local children = if has_attrs then v[2:] else v[1:];528          local attrs_str =529            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);530          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);531532      aux(value),533534  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',535  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },536537  base64:: $intrinsic(base64),538539  base64DecodeBytes(str)::540    if std.length(str) % 4 != 0 then541      error 'Not a base64 encoded string "%s"' % str542    else543      local aux(str, i, r) =544        if i >= std.length(str) then545          r546        else547          // all 6 bits of i, 2 MSB of i+1548          local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];549          // 4 LSB of i+1, 4MSB of i+2550          local n2 =551            if str[i + 2] == '=' then []552            else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];553          // 2 LSB of i+2, all 6 bits of i+3554          local n3 =555            if str[i + 3] == '=' then []556            else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];557          aux(str, i + 4, r + n1 + n2 + n3) tailstrict;558      aux(str, 0, []),559560  base64Decode(str)::561    local bytes = std.base64DecodeBytes(str);562    std.join('', std.map(function(b) std.char(b), bytes)),563564  reverse:: $intrinsic(reverse),565566  sortImpl:: $intrinsic(sortImpl),567568  sort(arr, keyF=id)::569    std.sortImpl(arr, keyF),570571  uniq(arr, keyF=id)::572    local f(a, b) =573      if std.length(a) == 0 then574        [b]575      else if keyF(a[std.length(a) - 1]) == keyF(b) then576        a577      else578        a + [b];579    std.foldl(f, arr, []),580581  set(arr, keyF=id)::582    std.uniq(std.sort(arr, keyF), keyF),583584  setMember(x, arr, keyF=id)::585    // TODO(dcunnin): Binary chop for O(log n) complexity586    std.length(std.setInter([x], arr, keyF)) > 0,587588  setUnion(a, b, keyF=id)::589    // NOTE: order matters, values in `a` win590    local aux(a, b, i, j, acc) =591      if i >= std.length(a) then592        acc + b[j:]593      else if j >= std.length(b) then594        acc + a[i:]595      else596        local ak = keyF(a[i]);597        local bk = keyF(b[j]);598        if ak == bk then599          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict600        else if ak < bk then601          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict602        else603          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;604    aux(a, b, 0, 0, []),605606  setInter(a, b, keyF=id)::607    local aux(a, b, i, j, acc) =608      if i >= std.length(a) || j >= std.length(b) then609        acc610      else611        if keyF(a[i]) == keyF(b[j]) then612          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict613        else if keyF(a[i]) < keyF(b[j]) then614          aux(a, b, i + 1, j, acc) tailstrict615        else616          aux(a, b, i, j + 1, acc) tailstrict;617    aux(a, b, 0, 0, []) tailstrict,618619  setDiff(a, b, keyF=id)::620    local aux(a, b, i, j, acc) =621      if i >= std.length(a) then622        acc623      else if j >= std.length(b) then624        acc + a[i:]625      else626        if keyF(a[i]) == keyF(b[j]) then627          aux(a, b, i + 1, j + 1, acc) tailstrict628        else if keyF(a[i]) < keyF(b[j]) then629          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict630        else631          aux(a, b, i, j + 1, acc) tailstrict;632    aux(a, b, 0, 0, []) tailstrict,633634  mergePatch(target, patch)::635    if std.isObject(patch) then636      local target_object =637        if std.isObject(target) then target else {};638639      local target_fields =640        if std.isObject(target_object) then std.objectFields(target_object) else [];641642      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];643      local both_fields = std.setUnion(target_fields, std.objectFields(patch));644645      {646        [k]:647          if !std.objectHas(patch, k) then648            target_object[k]649          else if !std.objectHas(target_object, k) then650            std.mergePatch(null, patch[k]) tailstrict651          else652            std.mergePatch(target_object[k], patch[k]) tailstrict653        for k in std.setDiff(both_fields, null_fields)654      }655    else656      patch,657658  objectFields(o)::659    std.objectFieldsEx(o, false),660661  objectFieldsAll(o)::662    std.objectFieldsEx(o, true),663664  objectHas(o, f)::665    std.objectHasEx(o, f, false),666667  objectHasAll(o, f)::668    std.objectHasEx(o, f, true),669670  objectValues(o)::671    [o[k] for k in std.objectFields(o)],672673  objectValuesAll(o)::674    [o[k] for k in std.objectFieldsAll(o)],675676  equals:: $intrinsic(equals),677678  resolvePath(f, r)::679    local arr = std.split(f, '/');680    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),681682  prune(a)::683    local isContent(b) =684      if b == null then685        false686      else if std.isArray(b) then687        std.length(b) > 0688      else if std.isObject(b) then689        std.length(b) > 0690      else691        true;692    if std.isArray(a) then693      [std.prune(x) for x in a if isContent($.prune(x))]694    else if std.isObject(a) then {695      [x]: $.prune(a[x])696      for x in std.objectFields(a)697      if isContent(std.prune(a[x]))698    } else699      a,700701  findSubstr(pat, str)::702    if !std.isString(pat) then703      error 'findSubstr first parameter should be a string, got ' + std.type(pat)704    else if !std.isString(str) then705      error 'findSubstr second parameter should be a string, got ' + std.type(str)706    else707      local pat_len = std.length(pat);708      local str_len = std.length(str);709      if pat_len == 0 || str_len == 0 || pat_len > str_len then710        []711      else712        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),713714  find(value, arr)::715    if !std.isArray(arr) then716      error 'find second parameter should be an array, got ' + std.type(arr)717    else718      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),719}
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  log:: $intrinsic(log),17  pow:: $intrinsic(pow),18  extVar:: $intrinsic(extVar),19  native:: $intrinsic(native),20  filter:: $intrinsic(filter),21  char:: $intrinsic(char),22  encodeUTF8:: $intrinsic(encodeUTF8),23  md5:: $intrinsic(md5),24  trace:: $intrinsic(trace),25  id:: $intrinsic(id),26  parseJson:: $intrinsic(parseJson),2728  isString(v):: std.type(v) == 'string',29  isNumber(v):: std.type(v) == 'number',30  isBoolean(v):: std.type(v) == 'boolean',31  isObject(v):: std.type(v) == 'object',32  isArray(v):: std.type(v) == 'array',33  isFunction(v):: std.type(v) == 'function',3435  toString(a)::36    if std.type(a) == 'string' then a else '' + a,3738  substr(str, from, len)::39    assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);40    assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);41    assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);42    assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;43    std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),4445  startsWith(a, b)::46    if std.length(a) < std.length(b) then47      false48    else49      std.substr(a, 0, std.length(b)) == b,5051  endsWith(a, b)::52    if std.length(a) < std.length(b) then53      false54    else55      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,5657  lstripChars(str, chars)::58    if std.length(str) > 0 && std.member(chars, str[0]) then59      std.lstripChars(str[1:], chars)60    else61      str,6263  rstripChars(str, chars)::64    local len = std.length(str);65    if len > 0 && std.member(chars, str[len - 1]) then66      std.rstripChars(str[:len - 1], chars)67    else68      str,6970  stripChars(str, chars)::71    std.lstripChars(std.rstripChars(str, chars), chars),7273  stringChars(str)::74    std.makeArray(std.length(str), function(i) str[i]),7576  local parse_nat(str, base) =77    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;78    // These codepoints are in ascending order:79    local zero_code = std.codepoint('0');80    local upper_a_code = std.codepoint('A');81    local lower_a_code = std.codepoint('a');82    local addDigit(aggregate, char) =83      local code = std.codepoint(char);84      local digit = if code >= lower_a_code then85        code - lower_a_code + 1086      else if code >= upper_a_code then87        code - upper_a_code + 1088      else89        code - zero_code;90      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];91      base * aggregate + digit;92    std.foldl(addDigit, std.stringChars(str), 0),9394  parseInt(str)::95    assert std.isString(str) : 'Expected string, got ' + std.type(str);96    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];97    if str[0] == '-' then98      -parse_nat(str[1:], 10)99    else100      parse_nat(str, 10),101102  parseOctal(str)::103    assert std.isString(str) : 'Expected string, got ' + std.type(str);104    assert std.length(str) > 0 : 'Not an octal number: ""';105    parse_nat(str, 8),106107  parseHex(str)::108    assert std.isString(str) : 'Expected string, got ' + std.type(str);109    assert std.length(str) > 0 : 'Not hexadecimal: ""';110    parse_nat(str, 16),111112  split(str, c)::113    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);114    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);115    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);116    std.splitLimit(str, c, -1),117118  splitLimit(str, c, maxsplits)::119    assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);120    assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);121    assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);122    assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);123    local aux(str, delim, i, arr, v) =124      local c = str[i];125      local i2 = i + 1;126      if i >= std.length(str) then127        arr + [v]128      else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then129        aux(str, delim, i2, arr + [v], '') tailstrict130      else131        aux(str, delim, i2, arr, v + c) tailstrict;132    aux(str, c, 0, [], ''),133134  strReplace:: $intrinsic(strReplace),135136  asciiUpper(str)::137    local cp = std.codepoint;138    local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then139      std.char(cp(c) - 32)140    else141      c;142    std.join('', std.map(up_letter, std.stringChars(str))),143144  asciiLower(str)::145    local cp = std.codepoint;146    local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then147      std.char(cp(c) + 32)148    else149      c;150    std.join('', std.map(down_letter, std.stringChars(str))),151152  range:: $intrinsic(range),153154  repeat(what, count)::155    local joiner =156      if std.isString(what) then ''157      else if std.isArray(what) then []158      else error 'std.repeat first argument must be an array or a string';159    std.join(joiner, std.makeArray(count, function(i) what)),160161  slice:: $intrinsic(slice),162163  member(arr, x)::164    if std.isArray(arr) then165      std.count(arr, x) > 0166    else if std.isString(arr) then167      std.length(std.findSubstr(x, arr)) > 0168    else error 'std.member first argument must be an array or a string',169170  count(arr, x):: std.length(std.filter(function(v) v == x, arr)),171172  mod:: $intrinsic(mod),173174  map:: $intrinsic(map),175176  mapWithIndex(func, arr)::177    if !std.isFunction(func) then178      error ('std.mapWithIndex first param must be function, got ' + std.type(func))179    else if !std.isArray(arr) && !std.isString(arr) then180      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))181    else182      std.makeArray(std.length(arr), function(i) func(i, arr[i])),183184  mapWithKey(func, obj)::185    if !std.isFunction(func) then186      error ('std.mapWithKey first param must be function, got ' + std.type(func))187    else if !std.isObject(obj) then188      error ('std.mapWithKey second param must be object, got ' + std.type(obj))189    else190      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },191192  flatMap:: $intrinsic(flatMap),193194  join:: $intrinsic(join),195196  lines(arr)::197    std.join('\n', arr + ['']),198199  deepJoin(arr)::200    if std.isString(arr) then201      arr202    else if std.isArray(arr) then203      std.join('', [std.deepJoin(x) for x in arr])204    else205      error 'Expected string or array, got %s' % std.type(arr),206207208  format:: $intrinsic(format),209210  foldr:: $intrinsic(foldr),211212  foldl:: $intrinsic(foldl),213214  filterMap(filter_func, map_func, arr)::215    if !std.isFunction(filter_func) then216      error ('std.filterMap first param must be function, got ' + std.type(filter_func))217    else if !std.isFunction(map_func) then218      error ('std.filterMap second param must be function, got ' + std.type(map_func))219    else if !std.isArray(arr) then220      error ('std.filterMap third param must be array, got ' + std.type(arr))221    else222      std.map(map_func, std.filter(filter_func, arr)),223224  assertEqual(a, b)::225    if a == b then226      true227    else228      error 'Assertion failed. ' + a + ' != ' + b,229230  abs(n)::231    if !std.isNumber(n) then232      error 'std.abs expected number, got ' + std.type(n)233    else234      if n > 0 then n else -n,235236  sign(n)::237    if !std.isNumber(n) then238      error 'std.sign expected number, got ' + std.type(n)239    else240      if n > 0 then241        1242      else if n < 0 then243        -1244      else 0,245246  max(a, b)::247    if !std.isNumber(a) then248      error 'std.max first param expected number, got ' + std.type(a)249    else if !std.isNumber(b) then250      error 'std.max second param expected number, got ' + std.type(b)251    else252      if a > b then a else b,253254  min(a, b)::255    if !std.isNumber(a) then256      error 'std.min first param expected number, got ' + std.type(a)257    else if !std.isNumber(b) then258      error 'std.min second param expected number, got ' + std.type(b)259    else260      if a < b then a else b,261262  clamp(x, minVal, maxVal)::263    if x < minVal then minVal264    else if x > maxVal then maxVal265    else x,266267  flattenArrays(arrs)::268    std.foldl(function(a, b) a + b, arrs, []),269270  manifestIni(ini)::271    local body_lines(body) =272      std.join([], [273        local value_or_values = body[k];274        if std.isArray(value_or_values) then275          ['%s = %s' % [k, value] for value in value_or_values]276        else277          ['%s = %s' % [k, value_or_values]]278279        for k in std.objectFields(body)280      ]);281282    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),283          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],284          all_sections = [285      section_lines(k, ini.sections[k])286      for k in std.objectFields(ini.sections)287    ];288    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),289290  manifestToml(value):: std.manifestTomlEx(value, '  '),291292  manifestTomlEx(value, indent)::293    local294      escapeStringToml = std.escapeStringJson,295      escapeKeyToml(key) =296        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));297        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),298      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),299      isSection(v) = std.isObject(v) || isTableArray(v),300      renderValue(v, indexedPath, inline, cindent) =301        if v == true then302          'true'303        else if v == false then304          'false'305        else if v == null then306          error 'Tried to manifest "null" at ' + indexedPath307        else if std.isNumber(v) then308          '' + v309        else if std.isString(v) then310          escapeStringToml(v)311        else if std.isFunction(v) then312          error 'Tried to manifest function at ' + indexedPath313        else if std.isArray(v) then314          if std.length(v) == 0 then315            '[]'316          else317            local range = std.range(0, std.length(v) - 1);318            local new_indent = if inline then '' else cindent + indent;319            local separator = if inline then ' ' else '\n';320            local lines = ['[' + separator]321                          + std.join([',' + separator],322                                     [323                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]324                                       for i in range325                                     ])326                          + [separator + (if inline then '' else cindent) + ']'];327            std.join('', lines)328        else if std.isObject(v) then329          local lines = ['{ ']330                        + std.join([', '],331                                   [332                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]333                                     for k in std.objectFields(v)334                                   ])335                        + [' }'];336          std.join('', lines),337      renderTableInternal(v, path, indexedPath, cindent) =338        local kvp = std.flattenArrays([339          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]340          for k in std.objectFields(v)341          if !isSection(v[k])342        ]);343        local sections = [std.join('\n', kvp)] + [344          (345            if std.isObject(v[k]) then346              renderTable(v[k], path + [k], indexedPath + [k], cindent)347            else348              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)349          )350          for k in std.objectFields(v)351          if isSection(v[k])352        ];353        std.join('\n\n', sections),354      renderTable(v, path, indexedPath, cindent) =355        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'356        + (if v == {} then '' else '\n')357        + renderTableInternal(v, path, indexedPath, cindent + indent),358      renderTableArray(v, path, indexedPath, cindent) =359        local range = std.range(0, std.length(v) - 1);360        local sections = [361          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'362           + (if v[i] == {} then '' else '\n')363           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))364          for i in range365        ];366        std.join('\n\n', sections);367    if std.isObject(value) then368      renderTableInternal(value, [], [], '')369    else370      error 'TOML body must be an object. Got ' + std.type(value),371372  escapeStringJson:: $intrinsic(escapeStringJson),373374  escapeStringPython(str)::375    std.escapeStringJson(str),376377  escapeStringBash(str_)::378    local str = std.toString(str_);379    local trans(ch) =380      if ch == "'" then381        "'\"'\"'"382      else383        ch;384    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),385386  escapeStringDollars(str_)::387    local str = std.toString(str_);388    local trans(ch) =389      if ch == '$' then390        '$$'391      else392        ch;393    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),394395  manifestJson(value):: std.manifestJsonEx(value, '    '),396397  manifestJsonEx:: $intrinsic(manifestJsonEx),398399  manifestYamlDoc(value, indent_array_in_object=false)::400    local aux(v, path, cindent) =401      if v == true then402        'true'403      else if v == false then404        'false'405      else if v == null then406        'null'407      else if std.isNumber(v) then408        '' + v409      else if std.isString(v) then410        local len = std.length(v);411        if len == 0 then412          '""'413        else if v[len - 1] == '\n' then414          local split = std.split(v, '\n');415          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])416        else417          std.escapeStringJson(v)418      else if std.isFunction(v) then419        error 'Tried to manifest function at ' + path420      else if std.isArray(v) then421        if std.length(v) == 0 then422          '[]'423        else424          local params(value) =425            if std.isArray(value) && std.length(value) > 0 then {426              // While we could avoid the new line, it yields YAML that is427              // hard to read, e.g.:428              // - - - 1429              //     - 2430              //   - - 3431              //     - 4432              new_indent: cindent + '  ',433              space: '\n' + self.new_indent,434            } else if std.isObject(value) && std.length(value) > 0 then {435              new_indent: cindent + '  ',436              // In this case we can start on the same line as the - because the indentation437              // matches up then.  The converse is not true, because fields are not always438              // 1 character long.439              space: ' ',440            } else {441              // In this case, new_indent is only used in the case of multi-line strings.442              new_indent: cindent,443              space: ' ',444            };445          local range = std.range(0, std.length(v) - 1);446          local parts = [447            '-' + param.space + aux(v[i], path + [i], param.new_indent)448            for i in range449            for param in [params(v[i])]450          ];451          std.join('\n' + cindent, parts)452      else if std.isObject(v) then453        if std.length(v) == 0 then454          '{}'455        else456          local params(value) =457            if std.isArray(value) && std.length(value) > 0 then {458              // Not indenting allows e.g.459              // ports:460              // - 80461              // instead of462              // ports:463              //   - 80464              new_indent: if indent_array_in_object then cindent + '  ' else cindent,465              space: '\n' + self.new_indent,466            } else if std.isObject(value) && std.length(value) > 0 then {467              new_indent: cindent + '  ',468              space: '\n' + self.new_indent,469            } else {470              // In this case, new_indent is only used in the case of multi-line strings.471              new_indent: cindent,472              space: ' ',473            };474          local lines = [475            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)476            for k in std.objectFields(v)477            for param in [params(v[k])]478          ];479          std.join('\n' + cindent, lines);480    aux(value, [], ''),481482  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::483    if !std.isArray(value) then484      error 'manifestYamlStream only takes arrays, got ' + std.type(value)485    else486      '---\n' + std.join(487        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]488      ) + if c_document_end then '\n...\n' else '\n',489490491  manifestPython(v)::492    if std.isObject(v) then493      local fields = [494        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]495        for k in std.objectFields(v)496      ];497      '{%s}' % [std.join(', ', fields)]498    else if std.isArray(v) then499      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]500    else if std.isString(v) then501      '%s' % [std.escapeStringPython(v)]502    else if std.isFunction(v) then503      error 'cannot manifest function'504    else if std.isNumber(v) then505      std.toString(v)506    else if v == true then507      'True'508    else if v == false then509      'False'510    else if v == null then511      'None',512513  manifestPythonVars(conf)::514    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];515    std.join('\n', vars + ['']),516517  manifestXmlJsonml(value)::518    if !std.isArray(value) then519      error 'Expected a JSONML value (an array), got %s' % std.type(value)520    else521      local aux(v) =522        if std.isString(v) then523          v524        else525          local tag = v[0];526          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);527          local attrs = if has_attrs then v[1] else {};528          local children = if has_attrs then v[2:] else v[1:];529          local attrs_str =530            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);531          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);532533      aux(value),534535  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',536  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },537538  base64:: $intrinsic(base64),539540  base64DecodeBytes(str)::541    if std.length(str) % 4 != 0 then542      error 'Not a base64 encoded string "%s"' % str543    else544      local aux(str, i, r) =545        if i >= std.length(str) then546          r547        else548          // all 6 bits of i, 2 MSB of i+1549          local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];550          // 4 LSB of i+1, 4MSB of i+2551          local n2 =552            if str[i + 2] == '=' then []553            else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];554          // 2 LSB of i+2, all 6 bits of i+3555          local n3 =556            if str[i + 3] == '=' then []557            else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];558          aux(str, i + 4, r + n1 + n2 + n3) tailstrict;559      aux(str, 0, []),560561  base64Decode(str)::562    local bytes = std.base64DecodeBytes(str);563    std.join('', std.map(function(b) std.char(b), bytes)),564565  reverse:: $intrinsic(reverse),566567  sortImpl:: $intrinsic(sortImpl),568569  sort(arr, keyF=id)::570    std.sortImpl(arr, keyF),571572  uniq(arr, keyF=id)::573    local f(a, b) =574      if std.length(a) == 0 then575        [b]576      else if keyF(a[std.length(a) - 1]) == keyF(b) then577        a578      else579        a + [b];580    std.foldl(f, arr, []),581582  set(arr, keyF=id)::583    std.uniq(std.sort(arr, keyF), keyF),584585  setMember(x, arr, keyF=id)::586    // TODO(dcunnin): Binary chop for O(log n) complexity587    std.length(std.setInter([x], arr, keyF)) > 0,588589  setUnion(a, b, keyF=id)::590    // NOTE: order matters, values in `a` win591    local aux(a, b, i, j, acc) =592      if i >= std.length(a) then593        acc + b[j:]594      else if j >= std.length(b) then595        acc + a[i:]596      else597        local ak = keyF(a[i]);598        local bk = keyF(b[j]);599        if ak == bk then600          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict601        else if ak < bk then602          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict603        else604          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;605    aux(a, b, 0, 0, []),606607  setInter(a, b, keyF=id)::608    local aux(a, b, i, j, acc) =609      if i >= std.length(a) || j >= std.length(b) then610        acc611      else612        if keyF(a[i]) == keyF(b[j]) then613          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict614        else if keyF(a[i]) < keyF(b[j]) then615          aux(a, b, i + 1, j, acc) tailstrict616        else617          aux(a, b, i, j + 1, acc) tailstrict;618    aux(a, b, 0, 0, []) tailstrict,619620  setDiff(a, b, keyF=id)::621    local aux(a, b, i, j, acc) =622      if i >= std.length(a) then623        acc624      else if j >= std.length(b) then625        acc + a[i:]626      else627        if keyF(a[i]) == keyF(b[j]) then628          aux(a, b, i + 1, j + 1, acc) tailstrict629        else if keyF(a[i]) < keyF(b[j]) then630          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict631        else632          aux(a, b, i, j + 1, acc) tailstrict;633    aux(a, b, 0, 0, []) tailstrict,634635  mergePatch(target, patch)::636    if std.isObject(patch) then637      local target_object =638        if std.isObject(target) then target else {};639640      local target_fields =641        if std.isObject(target_object) then std.objectFields(target_object) else [];642643      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];644      local both_fields = std.setUnion(target_fields, std.objectFields(patch));645646      {647        [k]:648          if !std.objectHas(patch, k) then649            target_object[k]650          else if !std.objectHas(target_object, k) then651            std.mergePatch(null, patch[k]) tailstrict652          else653            std.mergePatch(target_object[k], patch[k]) tailstrict654        for k in std.setDiff(both_fields, null_fields)655      }656    else657      patch,658659  objectFields(o)::660    std.objectFieldsEx(o, false),661662  objectFieldsAll(o)::663    std.objectFieldsEx(o, true),664665  objectHas(o, f)::666    std.objectHasEx(o, f, false),667668  objectHasAll(o, f)::669    std.objectHasEx(o, f, true),670671  objectValues(o)::672    [o[k] for k in std.objectFields(o)],673674  objectValuesAll(o)::675    [o[k] for k in std.objectFieldsAll(o)],676677  equals:: $intrinsic(equals),678679  resolvePath(f, r)::680    local arr = std.split(f, '/');681    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),682683  prune(a)::684    local isContent(b) =685      if b == null then686        false687      else if std.isArray(b) then688        std.length(b) > 0689      else if std.isObject(b) then690        std.length(b) > 0691      else692        true;693    if std.isArray(a) then694      [std.prune(x) for x in a if isContent($.prune(x))]695    else if std.isObject(a) then {696      [x]: $.prune(a[x])697      for x in std.objectFields(a)698      if isContent(std.prune(a[x]))699    } else700      a,701702  findSubstr(pat, str)::703    if !std.isString(pat) then704      error 'findSubstr first parameter should be a string, got ' + std.type(pat)705    else if !std.isString(str) then706      error 'findSubstr second parameter should be a string, got ' + std.type(str)707    else708      local pat_len = std.length(pat);709      local str_len = std.length(str);710      if pat_len == 0 || str_len == 0 || pat_len > str_len then711        []712      else713        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),714715  find(value, arr)::716    if !std.isArray(arr) then717      error 'find second parameter should be an array, got ' + std.type(arr)718    else719      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),720}