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

difftreelog

source

crates/jrsonnet-stdlib/src/std.jsonnet6.9 KiBsourcehistory
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  lstripChars(str, chars)::8    if std.length(str) > 0 && std.member(chars, str[0]) then9      std.lstripChars(str[1:], chars)10    else11      str,1213  rstripChars(str, chars)::14    local len = std.length(str);15    if len > 0 && std.member(chars, str[len - 1]) then16      std.rstripChars(str[:len - 1], chars)17    else18      str,1920  stripChars(str, chars)::21    std.lstripChars(std.rstripChars(str, chars), chars),2223  split(str, c):: std.splitLimit(str, c, -1),2425  mapWithIndex(func, arr)::26    if !std.isFunction(func) then27      error ('std.mapWithIndex first param must be function, got ' + std.type(func))28    else if !std.isArray(arr) && !std.isString(arr) then29      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))30    else31      std.makeArray(std.length(arr), function(i) func(i, arr[i])),3233  mapWithKey(func, obj)::34    if !std.isFunction(func) then35      error ('std.mapWithKey first param must be function, got ' + std.type(func))36    else if !std.isObject(obj) then37      error ('std.mapWithKey second param must be object, got ' + std.type(obj))38    else39      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },4041  lines(arr)::42    std.join('\n', arr + ['']),4344  deepJoin(arr)::45    if std.isString(arr) then46      arr47    else if std.isArray(arr) then48      std.join('', [std.deepJoin(x) for x in arr])49    else50      error 'Expected string or array, got %s' % std.type(arr),5152  assertEqual(a, b)::53    if a == b then54      true55    else56      error 'Assertion failed. ' + a + ' != ' + b,5758  clamp(x, minVal, maxVal)::59    if x < minVal then minVal60    else if x > maxVal then maxVal61    else x,6263  manifestIni(ini)::64    local body_lines(body) =65      std.join([], [66        local value_or_values = body[k];67        if std.isArray(value_or_values) then68          ['%s = %s' % [k, value] for value in value_or_values]69        else70          ['%s = %s' % [k, value_or_values]]7172        for k in std.objectFields(body)73      ]);7475    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),76          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],77          all_sections = [78      section_lines(k, ini.sections[k])79      for k in std.objectFields(ini.sections)80    ];81    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),8283  manifestToml(value):: std.manifestTomlEx(value, '  '),8485  escapeStringPython(str)::86    std.escapeStringJson(str),8788  escapeStringBash(str_)::89    local str = std.toString(str_);90    local trans(ch) =91      if ch == "'" then92        "'\"'\"'"93      else94        ch;95    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),9697  escapeStringDollars(str_)::98    local str = std.toString(str_);99    local trans(ch) =100      if ch == '$' then101        '$$'102      else103        ch;104    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),105106  local xml_escapes = {107    '<': '&lt;',108    '>': '&gt;',109    '&': '&amp;',110    '"': '&quot;',111    "'": '&apos;',112  },113114  escapeStringXML(str_)::115    local str = std.toString(str_);116    std.join('', [std.get(xml_escapes, ch, ch) for ch in std.stringChars(str)]),117118  manifestJson(value):: std.manifestJsonEx(value, '    ') tailstrict,119120  manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),121122  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true, quote_keys=true)::123    if !std.isArray(value) then124      error 'manifestYamlStream only takes arrays, got ' + std.type(value)125    else126      '---\n' + std.join(127        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object, quote_keys) for e in value]128      ) + if c_document_end then '\n...\n' else '\n',129130  manifestPython(v)::131    if std.isObject(v) then132      local fields = [133        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]134        for k in std.objectFields(v)135      ];136      '{%s}' % [std.join(', ', fields)]137    else if std.isArray(v) then138      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]139    else if std.isString(v) then140      '%s' % [std.escapeStringPython(v)]141    else if std.isFunction(v) then142      error 'cannot manifest function'143    else if std.isNumber(v) then144      std.toString(v)145    else if v == true then146      'True'147    else if v == false then148      'False'149    else if v == null then150      'None',151152  manifestPythonVars(conf)::153    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];154    std.join('\n', vars + ['']),155156  manifestXmlJsonml(value)::157    if !std.isArray(value) then158      error 'Expected a JSONML value (an array), got %s' % std.type(value)159    else160      local aux(v) =161        if std.isString(v) then162          v163        else164          local tag = v[0];165          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);166          local attrs = if has_attrs then v[1] else {};167          local children = if has_attrs then v[2:] else v[1:];168          local attrs_str =169            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);170          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);171172      aux(value),173174  mergePatch(target, patch)::175    if std.isObject(patch) then176      local target_object =177        if std.isObject(target) then target else {};178179      local target_fields =180        if std.isObject(target_object) then std.objectFields(target_object) else [];181182      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];183      local both_fields = std.setUnion(target_fields, std.objectFields(patch));184185      {186        [k]:187          if !std.objectHas(patch, k) then188            target_object[k]189          else if !std.objectHas(target_object, k) then190            std.mergePatch(null, patch[k]) tailstrict191          else192            std.mergePatch(target_object[k], patch[k]) tailstrict193        for k in std.setDiff(both_fields, null_fields)194      }195    else196      patch,197198  get(o, f, default=null, inc_hidden=true)::199    if std.objectHasEx(o, f, inc_hidden) then o[f] else default,200201  resolvePath(f, r)::202    local arr = std.split(f, '/');203    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),204205  find(value, arr)::206    if !std.isArray(arr) then207      error 'find second parameter should be an array, got ' + std.type(arr)208    else209      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),210211  // Compat212  __compare_array(arr1, arr2)::213    assert std.isArray(arr1) && std.isArray(arr2);214    std.__compare(arr1, arr2),215  __array_less(arr1, arr2):: std.__compare_array(arr1, arr2) == -1,216  __array_greater(arr1, arr2):: std.__compare_array(arr1, arr2) == 1,217  __array_less_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) <= 0,218  __array_greater_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) >= 0,219}