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 repeat(what, count)::31 local joiner =32 if std.isString(what) then ''33 else if std.isArray(what) then []34 else error 'std.repeat first argument must be an array or a string';35 std.join(joiner, std.makeArray(count, function(i) what)),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 manifestTomlEx(value, indent)::111 local112 escapeStringToml = std.escapeStringJson,113 escapeKeyToml(key) =114 local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));115 if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),116 isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),117 isSection(v) = std.isObject(v) || isTableArray(v),118 renderValue(v, indexedPath, inline, cindent) =119 if v == true then120 'true'121 else if v == false then122 'false'123 else if v == null then124 error 'Tried to manifest "null" at ' + indexedPath125 else if std.isNumber(v) then126 '' + v127 else if std.isString(v) then128 escapeStringToml(v)129 else if std.isFunction(v) then130 error 'Tried to manifest function at ' + indexedPath131 else if std.isArray(v) then132 if std.length(v) == 0 then133 '[]'134 else135 local range = std.range(0, std.length(v) - 1);136 local new_indent = if inline then '' else cindent + indent;137 local separator = if inline then ' ' else '\n';138 local lines = ['[' + separator]139 + std.join([',' + separator],140 [141 [new_indent + renderValue(v[i], indexedPath + [i], true, '')]142 for i in range143 ])144 + [separator + (if inline then '' else cindent) + ']'];145 std.join('', lines)146 else if std.isObject(v) then147 local lines = ['{ ']148 + std.join([', '],149 [150 [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]151 for k in std.objectFields(v)152 ])153 + [' }'];154 std.join('', lines),155 renderTableInternal(v, path, indexedPath, cindent) =156 local kvp = std.flattenArrays([157 [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]158 for k in std.objectFields(v)159 if !isSection(v[k])160 ]);161 local sections = [std.join('\n', kvp)] + [162 (163 if std.isObject(v[k]) then164 renderTable(v[k], path + [k], indexedPath + [k], cindent)165 else166 renderTableArray(v[k], path + [k], indexedPath + [k], cindent)167 )168 for k in std.objectFields(v)169 if isSection(v[k])170 ];171 std.join('\n\n', sections),172 renderTable(v, path, indexedPath, cindent) =173 cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'174 + (if v == {} then '' else '\n')175 + renderTableInternal(v, path, indexedPath, cindent + indent),176 renderTableArray(v, path, indexedPath, cindent) =177 local range = std.range(0, std.length(v) - 1);178 local sections = [179 (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'180 + (if v[i] == {} then '' else '\n')181 + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))182 for i in range183 ];184 std.join('\n\n', sections);185 if std.isObject(value) then186 renderTableInternal(value, [], [], '')187 else188 error 'TOML body must be an object. Got ' + std.type(value),189190 escapeStringPython(str)::191 std.escapeStringJson(str),192193 escapeStringBash(str_)::194 local str = std.toString(str_);195 local trans(ch) =196 if ch == "'" then197 "'\"'\"'"198 else199 ch;200 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),201202 escapeStringDollars(str_)::203 local str = std.toString(str_);204 local trans(ch) =205 if ch == '$' then206 '$$'207 else208 ch;209 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),210211 manifestJson(value):: std.manifestJsonEx(value, ' ') tailstrict,212213 manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),214215 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::216 if !std.isArray(value) then217 error 'manifestYamlStream only takes arrays, got ' + std.type(value)218 else219 '---\n' + std.join(220 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]221 ) + if c_document_end then '\n...\n' else '\n',222223224 manifestPython(v)::225 if std.isObject(v) then226 local fields = [227 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]228 for k in std.objectFields(v)229 ];230 '{%s}' % [std.join(', ', fields)]231 else if std.isArray(v) then232 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]233 else if std.isString(v) then234 '%s' % [std.escapeStringPython(v)]235 else if std.isFunction(v) then236 error 'cannot manifest function'237 else if std.isNumber(v) then238 std.toString(v)239 else if v == true then240 'True'241 else if v == false then242 'False'243 else if v == null then244 'None',245246 manifestPythonVars(conf)::247 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];248 std.join('\n', vars + ['']),249250 manifestXmlJsonml(value)::251 if !std.isArray(value) then252 error 'Expected a JSONML value (an array), got %s' % std.type(value)253 else254 local aux(v) =255 if std.isString(v) then256 v257 else258 local tag = v[0];259 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);260 local attrs = if has_attrs then v[1] else {};261 local children = if has_attrs then v[2:] else v[1:];262 local attrs_str =263 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);264 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);265266 aux(value),267268 uniq(arr, keyF=id)::269 local f(a, b) =270 if std.length(a) == 0 then271 [b]272 else if keyF(a[std.length(a) - 1]) == keyF(b) then273 a274 else275 a + [b];276 std.foldl(f, arr, []),277278 set(arr, keyF=id)::279 std.uniq(std.sort(arr, keyF), keyF),280281 setMember(x, arr, keyF=id)::282 283 std.length(std.setInter([x], arr, keyF)) > 0,284285 setUnion(a, b, keyF=id)::286 287 local aux(a, b, i, j, acc) =288 if i >= std.length(a) then289 acc + b[j:]290 else if j >= std.length(b) then291 acc + a[i:]292 else293 local ak = keyF(a[i]);294 local bk = keyF(b[j]);295 if ak == bk then296 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict297 else if ak < bk then298 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict299 else300 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;301 aux(a, b, 0, 0, []),302303 setInter(a, b, keyF=id)::304 local aux(a, b, i, j, acc) =305 if i >= std.length(a) || j >= std.length(b) then306 acc307 else308 if keyF(a[i]) == keyF(b[j]) then309 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict310 else if keyF(a[i]) < keyF(b[j]) then311 aux(a, b, i + 1, j, acc) tailstrict312 else313 aux(a, b, i, j + 1, acc) tailstrict;314 aux(a, b, 0, 0, []) tailstrict,315316 setDiff(a, b, keyF=id)::317 local aux(a, b, i, j, acc) =318 if i >= std.length(a) then319 acc320 else if j >= std.length(b) then321 acc + a[i:]322 else323 if keyF(a[i]) == keyF(b[j]) then324 aux(a, b, i + 1, j + 1, acc) tailstrict325 else if keyF(a[i]) < keyF(b[j]) then326 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict327 else328 aux(a, b, i, j + 1, acc) tailstrict;329 aux(a, b, 0, 0, []) tailstrict,330331 mergePatch(target, patch)::332 if std.isObject(patch) then333 local target_object =334 if std.isObject(target) then target else {};335336 local target_fields =337 if std.isObject(target_object) then std.objectFields(target_object) else [];338339 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];340 local both_fields = std.setUnion(target_fields, std.objectFields(patch));341342 {343 [k]:344 if !std.objectHas(patch, k) then345 target_object[k]346 else if !std.objectHas(target_object, k) then347 std.mergePatch(null, patch[k]) tailstrict348 else349 std.mergePatch(target_object[k], patch[k]) tailstrict350 for k in std.setDiff(both_fields, null_fields)351 }352 else353 patch,354355 get(o, f, default=null, inc_hidden=true)::356 if std.objectHasEx(o, f, inc_hidden) then o[f] else default,357358 objectFields(o)::359 std.objectFieldsEx(o, false),360361 objectFieldsAll(o)::362 std.objectFieldsEx(o, true),363364 objectHas(o, f)::365 std.objectHasEx(o, f, false),366367 objectHasAll(o, f)::368 std.objectHasEx(o, f, true),369370 objectValues(o)::371 [o[k] for k in std.objectFields(o)],372373 objectValuesAll(o)::374 [o[k] for k in std.objectFieldsAll(o)],375376 resolvePath(f, r)::377 local arr = std.split(f, '/');378 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),379380 prune(a)::381 local isContent(b) =382 if b == null then383 false384 else if std.isArray(b) then385 std.length(b) > 0386 else if std.isObject(b) then387 std.length(b) > 0388 else389 true;390 if std.isArray(a) then391 [std.prune(x) for x in a if isContent($.prune(x))]392 else if std.isObject(a) then {393 [x]: $.prune(a[x])394 for x in std.objectFields(a)395 if isContent(std.prune(a[x]))396 } else397 a,398399 find(value, arr)::400 if !std.isArray(arr) then401 error 'find second parameter should be an array, got ' + std.type(arr)402 else403 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),404}