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 splitLimitR(str, c, maxsplits)::29 if maxsplits == -1 then30 std.splitLimit(str, c, -1)31 else32 local revStr(str) = std.join('', std.reverse(std.stringChars(str)));33 std.map(function(e) revStr(e), std.reverse(std.splitLimit(revStr(str), revStr(c), maxsplits))),3435 split(str, c):: std.splitLimit(str, c, -1),3637 mapWithIndex(func, arr)::38 if !std.isFunction(func) then39 error ('std.mapWithIndex first param must be function, got ' + std.type(func))40 else if !std.isArray(arr) && !std.isString(arr) then41 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))42 else43 std.makeArray(std.length(arr), function(i) func(i, arr[i])),4445 mapWithKey(func, obj)::46 if !std.isFunction(func) then47 error ('std.mapWithKey first param must be function, got ' + std.type(func))48 else if !std.isObject(obj) then49 error ('std.mapWithKey second param must be object, got ' + std.type(obj))50 else51 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },5253 lines(arr)::54 std.join('\n', arr + ['']),5556 deepJoin(arr)::57 if std.isString(arr) then58 arr59 else if std.isArray(arr) then60 std.join('', [std.deepJoin(x) for x in arr])61 else62 error 'Expected string or array, got %s' % std.type(arr),6364 filterMap(filter_func, map_func, arr)::65 if !std.isFunction(filter_func) then66 error ('std.filterMap first param must be function, got ' + std.type(filter_func))67 else if !std.isFunction(map_func) then68 error ('std.filterMap second param must be function, got ' + std.type(map_func))69 else if !std.isArray(arr) then70 error ('std.filterMap third param must be array, got ' + std.type(arr))71 else72 std.map(map_func, std.filter(filter_func, arr)),7374 assertEqual(a, b)::75 if a == b then76 true77 else78 error 'Assertion failed. ' + a + ' != ' + b,7980 clamp(x, minVal, maxVal)::81 if x < minVal then minVal82 else if x > maxVal then maxVal83 else x,8485 flattenArrays(arrs)::86 std.foldl(function(a, b) a + b, arrs, []),8788 manifestIni(ini)::89 local body_lines(body) =90 std.join([], [91 local value_or_values = body[k];92 if std.isArray(value_or_values) then93 ['%s = %s' % [k, value] for value in value_or_values]94 else95 ['%s = %s' % [k, value_or_values]]9697 for k in std.objectFields(body)98 ]);99100 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),101 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],102 all_sections = [103 section_lines(k, ini.sections[k])104 for k in std.objectFields(ini.sections)105 ];106 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),107108 manifestToml(value):: std.manifestTomlEx(value, ' '),109110 escapeStringPython(str)::111 std.escapeStringJson(str),112113 escapeStringBash(str_)::114 local str = std.toString(str_);115 local trans(ch) =116 if ch == "'" then117 "'\"'\"'"118 else119 ch;120 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),121122 escapeStringDollars(str_)::123 local str = std.toString(str_);124 local trans(ch) =125 if ch == '$' then126 '$$'127 else128 ch;129 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),130131 local xml_escapes = {132 '<': '<',133 '>': '>',134 '&': '&',135 '"': '"',136 "'": ''',137 },138139 escapeStringXML(str_)::140 local str = std.toString(str_);141 std.join('', [std.get(xml_escapes, ch, ch) for ch in std.stringChars(str)]),142143 manifestJson(value):: std.manifestJsonEx(value, ' ') tailstrict,144145 manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),146147 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true, quote_keys=true)::148 if !std.isArray(value) then149 error 'manifestYamlStream only takes arrays, got ' + std.type(value)150 else151 '---\n' + std.join(152 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object, quote_keys) for e in value]153 ) + if c_document_end then '\n...\n' else '\n',154155 manifestPython(v)::156 if std.isObject(v) then157 local fields = [158 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]159 for k in std.objectFields(v)160 ];161 '{%s}' % [std.join(', ', fields)]162 else if std.isArray(v) then163 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]164 else if std.isString(v) then165 '%s' % [std.escapeStringPython(v)]166 else if std.isFunction(v) then167 error 'cannot manifest function'168 else if std.isNumber(v) then169 std.toString(v)170 else if v == true then171 'True'172 else if v == false then173 'False'174 else if v == null then175 'None',176177 manifestPythonVars(conf)::178 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];179 std.join('\n', vars + ['']),180181 manifestXmlJsonml(value)::182 if !std.isArray(value) then183 error 'Expected a JSONML value (an array), got %s' % std.type(value)184 else185 local aux(v) =186 if std.isString(v) then187 v188 else189 local tag = v[0];190 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);191 local attrs = if has_attrs then v[1] else {};192 local children = if has_attrs then v[2:] else v[1:];193 local attrs_str =194 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);195 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);196197 aux(value),198199 uniq(arr, keyF=id)::200 local f(a, b) =201 if std.length(a) == 0 then202 [b]203 else if keyF(a[std.length(a) - 1]) == keyF(b) then204 a205 else206 a + [b];207 std.foldl(f, arr, []),208209 set(arr, keyF=id)::210 std.uniq(std.sort(arr, keyF), keyF),211212 setUnion(a, b, keyF=id)::213 214 local aux(a, b, i, j, acc) =215 if i >= std.length(a) then216 acc + b[j:]217 else if j >= std.length(b) then218 acc + a[i:]219 else220 local ak = keyF(a[i]);221 local bk = keyF(b[j]);222 if ak == bk then223 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict224 else if ak < bk then225 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict226 else227 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;228 aux(a, b, 0, 0, []),229230 setInter(a, b, keyF=id)::231 local aux(a, b, i, j, acc) =232 if i >= std.length(a) || j >= std.length(b) then233 acc234 else235 if keyF(a[i]) == keyF(b[j]) then236 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict237 else if keyF(a[i]) < keyF(b[j]) then238 aux(a, b, i + 1, j, acc) tailstrict239 else240 aux(a, b, i, j + 1, acc) tailstrict;241 aux(a, b, 0, 0, []) tailstrict,242243 setDiff(a, b, keyF=id)::244 local aux(a, b, i, j, acc) =245 if i >= std.length(a) then246 acc247 else if j >= std.length(b) then248 acc + a[i:]249 else250 if keyF(a[i]) == keyF(b[j]) then251 aux(a, b, i + 1, j + 1, acc) tailstrict252 else if keyF(a[i]) < keyF(b[j]) then253 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict254 else255 aux(a, b, i, j + 1, acc) tailstrict;256 aux(a, b, 0, 0, []) tailstrict,257258 mergePatch(target, patch)::259 if std.isObject(patch) then260 local target_object =261 if std.isObject(target) then target else {};262263 local target_fields =264 if std.isObject(target_object) then std.objectFields(target_object) else [];265266 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];267 local both_fields = std.setUnion(target_fields, std.objectFields(patch));268269 {270 [k]:271 if !std.objectHas(patch, k) then272 target_object[k]273 else if !std.objectHas(target_object, k) then274 std.mergePatch(null, patch[k]) tailstrict275 else276 std.mergePatch(target_object[k], patch[k]) tailstrict277 for k in std.setDiff(both_fields, null_fields)278 }279 else280 patch,281282 get(o, f, default=null, inc_hidden=true)::283 if std.objectHasEx(o, f, inc_hidden) then o[f] else default,284285 objectFields(o)::286 std.objectFieldsEx(o, false),287288 objectFieldsAll(o)::289 std.objectFieldsEx(o, true),290291 objectHas(o, f)::292 std.objectHasEx(o, f, false),293294 objectHasAll(o, f)::295 std.objectHasEx(o, f, true),296297 objectValues(o)::298 [o[k] for k in std.objectFields(o)],299300 objectValuesAll(o)::301 [o[k] for k in std.objectFieldsAll(o)],302303 resolvePath(f, r)::304 local arr = std.split(f, '/');305 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),306307 prune(a)::308 local isContent(b) =309 if b == null then310 false311 else if std.isArray(b) then312 std.length(b) > 0313 else if std.isObject(b) then314 std.length(b) > 0315 else316 true;317 if std.isArray(a) then318 [std.prune(x) for x in a if isContent($.prune(x))]319 else if std.isObject(a) then {320 [x]: $.prune(a[x])321 for x in std.objectFields(a)322 if isContent(std.prune(a[x]))323 } else324 a,325326 find(value, arr)::327 if !std.isArray(arr) then328 error 'find second parameter should be an array, got ' + std.type(arr)329 else330 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),331}