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 split(str, c):: std.splitLimit(str, c, -1),2930 mapWithIndex(func, arr)::31 if !std.isFunction(func) then32 error ('std.mapWithIndex first param must be function, got ' + std.type(func))33 else if !std.isArray(arr) && !std.isString(arr) then34 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))35 else36 std.makeArray(std.length(arr), function(i) func(i, arr[i])),3738 mapWithKey(func, obj)::39 if !std.isFunction(func) then40 error ('std.mapWithKey first param must be function, got ' + std.type(func))41 else if !std.isObject(obj) then42 error ('std.mapWithKey second param must be object, got ' + std.type(obj))43 else44 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },4546 lines(arr)::47 std.join('\n', arr + ['']),4849 deepJoin(arr)::50 if std.isString(arr) then51 arr52 else if std.isArray(arr) then53 std.join('', [std.deepJoin(x) for x in arr])54 else55 error 'Expected string or array, got %s' % std.type(arr),5657 filterMap(filter_func, map_func, arr)::58 if !std.isFunction(filter_func) then59 error ('std.filterMap first param must be function, got ' + std.type(filter_func))60 else if !std.isFunction(map_func) then61 error ('std.filterMap second param must be function, got ' + std.type(map_func))62 else if !std.isArray(arr) then63 error ('std.filterMap third param must be array, got ' + std.type(arr))64 else65 std.map(map_func, std.filter(filter_func, arr)),6667 assertEqual(a, b)::68 if a == b then69 true70 else71 error 'Assertion failed. ' + a + ' != ' + b,7273 clamp(x, minVal, maxVal)::74 if x < minVal then minVal75 else if x > maxVal then maxVal76 else x,7778 flattenArrays(arrs)::79 std.foldl(function(a, b) a + b, arrs, []),8081 manifestIni(ini)::82 local body_lines(body) =83 std.join([], [84 local value_or_values = body[k];85 if std.isArray(value_or_values) then86 ['%s = %s' % [k, value] for value in value_or_values]87 else88 ['%s = %s' % [k, value_or_values]]8990 for k in std.objectFields(body)91 ]);9293 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),94 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],95 all_sections = [96 section_lines(k, ini.sections[k])97 for k in std.objectFields(ini.sections)98 ];99 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),100101 manifestToml(value):: std.manifestTomlEx(value, ' '),102103 manifestTomlEx(value, indent)::104 local105 escapeStringToml = std.escapeStringJson,106 escapeKeyToml(key) =107 local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));108 if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),109 isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),110 isSection(v) = std.isObject(v) || isTableArray(v),111 renderValue(v, indexedPath, inline, cindent) =112 if v == true then113 'true'114 else if v == false then115 'false'116 else if v == null then117 error 'Tried to manifest "null" at ' + indexedPath118 else if std.isNumber(v) then119 '' + v120 else if std.isString(v) then121 escapeStringToml(v)122 else if std.isFunction(v) then123 error 'Tried to manifest function at ' + indexedPath124 else if std.isArray(v) then125 if std.length(v) == 0 then126 '[]'127 else128 local range = std.range(0, std.length(v) - 1);129 local new_indent = if inline then '' else cindent + indent;130 local separator = if inline then ' ' else '\n';131 local lines = ['[' + separator]132 + std.join([',' + separator],133 [134 [new_indent + renderValue(v[i], indexedPath + [i], true, '')]135 for i in range136 ])137 + [separator + (if inline then '' else cindent) + ']'];138 std.join('', lines)139 else if std.isObject(v) then140 local lines = ['{ ']141 + std.join([', '],142 [143 [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]144 for k in std.objectFields(v)145 ])146 + [' }'];147 std.join('', lines),148 renderTableInternal(v, path, indexedPath, cindent) =149 local kvp = std.flattenArrays([150 [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]151 for k in std.objectFields(v)152 if !isSection(v[k])153 ]);154 local sections = [std.join('\n', kvp)] + [155 (156 if std.isObject(v[k]) then157 renderTable(v[k], path + [k], indexedPath + [k], cindent)158 else159 renderTableArray(v[k], path + [k], indexedPath + [k], cindent)160 )161 for k in std.objectFields(v)162 if isSection(v[k])163 ];164 std.join('\n\n', sections),165 renderTable(v, path, indexedPath, cindent) =166 cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'167 + (if v == {} then '' else '\n')168 + renderTableInternal(v, path, indexedPath, cindent + indent),169 renderTableArray(v, path, indexedPath, cindent) =170 local range = std.range(0, std.length(v) - 1);171 local sections = [172 (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'173 + (if v[i] == {} then '' else '\n')174 + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))175 for i in range176 ];177 std.join('\n\n', sections);178 if std.isObject(value) then179 renderTableInternal(value, [], [], '')180 else181 error 'TOML body must be an object. Got ' + std.type(value),182183 escapeStringPython(str)::184 std.escapeStringJson(str),185186 escapeStringBash(str_)::187 local str = std.toString(str_);188 local trans(ch) =189 if ch == "'" then190 "'\"'\"'"191 else192 ch;193 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),194195 escapeStringDollars(str_)::196 local str = std.toString(str_);197 local trans(ch) =198 if ch == '$' then199 '$$'200 else201 ch;202 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),203204 manifestJson(value):: std.manifestJsonEx(value, ' ') tailstrict,205206 manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),207208 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::209 if !std.isArray(value) then210 error 'manifestYamlStream only takes arrays, got ' + std.type(value)211 else212 '---\n' + std.join(213 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]214 ) + if c_document_end then '\n...\n' else '\n',215216217 manifestPython(v)::218 if std.isObject(v) then219 local fields = [220 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]221 for k in std.objectFields(v)222 ];223 '{%s}' % [std.join(', ', fields)]224 else if std.isArray(v) then225 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]226 else if std.isString(v) then227 '%s' % [std.escapeStringPython(v)]228 else if std.isFunction(v) then229 error 'cannot manifest function'230 else if std.isNumber(v) then231 std.toString(v)232 else if v == true then233 'True'234 else if v == false then235 'False'236 else if v == null then237 'None',238239 manifestPythonVars(conf)::240 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];241 std.join('\n', vars + ['']),242243 manifestXmlJsonml(value)::244 if !std.isArray(value) then245 error 'Expected a JSONML value (an array), got %s' % std.type(value)246 else247 local aux(v) =248 if std.isString(v) then249 v250 else251 local tag = v[0];252 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);253 local attrs = if has_attrs then v[1] else {};254 local children = if has_attrs then v[2:] else v[1:];255 local attrs_str =256 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);257 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);258259 aux(value),260261 uniq(arr, keyF=id)::262 local f(a, b) =263 if std.length(a) == 0 then264 [b]265 else if keyF(a[std.length(a) - 1]) == keyF(b) then266 a267 else268 a + [b];269 std.foldl(f, arr, []),270271 set(arr, keyF=id)::272 std.uniq(std.sort(arr, keyF), keyF),273274 setMember(x, arr, keyF=id)::275 276 std.length(std.setInter([x], arr, keyF)) > 0,277278 setUnion(a, b, keyF=id)::279 280 local aux(a, b, i, j, acc) =281 if i >= std.length(a) then282 acc + b[j:]283 else if j >= std.length(b) then284 acc + a[i:]285 else286 local ak = keyF(a[i]);287 local bk = keyF(b[j]);288 if ak == bk then289 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict290 else if ak < bk then291 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict292 else293 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;294 aux(a, b, 0, 0, []),295296 setInter(a, b, keyF=id)::297 local aux(a, b, i, j, acc) =298 if i >= std.length(a) || j >= std.length(b) then299 acc300 else301 if keyF(a[i]) == keyF(b[j]) then302 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict303 else if keyF(a[i]) < keyF(b[j]) then304 aux(a, b, i + 1, j, acc) tailstrict305 else306 aux(a, b, i, j + 1, acc) tailstrict;307 aux(a, b, 0, 0, []) tailstrict,308309 setDiff(a, b, keyF=id)::310 local aux(a, b, i, j, acc) =311 if i >= std.length(a) then312 acc313 else if j >= std.length(b) then314 acc + a[i:]315 else316 if keyF(a[i]) == keyF(b[j]) then317 aux(a, b, i + 1, j + 1, acc) tailstrict318 else if keyF(a[i]) < keyF(b[j]) then319 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict320 else321 aux(a, b, i, j + 1, acc) tailstrict;322 aux(a, b, 0, 0, []) tailstrict,323324 mergePatch(target, patch)::325 if std.isObject(patch) then326 local target_object =327 if std.isObject(target) then target else {};328329 local target_fields =330 if std.isObject(target_object) then std.objectFields(target_object) else [];331332 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];333 local both_fields = std.setUnion(target_fields, std.objectFields(patch));334335 {336 [k]:337 if !std.objectHas(patch, k) then338 target_object[k]339 else if !std.objectHas(target_object, k) then340 std.mergePatch(null, patch[k]) tailstrict341 else342 std.mergePatch(target_object[k], patch[k]) tailstrict343 for k in std.setDiff(both_fields, null_fields)344 }345 else346 patch,347348 get(o, f, default=null, inc_hidden=true)::349 if std.objectHasEx(o, f, inc_hidden) then o[f] else default,350351 objectFields(o)::352 std.objectFieldsEx(o, false),353354 objectFieldsAll(o)::355 std.objectFieldsEx(o, true),356357 objectHas(o, f)::358 std.objectHasEx(o, f, false),359360 objectHasAll(o, f)::361 std.objectHasEx(o, f, true),362363 objectValues(o)::364 [o[k] for k in std.objectFields(o)],365366 objectValuesAll(o)::367 [o[k] for k in std.objectFieldsAll(o)],368369 resolvePath(f, r)::370 local arr = std.split(f, '/');371 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),372373 prune(a)::374 local isContent(b) =375 if b == null then376 false377 else if std.isArray(b) then378 std.length(b) > 0379 else if std.isObject(b) then380 std.length(b) > 0381 else382 true;383 if std.isArray(a) then384 [std.prune(x) for x in a if isContent($.prune(x))]385 else if std.isObject(a) then {386 [x]: $.prune(a[x])387 for x in std.objectFields(a)388 if isContent(std.prune(a[x]))389 } else390 a,391392 find(value, arr)::393 if !std.isArray(arr) then394 error 'find second parameter should be an array, got ' + std.type(arr)395 else396 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),397}