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

difftreelog

source

crates/jrsonnet-stdlib/src/std.jsonnet3.1 KiBsourcehistory
1{2  local std = self,34  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',56  mapWithIndex(func, arr)::7    if !std.isFunction(func) then8      error ('std.mapWithIndex first param must be function, got ' + std.type(func))9    else if !std.isArray(arr) && !std.isString(arr) then10      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))11    else12      std.makeArray(std.length(arr), function(i) func(i, arr[i])),1314  mapWithKey(func, obj)::15    if !std.isFunction(func) then16      error ('std.mapWithKey first param must be function, got ' + std.type(func))17    else if !std.isObject(obj) then18      error ('std.mapWithKey second param must be object, got ' + std.type(obj))19    else20      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },2122  lines(arr)::23    std.join('\n', arr + ['']),2425  deepJoin(arr)::26    if std.isString(arr) then27      arr28    else if std.isArray(arr) then29      std.join('', [std.deepJoin(x) for x in arr])30    else31      error 'Expected string or array, got %s' % std.type(arr),3233  assertEqual(a, b)::34    if a == b then35      true36    else37      error 'Assertion failed. ' + a + ' != ' + b,3839  manifestIni(ini)::40    local body_lines(body) =41      std.join([], [42        local value_or_values = body[k];43        if std.isArray(value_or_values) then44          ['%s = %s' % [k, value] for value in value_or_values]45        else46          ['%s = %s' % [k, value_or_values]]4748        for k in std.objectFields(body)49      ]);5051    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),52          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],53          all_sections = [54      section_lines(k, ini.sections[k])55      for k in std.objectFields(ini.sections)56    ];57    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),5859  mergePatch(target, patch)::60    if std.isObject(patch) then61      local target_object =62        if std.isObject(target) then target else {};6364      local target_fields =65        if std.isObject(target_object) then std.objectFields(target_object) else [];6667      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];68      local both_fields = std.setUnion(target_fields, std.objectFields(patch));6970      {71        [k]:72          if !std.objectHas(patch, k) then73            target_object[k]74          else if !std.objectHas(target_object, k) then75            std.mergePatch(null, patch[k]) tailstrict76          else77            std.mergePatch(target_object[k], patch[k]) tailstrict78        for k in std.setDiff(both_fields, null_fields)79      }80    else81      patch,8283  resolvePath(f, r)::84    local arr = std.split(f, '/');85    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),8687  find(value, arr)::88    if !std.isArray(arr) then89      error 'find second parameter should be an array, got ' + std.type(arr)90    else91      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),92}