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

difftreelog

fix add missing stdlib functions

Yaroslav Bolyukin2022-12-08parent: #4c3faa0.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,3  local id = std.id,45  thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',67  toString(a):: '' + a,89  lstripChars(str, chars)::10    if std.length(str) > 0 && std.member(chars, str[0]) then11      std.lstripChars(str[1:], chars)12    else13      str,1415  rstripChars(str, chars)::16    local len = std.length(str);17    if len > 0 && std.member(chars, str[len - 1]) then18      std.rstripChars(str[:len - 1], chars)19    else20      str,2122  stripChars(str, chars)::23    std.lstripChars(std.rstripChars(str, chars), chars),2425  stringChars(str)::26    std.makeArray(std.length(str), function(i) str[i]),2728  split(str, c):: std.splitLimit(str, c, -1),2930  mapWithIndex(func, arr)::31    if !std.isFunction(func) then32      error ('std.mapWithIndex first param must be function, got ' + std.type(func))33    else if !std.isArray(arr) && !std.isString(arr) then34      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))35    else36      std.makeArray(std.length(arr), function(i) func(i, arr[i])),3738  mapWithKey(func, obj)::39    if !std.isFunction(func) then40      error ('std.mapWithKey first param must be function, got ' + std.type(func))41    else if !std.isObject(obj) then42      error ('std.mapWithKey second param must be object, got ' + std.type(obj))43    else44      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },4546  lines(arr)::47    std.join('\n', arr + ['']),4849  deepJoin(arr)::50    if std.isString(arr) then51      arr52    else if std.isArray(arr) then53      std.join('', [std.deepJoin(x) for x in arr])54    else55      error 'Expected string or array, got %s' % std.type(arr),5657  filterMap(filter_func, map_func, arr)::58    if !std.isFunction(filter_func) then59      error ('std.filterMap first param must be function, got ' + std.type(filter_func))60    else if !std.isFunction(map_func) then61      error ('std.filterMap second param must be function, got ' + std.type(map_func))62    else if !std.isArray(arr) then63      error ('std.filterMap third param must be array, got ' + std.type(arr))64    else65      std.map(map_func, std.filter(filter_func, arr)),6667  assertEqual(a, b)::68    if a == b then69      true70    else71      error 'Assertion failed. ' + a + ' != ' + b,7273  clamp(x, minVal, maxVal)::74    if x < minVal then minVal75    else if x > maxVal then maxVal76    else x,7778  flattenArrays(arrs)::79    std.foldl(function(a, b) a + b, arrs, []),8081  manifestIni(ini)::82    local body_lines(body) =83      std.join([], [84        local value_or_values = body[k];85        if std.isArray(value_or_values) then86          ['%s = %s' % [k, value] for value in value_or_values]87        else88          ['%s = %s' % [k, value_or_values]]8990        for k in std.objectFields(body)91      ]);9293    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),94          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],95          all_sections = [96      section_lines(k, ini.sections[k])97      for k in std.objectFields(ini.sections)98    ];99    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),100101  manifestToml(value):: std.manifestTomlEx(value, '  '),102103  escapeStringPython(str)::104    std.escapeStringJson(str),105106  escapeStringBash(str_)::107    local str = std.toString(str_);108    local trans(ch) =109      if ch == "'" then110        "'\"'\"'"111      else112        ch;113    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),114115  escapeStringDollars(str_)::116    local str = std.toString(str_);117    local trans(ch) =118      if ch == '$' then119        '$$'120      else121        ch;122    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),123124  manifestJson(value):: std.manifestJsonEx(value, '    ') tailstrict,125126  manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),127128  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::129    if !std.isArray(value) then130      error 'manifestYamlStream only takes arrays, got ' + std.type(value)131    else132      '---\n' + std.join(133        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]134      ) + if c_document_end then '\n...\n' else '\n',135136137  manifestPython(v)::138    if std.isObject(v) then139      local fields = [140        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]141        for k in std.objectFields(v)142      ];143      '{%s}' % [std.join(', ', fields)]144    else if std.isArray(v) then145      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]146    else if std.isString(v) then147      '%s' % [std.escapeStringPython(v)]148    else if std.isFunction(v) then149      error 'cannot manifest function'150    else if std.isNumber(v) then151      std.toString(v)152    else if v == true then153      'True'154    else if v == false then155      'False'156    else if v == null then157      'None',158159  manifestPythonVars(conf)::160    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];161    std.join('\n', vars + ['']),162163  manifestXmlJsonml(value)::164    if !std.isArray(value) then165      error 'Expected a JSONML value (an array), got %s' % std.type(value)166    else167      local aux(v) =168        if std.isString(v) then169          v170        else171          local tag = v[0];172          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);173          local attrs = if has_attrs then v[1] else {};174          local children = if has_attrs then v[2:] else v[1:];175          local attrs_str =176            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);177          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);178179      aux(value),180181  uniq(arr, keyF=id)::182    local f(a, b) =183      if std.length(a) == 0 then184        [b]185      else if keyF(a[std.length(a) - 1]) == keyF(b) then186        a187      else188        a + [b];189    std.foldl(f, arr, []),190191  set(arr, keyF=id)::192    std.uniq(std.sort(arr, keyF), keyF),193194  setMember(x, arr, keyF=id)::195    // TODO(dcunnin): Binary chop for O(log n) complexity196    std.length(std.setInter([x], arr, keyF)) > 0,197198  setUnion(a, b, keyF=id)::199    // NOTE: order matters, values in `a` win200    local aux(a, b, i, j, acc) =201      if i >= std.length(a) then202        acc + b[j:]203      else if j >= std.length(b) then204        acc + a[i:]205      else206        local ak = keyF(a[i]);207        local bk = keyF(b[j]);208        if ak == bk then209          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict210        else if ak < bk then211          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict212        else213          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;214    aux(a, b, 0, 0, []),215216  setInter(a, b, keyF=id)::217    local aux(a, b, i, j, acc) =218      if i >= std.length(a) || j >= std.length(b) then219        acc220      else221        if keyF(a[i]) == keyF(b[j]) then222          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict223        else if keyF(a[i]) < keyF(b[j]) then224          aux(a, b, i + 1, j, acc) tailstrict225        else226          aux(a, b, i, j + 1, acc) tailstrict;227    aux(a, b, 0, 0, []) tailstrict,228229  setDiff(a, b, keyF=id)::230    local aux(a, b, i, j, acc) =231      if i >= std.length(a) then232        acc233      else if j >= std.length(b) then234        acc + a[i:]235      else236        if keyF(a[i]) == keyF(b[j]) then237          aux(a, b, i + 1, j + 1, acc) tailstrict238        else if keyF(a[i]) < keyF(b[j]) then239          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict240        else241          aux(a, b, i, j + 1, acc) tailstrict;242    aux(a, b, 0, 0, []) tailstrict,243244  mergePatch(target, patch)::245    if std.isObject(patch) then246      local target_object =247        if std.isObject(target) then target else {};248249      local target_fields =250        if std.isObject(target_object) then std.objectFields(target_object) else [];251252      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];253      local both_fields = std.setUnion(target_fields, std.objectFields(patch));254255      {256        [k]:257          if !std.objectHas(patch, k) then258            target_object[k]259          else if !std.objectHas(target_object, k) then260            std.mergePatch(null, patch[k]) tailstrict261          else262            std.mergePatch(target_object[k], patch[k]) tailstrict263        for k in std.setDiff(both_fields, null_fields)264      }265    else266      patch,267268  get(o, f, default=null, inc_hidden=true)::269    if std.objectHasEx(o, f, inc_hidden) then o[f] else default,270271  objectFields(o)::272    std.objectFieldsEx(o, false),273274  objectFieldsAll(o)::275    std.objectFieldsEx(o, true),276277  objectHas(o, f)::278    std.objectHasEx(o, f, false),279280  objectHasAll(o, f)::281    std.objectHasEx(o, f, true),282283  objectValues(o)::284    [o[k] for k in std.objectFields(o)],285286  objectValuesAll(o)::287    [o[k] for k in std.objectFieldsAll(o)],288289  resolvePath(f, r)::290    local arr = std.split(f, '/');291    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),292293  prune(a)::294    local isContent(b) =295      if b == null then296        false297      else if std.isArray(b) then298        std.length(b) > 0299      else if std.isObject(b) then300        std.length(b) > 0301      else302        true;303    if std.isArray(a) then304      [std.prune(x) for x in a if isContent($.prune(x))]305    else if std.isObject(a) then {306      [x]: $.prune(a[x])307      for x in std.objectFields(a)308      if isContent(std.prune(a[x]))309    } else310      a,311312  find(value, arr)::313    if !std.isArray(arr) then314      error 'find second parameter should be an array, got ' + std.type(arr)315    else316      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),317}