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 setUnion(a, b, keyF=id)::200 201 local aux(a, b, i, j, acc) =202 if i >= std.length(a) then203 acc + b[j:]204 else if j >= std.length(b) then205 acc + a[i:]206 else207 local ak = keyF(a[i]);208 local bk = keyF(b[j]);209 if ak == bk then210 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict211 else if ak < bk then212 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict213 else214 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;215 aux(a, b, 0, 0, []),216217 setInter(a, b, keyF=id)::218 local aux(a, b, i, j, acc) =219 if i >= std.length(a) || j >= std.length(b) then220 acc221 else222 if keyF(a[i]) == keyF(b[j]) then223 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict224 else if keyF(a[i]) < keyF(b[j]) then225 aux(a, b, i + 1, j, acc) tailstrict226 else227 aux(a, b, i, j + 1, acc) tailstrict;228 aux(a, b, 0, 0, []) tailstrict,229230 setDiff(a, b, keyF=id)::231 local aux(a, b, i, j, acc) =232 if i >= std.length(a) then233 acc234 else if j >= std.length(b) then235 acc + a[i:]236 else237 if keyF(a[i]) == keyF(b[j]) then238 aux(a, b, i + 1, j + 1, acc) tailstrict239 else if keyF(a[i]) < keyF(b[j]) then240 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict241 else242 aux(a, b, i, j + 1, acc) tailstrict;243 aux(a, b, 0, 0, []) tailstrict,244245 mergePatch(target, patch)::246 if std.isObject(patch) then247 local target_object =248 if std.isObject(target) then target else {};249250 local target_fields =251 if std.isObject(target_object) then std.objectFields(target_object) else [];252253 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];254 local both_fields = std.setUnion(target_fields, std.objectFields(patch));255256 {257 [k]:258 if !std.objectHas(patch, k) then259 target_object[k]260 else if !std.objectHas(target_object, k) then261 std.mergePatch(null, patch[k]) tailstrict262 else263 std.mergePatch(target_object[k], patch[k]) tailstrict264 for k in std.setDiff(both_fields, null_fields)265 }266 else267 patch,268269 get(o, f, default=null, inc_hidden=true)::270 if std.objectHasEx(o, f, inc_hidden) then o[f] else default,271272 objectFields(o)::273 std.objectFieldsEx(o, false),274275 objectFieldsAll(o)::276 std.objectFieldsEx(o, true),277278 objectHas(o, f)::279 std.objectHasEx(o, f, false),280281 objectHasAll(o, f)::282 std.objectHasEx(o, f, true),283284 objectValues(o)::285 [o[k] for k in std.objectFields(o)],286287 objectValuesAll(o)::288 [o[k] for k in std.objectFieldsAll(o)],289290 resolvePath(f, r)::291 local arr = std.split(f, '/');292 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),293294 prune(a)::295 local isContent(b) =296 if b == null then297 false298 else if std.isArray(b) then299 std.length(b) > 0300 else if std.isObject(b) then301 std.length(b) > 0302 else303 true;304 if std.isArray(a) then305 [std.prune(x) for x in a if isContent($.prune(x))]306 else if std.isObject(a) then {307 [x]: $.prune(a[x])308 for x in std.objectFields(a)309 if isContent(std.prune(a[x]))310 } else311 a,312313 find(value, arr)::314 if !std.isArray(arr) then315 error 'find second parameter should be an array, got ' + std.type(arr)316 else317 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),318}