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

difftreelog

source

crates/jrsonnet-stdlib/src/std.jsonnet22.1 KiBsourcehistory
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),2627  log:: $intrinsic(log),28  pow:: $intrinsic(pow),29  sqrt:: $intrinsic(sqrt),3031  sin:: $intrinsic(sin),32  cos:: $intrinsic(cos),33  tan:: $intrinsic(tan),34  asin:: $intrinsic(asin),35  acos:: $intrinsic(acos),36  atan:: $intrinsic(atan),3738  exp:: $intrinsic(exp),39  mantissa:: $intrinsic(mantissa),40  exponent:: $intrinsic(exponent),4142  isString(v):: std.type(v) == 'string',43  isNumber(v):: std.type(v) == 'number',44  isBoolean(v):: std.type(v) == 'boolean',45  isObject(v):: std.type(v) == 'object',46  isArray(v):: std.type(v) == 'array',47  isFunction(v):: std.type(v) == 'function',4849  toString(a)::50    if std.type(a) == 'string' then a else '' + a,5152  substr:: $intrinsic(substr),5354  startsWith(a, b)::55    if std.length(a) < std.length(b) then56      false57    else58      std.substr(a, 0, std.length(b)) == b,5960  endsWith(a, b)::61    if std.length(a) < std.length(b) then62      false63    else64      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6566  lstripChars(str, chars)::67    if std.length(str) > 0 && std.member(chars, str[0]) then68      std.lstripChars(str[1:], chars)69    else70      str,7172  rstripChars(str, chars)::73    local len = std.length(str);74    if len > 0 && std.member(chars, str[len - 1]) then75      std.rstripChars(str[:len - 1], chars)76    else77      str,7879  stripChars(str, chars)::80    std.lstripChars(std.rstripChars(str, chars), chars),8182  stringChars(str)::83    std.makeArray(std.length(str), function(i) str[i]),8485  local parse_nat(str, base) =86    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;87    // These codepoints are in ascending order:88    local zero_code = std.codepoint('0');89    local upper_a_code = std.codepoint('A');90    local lower_a_code = std.codepoint('a');91    local addDigit(aggregate, char) =92      local code = std.codepoint(char);93      local digit = if code >= lower_a_code then94        code - lower_a_code + 1095      else if code >= upper_a_code then96        code - upper_a_code + 1097      else98        code - zero_code;99      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];100      base * aggregate + digit;101    std.foldl(addDigit, std.stringChars(str), 0),102103  parseInt(str)::104    assert std.isString(str) : 'Expected string, got ' + std.type(str);105    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];106    if str[0] == '-' then107      -parse_nat(str[1:], 10)108    else109      parse_nat(str, 10),110111  parseOctal(str)::112    assert std.isString(str) : 'Expected string, got ' + std.type(str);113    assert std.length(str) > 0 : 'Not an octal number: ""';114    parse_nat(str, 8),115116  parseHex(str)::117    assert std.isString(str) : 'Expected string, got ' + std.type(str);118    assert std.length(str) > 0 : 'Not hexadecimal: ""';119    parse_nat(str, 16),120121  split(str, c)::122    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);123    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);124    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);125    std.splitLimit(str, c, -1),126127  splitLimit:: $intrinsic(splitLimit),128129  strReplace:: $intrinsic(strReplace),130131  asciiUpper(str)::132    local cp = std.codepoint;133    local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then134      std.char(cp(c) - 32)135    else136      c;137    std.join('', std.map(up_letter, std.stringChars(str))),138139  asciiLower(str)::140    local cp = std.codepoint;141    local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then142      std.char(cp(c) + 32)143    else144      c;145    std.join('', std.map(down_letter, std.stringChars(str))),146147  range:: $intrinsic(range),148149  repeat(what, count)::150    local joiner =151      if std.isString(what) then ''152      else if std.isArray(what) then []153      else error 'std.repeat first argument must be an array or a string';154    std.join(joiner, std.makeArray(count, function(i) what)),155156  slice:: $intrinsic(slice),157158  member(arr, x)::159    if std.isArray(arr) then160      std.count(arr, x) > 0161    else if std.isString(arr) then162      std.length(std.findSubstr(x, arr)) > 0163    else error 'std.member first argument must be an array or a string',164165  count(arr, x):: std.length(std.filter(function(v) v == x, arr)),166167  mod:: $intrinsic(mod),168169  map:: $intrinsic(map),170171  mapWithIndex(func, arr)::172    if !std.isFunction(func) then173      error ('std.mapWithIndex first param must be function, got ' + std.type(func))174    else if !std.isArray(arr) && !std.isString(arr) then175      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))176    else177      std.makeArray(std.length(arr), function(i) func(i, arr[i])),178179  mapWithKey(func, obj)::180    if !std.isFunction(func) then181      error ('std.mapWithKey first param must be function, got ' + std.type(func))182    else if !std.isObject(obj) then183      error ('std.mapWithKey second param must be object, got ' + std.type(obj))184    else185      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },186187  flatMap:: $intrinsic(flatMap),188189  join:: $intrinsic(join),190191  lines(arr)::192    std.join('\n', arr + ['']),193194  deepJoin(arr)::195    if std.isString(arr) then196      arr197    else if std.isArray(arr) then198      std.join('', [std.deepJoin(x) for x in arr])199    else200      error 'Expected string or array, got %s' % std.type(arr),201202203  format:: $intrinsic(format),204205  foldr:: $intrinsic(foldr),206207  foldl:: $intrinsic(foldl),208209  filterMap(filter_func, map_func, arr)::210    if !std.isFunction(filter_func) then211      error ('std.filterMap first param must be function, got ' + std.type(filter_func))212    else if !std.isFunction(map_func) then213      error ('std.filterMap second param must be function, got ' + std.type(map_func))214    else if !std.isArray(arr) then215      error ('std.filterMap third param must be array, got ' + std.type(arr))216    else217      std.map(map_func, std.filter(filter_func, arr)),218219  assertEqual(a, b)::220    if a == b then221      true222    else223      error 'Assertion failed. ' + a + ' != ' + b,224225  abs(n)::226    if !std.isNumber(n) then227      error 'std.abs expected number, got ' + std.type(n)228    else229      if n > 0 then n else -n,230231  sign(n)::232    if !std.isNumber(n) then233      error 'std.sign expected number, got ' + std.type(n)234    else235      if n > 0 then236        1237      else if n < 0 then238        -1239      else 0,240241  max(a, b)::242    if !std.isNumber(a) then243      error 'std.max first param expected number, got ' + std.type(a)244    else if !std.isNumber(b) then245      error 'std.max second param expected number, got ' + std.type(b)246    else247      if a > b then a else b,248249  min(a, b)::250    if !std.isNumber(a) then251      error 'std.min first param expected number, got ' + std.type(a)252    else if !std.isNumber(b) then253      error 'std.min second param expected number, got ' + std.type(b)254    else255      if a < b then a else b,256257  clamp(x, minVal, maxVal)::258    if x < minVal then minVal259    else if x > maxVal then maxVal260    else x,261262  flattenArrays(arrs)::263    std.foldl(function(a, b) a + b, arrs, []),264265  manifestIni(ini)::266    local body_lines(body) =267      std.join([], [268        local value_or_values = body[k];269        if std.isArray(value_or_values) then270          ['%s = %s' % [k, value] for value in value_or_values]271        else272          ['%s = %s' % [k, value_or_values]]273274        for k in std.objectFields(body)275      ]);276277    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),278          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],279          all_sections = [280      section_lines(k, ini.sections[k])281      for k in std.objectFields(ini.sections)282    ];283    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),284285  manifestToml(value):: std.manifestTomlEx(value, '  '),286287  manifestTomlEx(value, indent)::288    local289      escapeStringToml = std.escapeStringJson,290      escapeKeyToml(key) =291        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));292        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),293      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),294      isSection(v) = std.isObject(v) || isTableArray(v),295      renderValue(v, indexedPath, inline, cindent) =296        if v == true then297          'true'298        else if v == false then299          'false'300        else if v == null then301          error 'Tried to manifest "null" at ' + indexedPath302        else if std.isNumber(v) then303          '' + v304        else if std.isString(v) then305          escapeStringToml(v)306        else if std.isFunction(v) then307          error 'Tried to manifest function at ' + indexedPath308        else if std.isArray(v) then309          if std.length(v) == 0 then310            '[]'311          else312            local range = std.range(0, std.length(v) - 1);313            local new_indent = if inline then '' else cindent + indent;314            local separator = if inline then ' ' else '\n';315            local lines = ['[' + separator]316                          + std.join([',' + separator],317                                     [318                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]319                                       for i in range320                                     ])321                          + [separator + (if inline then '' else cindent) + ']'];322            std.join('', lines)323        else if std.isObject(v) then324          local lines = ['{ ']325                        + std.join([', '],326                                   [327                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]328                                     for k in std.objectFields(v)329                                   ])330                        + [' }'];331          std.join('', lines),332      renderTableInternal(v, path, indexedPath, cindent) =333        local kvp = std.flattenArrays([334          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]335          for k in std.objectFields(v)336          if !isSection(v[k])337        ]);338        local sections = [std.join('\n', kvp)] + [339          (340            if std.isObject(v[k]) then341              renderTable(v[k], path + [k], indexedPath + [k], cindent)342            else343              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)344          )345          for k in std.objectFields(v)346          if isSection(v[k])347        ];348        std.join('\n\n', sections),349      renderTable(v, path, indexedPath, cindent) =350        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'351        + (if v == {} then '' else '\n')352        + renderTableInternal(v, path, indexedPath, cindent + indent),353      renderTableArray(v, path, indexedPath, cindent) =354        local range = std.range(0, std.length(v) - 1);355        local sections = [356          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'357           + (if v[i] == {} then '' else '\n')358           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))359          for i in range360        ];361        std.join('\n\n', sections);362    if std.isObject(value) then363      renderTableInternal(value, [], [], '')364    else365      error 'TOML body must be an object. Got ' + std.type(value),366367  escapeStringJson:: $intrinsic(escapeStringJson),368369  escapeStringPython(str)::370    std.escapeStringJson(str),371372  escapeStringBash(str_)::373    local str = std.toString(str_);374    local trans(ch) =375      if ch == "'" then376        "'\"'\"'"377      else378        ch;379    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),380381  escapeStringDollars(str_)::382    local str = std.toString(str_);383    local trans(ch) =384      if ch == '$' then385        '$$'386      else387        ch;388    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),389390  manifestJson(value):: std.manifestJsonEx(value, '    '),391392  manifestJsonEx:: $intrinsic(manifestJsonEx),393394  manifestYamlDoc(value, indent_array_in_object=false)::395    local aux(v, path, cindent) =396      if v == true then397        'true'398      else if v == false then399        'false'400      else if v == null then401        'null'402      else if std.isNumber(v) then403        '' + v404      else if std.isString(v) then405        local len = std.length(v);406        if len == 0 then407          '""'408        else if v[len - 1] == '\n' then409          local split = std.split(v, '\n');410          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])411        else412          std.escapeStringJson(v)413      else if std.isFunction(v) then414        error 'Tried to manifest function at ' + path415      else if std.isArray(v) then416        if std.length(v) == 0 then417          '[]'418        else419          local params(value) =420            if std.isArray(value) && std.length(value) > 0 then {421              // While we could avoid the new line, it yields YAML that is422              // hard to read, e.g.:423              // - - - 1424              //     - 2425              //   - - 3426              //     - 4427              new_indent: cindent + '  ',428              space: '\n' + self.new_indent,429            } else if std.isObject(value) && std.length(value) > 0 then {430              new_indent: cindent + '  ',431              // In this case we can start on the same line as the - because the indentation432              // matches up then.  The converse is not true, because fields are not always433              // 1 character long.434              space: ' ',435            } else {436              // In this case, new_indent is only used in the case of multi-line strings.437              new_indent: cindent,438              space: ' ',439            };440          local range = std.range(0, std.length(v) - 1);441          local parts = [442            '-' + param.space + aux(v[i], path + [i], param.new_indent)443            for i in range444            for param in [params(v[i])]445          ];446          std.join('\n' + cindent, parts)447      else if std.isObject(v) then448        if std.length(v) == 0 then449          '{}'450        else451          local params(value) =452            if std.isArray(value) && std.length(value) > 0 then {453              // Not indenting allows e.g.454              // ports:455              // - 80456              // instead of457              // ports:458              //   - 80459              new_indent: if indent_array_in_object then cindent + '  ' else cindent,460              space: '\n' + self.new_indent,461            } else if std.isObject(value) && std.length(value) > 0 then {462              new_indent: cindent + '  ',463              space: '\n' + self.new_indent,464            } else {465              // In this case, new_indent is only used in the case of multi-line strings.466              new_indent: cindent,467              space: ' ',468            };469          local lines = [470            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)471            for k in std.objectFields(v)472            for param in [params(v[k])]473          ];474          std.join('\n' + cindent, lines);475    aux(value, [], ''),476477  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::478    if !std.isArray(value) then479      error 'manifestYamlStream only takes arrays, got ' + std.type(value)480    else481      '---\n' + std.join(482        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]483      ) + if c_document_end then '\n...\n' else '\n',484485486  manifestPython(v)::487    if std.isObject(v) then488      local fields = [489        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]490        for k in std.objectFields(v)491      ];492      '{%s}' % [std.join(', ', fields)]493    else if std.isArray(v) then494      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]495    else if std.isString(v) then496      '%s' % [std.escapeStringPython(v)]497    else if std.isFunction(v) then498      error 'cannot manifest function'499    else if std.isNumber(v) then500      std.toString(v)501    else if v == true then502      'True'503    else if v == false then504      'False'505    else if v == null then506      'None',507508  manifestPythonVars(conf)::509    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];510    std.join('\n', vars + ['']),511512  manifestXmlJsonml(value)::513    if !std.isArray(value) then514      error 'Expected a JSONML value (an array), got %s' % std.type(value)515    else516      local aux(v) =517        if std.isString(v) then518          v519        else520          local tag = v[0];521          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);522          local attrs = if has_attrs then v[1] else {};523          local children = if has_attrs then v[2:] else v[1:];524          local attrs_str =525            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);526          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);527528      aux(value),529530  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',531  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },532533  base64:: $intrinsic(base64),534535  base64DecodeBytes:: $intrinsic(base64DecodeBytes),536537  base64Decode:: $intrinsic(base64Decode),538539  reverse:: $intrinsic(reverse),540541  sortImpl:: $intrinsic(sortImpl),542543  sort(arr, keyF=id)::544    std.sortImpl(arr, keyF),545546  uniq(arr, keyF=id)::547    local f(a, b) =548      if std.length(a) == 0 then549        [b]550      else if keyF(a[std.length(a) - 1]) == keyF(b) then551        a552      else553        a + [b];554    std.foldl(f, arr, []),555556  set(arr, keyF=id)::557    std.uniq(std.sort(arr, keyF), keyF),558559  setMember(x, arr, keyF=id)::560    // TODO(dcunnin): Binary chop for O(log n) complexity561    std.length(std.setInter([x], arr, keyF)) > 0,562563  setUnion(a, b, keyF=id)::564    // NOTE: order matters, values in `a` win565    local aux(a, b, i, j, acc) =566      if i >= std.length(a) then567        acc + b[j:]568      else if j >= std.length(b) then569        acc + a[i:]570      else571        local ak = keyF(a[i]);572        local bk = keyF(b[j]);573        if ak == bk then574          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict575        else if ak < bk then576          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict577        else578          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;579    aux(a, b, 0, 0, []),580581  setInter(a, b, keyF=id)::582    local aux(a, b, i, j, acc) =583      if i >= std.length(a) || j >= std.length(b) then584        acc585      else586        if keyF(a[i]) == keyF(b[j]) then587          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict588        else if keyF(a[i]) < keyF(b[j]) then589          aux(a, b, i + 1, j, acc) tailstrict590        else591          aux(a, b, i, j + 1, acc) tailstrict;592    aux(a, b, 0, 0, []) tailstrict,593594  setDiff(a, b, keyF=id)::595    local aux(a, b, i, j, acc) =596      if i >= std.length(a) then597        acc598      else if j >= std.length(b) then599        acc + a[i:]600      else601        if keyF(a[i]) == keyF(b[j]) then602          aux(a, b, i + 1, j + 1, acc) tailstrict603        else if keyF(a[i]) < keyF(b[j]) then604          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict605        else606          aux(a, b, i, j + 1, acc) tailstrict;607    aux(a, b, 0, 0, []) tailstrict,608609  mergePatch(target, patch)::610    if std.isObject(patch) then611      local target_object =612        if std.isObject(target) then target else {};613614      local target_fields =615        if std.isObject(target_object) then std.objectFields(target_object) else [];616617      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];618      local both_fields = std.setUnion(target_fields, std.objectFields(patch));619620      {621        [k]:622          if !std.objectHas(patch, k) then623            target_object[k]624          else if !std.objectHas(target_object, k) then625            std.mergePatch(null, patch[k]) tailstrict626          else627            std.mergePatch(target_object[k], patch[k]) tailstrict628        for k in std.setDiff(both_fields, null_fields)629      }630    else631      patch,632633  objectFields(o)::634    std.objectFieldsEx(o, false),635636  objectFieldsAll(o)::637    std.objectFieldsEx(o, true),638639  objectHas(o, f)::640    std.objectHasEx(o, f, false),641642  objectHasAll(o, f)::643    std.objectHasEx(o, f, true),644645  objectValues(o)::646    [o[k] for k in std.objectFields(o)],647648  objectValuesAll(o)::649    [o[k] for k in std.objectFieldsAll(o)],650651  equals:: $intrinsic(equals),652653  resolvePath(f, r)::654    local arr = std.split(f, '/');655    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),656657  prune(a)::658    local isContent(b) =659      if b == null then660        false661      else if std.isArray(b) then662        std.length(b) > 0663      else if std.isObject(b) then664        std.length(b) > 0665      else666        true;667    if std.isArray(a) then668      [std.prune(x) for x in a if isContent($.prune(x))]669    else if std.isObject(a) then {670      [x]: $.prune(a[x])671      for x in std.objectFields(a)672      if isContent(std.prune(a[x]))673    } else674      a,675676  findSubstr(pat, str)::677    if !std.isString(pat) then678      error 'findSubstr first parameter should be a string, got ' + std.type(pat)679    else if !std.isString(str) then680      error 'findSubstr second parameter should be a string, got ' + std.type(str)681    else682      local pat_len = std.length(pat);683      local str_len = std.length(str);684      if pat_len == 0 || str_len == 0 || pat_len > str_len then685        []686      else687        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),688689  find(value, arr)::690    if !std.isArray(arr) then691      error 'find second parameter should be an array, got ' + std.type(arr)692    else693      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),694}