git.delta.rocks / jrsonnet / refs/commits / 00b3f93343a7

difftreelog

source

crates/jrsonnet-stdlib/src/std.jsonnet18.8 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),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  any:: $intrinsic(any),44  all:: $intrinsic(all),4546  isString(v):: std.type(v) == 'string',47  isNumber(v):: std.type(v) == 'number',48  isBoolean(v):: std.type(v) == 'boolean',49  isObject(v):: std.type(v) == 'object',50  isArray(v):: std.type(v) == 'array',51  isFunction(v):: std.type(v) == 'function',5253  toString(a)::54    if std.type(a) == 'string' then a else '' + a,5556  substr:: $intrinsic(substr),5758  startsWith(a, b)::59    if std.length(a) < std.length(b) then60      false61    else62      std.substr(a, 0, std.length(b)) == b,6364  endsWith(a, b)::65    if std.length(a) < std.length(b) then66      false67    else68      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6970  lstripChars(str, chars)::71    if std.length(str) > 0 && std.member(chars, str[0]) then72      std.lstripChars(str[1:], chars)73    else74      str,7576  rstripChars(str, chars)::77    local len = std.length(str);78    if len > 0 && std.member(chars, str[len - 1]) then79      std.rstripChars(str[:len - 1], chars)80    else81      str,8283  stripChars(str, chars)::84    std.lstripChars(std.rstripChars(str, chars), chars),8586  stringChars(str)::87    std.makeArray(std.length(str), function(i) str[i]),8889  local parse_nat(str, base) =90    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;91    // These codepoints are in ascending order:92    local zero_code = std.codepoint('0');93    local upper_a_code = std.codepoint('A');94    local lower_a_code = std.codepoint('a');95    local addDigit(aggregate, char) =96      local code = std.codepoint(char);97      local digit = if code >= lower_a_code then98        code - lower_a_code + 1099      else if code >= upper_a_code then100        code - upper_a_code + 10101      else102        code - zero_code;103      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];104      base * aggregate + digit;105    std.foldl(addDigit, std.stringChars(str), 0),106107  parseInt(str)::108    assert std.isString(str) : 'Expected string, got ' + std.type(str);109    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];110    if str[0] == '-' then111      -parse_nat(str[1:], 10)112    else113      parse_nat(str, 10),114115  parseOctal(str)::116    assert std.isString(str) : 'Expected string, got ' + std.type(str);117    assert std.length(str) > 0 : 'Not an octal number: ""';118    parse_nat(str, 8),119120  parseHex(str)::121    assert std.isString(str) : 'Expected string, got ' + std.type(str);122    assert std.length(str) > 0 : 'Not hexadecimal: ""';123    parse_nat(str, 16),124125  split(str, c)::126    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);127    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);128    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);129    std.splitLimit(str, c, -1),130131  splitLimit:: $intrinsic(splitLimit),132133  strReplace:: $intrinsic(strReplace),134135  asciiUpper:: $intrinsic(asciiUpper),136137  asciiLower:: $intrinsic(asciiLower),138139  range:: $intrinsic(range),140141  repeat(what, count)::142    local joiner =143      if std.isString(what) then ''144      else if std.isArray(what) then []145      else error 'std.repeat first argument must be an array or a string';146    std.join(joiner, std.makeArray(count, function(i) what)),147148  slice:: $intrinsic(slice),149150  member:: $intrinsic(member),151152  count:: $intrinsic(count),153154  mod:: $intrinsic(mod),155156  map:: $intrinsic(map),157158  mapWithIndex(func, arr)::159    if !std.isFunction(func) then160      error ('std.mapWithIndex first param must be function, got ' + std.type(func))161    else if !std.isArray(arr) && !std.isString(arr) then162      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))163    else164      std.makeArray(std.length(arr), function(i) func(i, arr[i])),165166  mapWithKey(func, obj)::167    if !std.isFunction(func) then168      error ('std.mapWithKey first param must be function, got ' + std.type(func))169    else if !std.isObject(obj) then170      error ('std.mapWithKey second param must be object, got ' + std.type(obj))171    else172      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },173174  flatMap:: $intrinsic(flatMap),175176  join:: $intrinsic(join),177178  lines(arr)::179    std.join('\n', arr + ['']),180181  deepJoin(arr)::182    if std.isString(arr) then183      arr184    else if std.isArray(arr) then185      std.join('', [std.deepJoin(x) for x in arr])186    else187      error 'Expected string or array, got %s' % std.type(arr),188189190  format:: $intrinsic(format),191192  foldr:: $intrinsic(foldr),193194  foldl:: $intrinsic(foldl),195196  filterMap(filter_func, map_func, arr)::197    if !std.isFunction(filter_func) then198      error ('std.filterMap first param must be function, got ' + std.type(filter_func))199    else if !std.isFunction(map_func) then200      error ('std.filterMap second param must be function, got ' + std.type(map_func))201    else if !std.isArray(arr) then202      error ('std.filterMap third param must be array, got ' + std.type(arr))203    else204      std.map(map_func, std.filter(filter_func, arr)),205206  assertEqual(a, b)::207    if a == b then208      true209    else210      error 'Assertion failed. ' + a + ' != ' + b,211212  abs(n)::213    if !std.isNumber(n) then214      error 'std.abs expected number, got ' + std.type(n)215    else216      if n > 0 then n else -n,217218  sign(n)::219    if !std.isNumber(n) then220      error 'std.sign expected number, got ' + std.type(n)221    else222      if n > 0 then223        1224      else if n < 0 then225        -1226      else 0,227228  max(a, b)::229    if !std.isNumber(a) then230      error 'std.max first param expected number, got ' + std.type(a)231    else if !std.isNumber(b) then232      error 'std.max second param expected number, got ' + std.type(b)233    else234      if a > b then a else b,235236  min(a, b)::237    if !std.isNumber(a) then238      error 'std.min first param expected number, got ' + std.type(a)239    else if !std.isNumber(b) then240      error 'std.min second param expected number, got ' + std.type(b)241    else242      if a < b then a else b,243244  clamp(x, minVal, maxVal)::245    if x < minVal then minVal246    else if x > maxVal then maxVal247    else x,248249  flattenArrays(arrs)::250    std.foldl(function(a, b) a + b, arrs, []),251252  manifestIni(ini)::253    local body_lines(body) =254      std.join([], [255        local value_or_values = body[k];256        if std.isArray(value_or_values) then257          ['%s = %s' % [k, value] for value in value_or_values]258        else259          ['%s = %s' % [k, value_or_values]]260261        for k in std.objectFields(body)262      ]);263264    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),265          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],266          all_sections = [267      section_lines(k, ini.sections[k])268      for k in std.objectFields(ini.sections)269    ];270    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),271272  manifestToml(value):: std.manifestTomlEx(value, '  '),273274  manifestTomlEx(value, indent)::275    local276      escapeStringToml = std.escapeStringJson,277      escapeKeyToml(key) =278        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));279        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),280      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),281      isSection(v) = std.isObject(v) || isTableArray(v),282      renderValue(v, indexedPath, inline, cindent) =283        if v == true then284          'true'285        else if v == false then286          'false'287        else if v == null then288          error 'Tried to manifest "null" at ' + indexedPath289        else if std.isNumber(v) then290          '' + v291        else if std.isString(v) then292          escapeStringToml(v)293        else if std.isFunction(v) then294          error 'Tried to manifest function at ' + indexedPath295        else if std.isArray(v) then296          if std.length(v) == 0 then297            '[]'298          else299            local range = std.range(0, std.length(v) - 1);300            local new_indent = if inline then '' else cindent + indent;301            local separator = if inline then ' ' else '\n';302            local lines = ['[' + separator]303                          + std.join([',' + separator],304                                     [305                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]306                                       for i in range307                                     ])308                          + [separator + (if inline then '' else cindent) + ']'];309            std.join('', lines)310        else if std.isObject(v) then311          local lines = ['{ ']312                        + std.join([', '],313                                   [314                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]315                                     for k in std.objectFields(v)316                                   ])317                        + [' }'];318          std.join('', lines),319      renderTableInternal(v, path, indexedPath, cindent) =320        local kvp = std.flattenArrays([321          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]322          for k in std.objectFields(v)323          if !isSection(v[k])324        ]);325        local sections = [std.join('\n', kvp)] + [326          (327            if std.isObject(v[k]) then328              renderTable(v[k], path + [k], indexedPath + [k], cindent)329            else330              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)331          )332          for k in std.objectFields(v)333          if isSection(v[k])334        ];335        std.join('\n\n', sections),336      renderTable(v, path, indexedPath, cindent) =337        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'338        + (if v == {} then '' else '\n')339        + renderTableInternal(v, path, indexedPath, cindent + indent),340      renderTableArray(v, path, indexedPath, cindent) =341        local range = std.range(0, std.length(v) - 1);342        local sections = [343          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'344           + (if v[i] == {} then '' else '\n')345           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))346          for i in range347        ];348        std.join('\n\n', sections);349    if std.isObject(value) then350      renderTableInternal(value, [], [], '')351    else352      error 'TOML body must be an object. Got ' + std.type(value),353354  escapeStringJson:: $intrinsic(escapeStringJson),355356  escapeStringPython(str)::357    std.escapeStringJson(str),358359  escapeStringBash(str_)::360    local str = std.toString(str_);361    local trans(ch) =362      if ch == "'" then363        "'\"'\"'"364      else365        ch;366    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),367368  escapeStringDollars(str_)::369    local str = std.toString(str_);370    local trans(ch) =371      if ch == '$' then372        '$$'373      else374        ch;375    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),376377  manifestJson(value):: std.manifestJsonEx(value, '    ') tailstrict,378379  manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),380381  manifestJsonEx:: $intrinsic(manifestJsonEx),382383  manifestYamlDoc:: $intrinsic(manifestYamlDoc),384385  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::386    if !std.isArray(value) then387      error 'manifestYamlStream only takes arrays, got ' + std.type(value)388    else389      '---\n' + std.join(390        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]391      ) + if c_document_end then '\n...\n' else '\n',392393394  manifestPython(v)::395    if std.isObject(v) then396      local fields = [397        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]398        for k in std.objectFields(v)399      ];400      '{%s}' % [std.join(', ', fields)]401    else if std.isArray(v) then402      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]403    else if std.isString(v) then404      '%s' % [std.escapeStringPython(v)]405    else if std.isFunction(v) then406      error 'cannot manifest function'407    else if std.isNumber(v) then408      std.toString(v)409    else if v == true then410      'True'411    else if v == false then412      'False'413    else if v == null then414      'None',415416  manifestPythonVars(conf)::417    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];418    std.join('\n', vars + ['']),419420  manifestXmlJsonml(value)::421    if !std.isArray(value) then422      error 'Expected a JSONML value (an array), got %s' % std.type(value)423    else424      local aux(v) =425        if std.isString(v) then426          v427        else428          local tag = v[0];429          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);430          local attrs = if has_attrs then v[1] else {};431          local children = if has_attrs then v[2:] else v[1:];432          local attrs_str =433            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);434          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);435436      aux(value),437438  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',439  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },440441  base64:: $intrinsic(base64),442443  base64DecodeBytes:: $intrinsic(base64DecodeBytes),444445  base64Decode:: $intrinsic(base64Decode),446447  reverse:: $intrinsic(reverse),448449  sort:: $intrinsic(sort),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  get(o, f, default = null, inc_hidden = true)::539    if std.objectHasEx(o, f, inc_hidden) then o[f] else default,540541  objectFields(o)::542    std.objectFieldsEx(o, false),543544  objectFieldsAll(o)::545    std.objectFieldsEx(o, true),546547  objectHas(o, f)::548    std.objectHasEx(o, f, false),549550  objectHasAll(o, f)::551    std.objectHasEx(o, f, true),552553  objectValues(o)::554    [o[k] for k in std.objectFields(o)],555556  objectValuesAll(o)::557    [o[k] for k in std.objectFieldsAll(o)],558559  equals:: $intrinsic(equals),560561  resolvePath(f, r)::562    local arr = std.split(f, '/');563    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),564565  prune(a)::566    local isContent(b) =567      if b == null then568        false569      else if std.isArray(b) then570        std.length(b) > 0571      else if std.isObject(b) then572        std.length(b) > 0573      else574        true;575    if std.isArray(a) then576      [std.prune(x) for x in a if isContent($.prune(x))]577    else if std.isObject(a) then {578      [x]: $.prune(a[x])579      for x in std.objectFields(a)580      if isContent(std.prune(a[x]))581    } else582      a,583584  findSubstr(pat, str)::585    if !std.isString(pat) then586      error 'findSubstr first parameter should be a string, got ' + std.type(pat)587    else if !std.isString(str) then588      error 'findSubstr second parameter should be a string, got ' + std.type(str)589    else590      local pat_len = std.length(pat);591      local str_len = std.length(str);592      if pat_len == 0 || str_len == 0 || pat_len > str_len then593        []594      else595        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),596597  find(value, arr)::598    if !std.isArray(arr) then599      error 'find second parameter should be an array, got ' + std.type(arr)600    else601      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),602}