difftreelog
Merge pull request #56 from messense/member-count-builtin
in: master
3 files changed
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -128,6 +128,8 @@
("parseJson".into(), builtin_parse_json),
("asciiUpper".into(), builtin_ascii_upper),
("asciiLower".into(), builtin_ascii_lower),
+ ("member".into(), builtin_member),
+ ("count".into(), builtin_count),
].iter().cloned().collect()
};
}
@@ -854,6 +856,46 @@
})
}
+fn builtin_member(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+ parse_args!(context, "member", args, 2, [
+ 0, arr: ty!((array | string));
+ 1, x: ty!(any);
+ ], {
+ match arr {
+ Val::Str(s) => {
+ let x = x.try_cast_str("x should be string")?;
+ Ok(Val::Bool(!x.is_empty() && s.contains(&*x)))
+ }
+ Val::Arr(a) => {
+ for item in a.iter() {
+ let item = item?;
+ if equals(&item, &x)? {
+ return Ok(Val::Bool(true));
+ }
+ }
+ Ok(Val::Bool(false))
+ }
+ _ => unreachable!(),
+ }
+ })
+}
+
+fn builtin_count(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+ parse_args!(context, "count", args, 2, [
+ 0, arr: ty!(array) => Val::Arr;
+ 1, x: ty!(any);
+ ], {
+ let mut count = 0;
+ for item in arr.iter() {
+ let item = item?;
+ if equals(&item, &x)? {
+ count += 1;
+ }
+ }
+ Ok(Val::Num(count as f64))
+ })
+}
+
pub fn call_builtin(
context: Context,
loc: Option<&ExprLocation>,
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1051,4 +1051,21 @@
assert_eval!(r#"std.assertEqual(std.asciiUpper("aBc😀"), "ABC😀")"#);
assert_eval!(r#"std.assertEqual(std.asciiLower("aBc😀"), "abc😀")"#);
}
+
+ #[test]
+ fn test_member() {
+ assert_eval!(r#"!std.member("", "")"#);
+ assert_eval!(r#"std.member("abc", "a")"#);
+ assert_eval!(r#"!std.member("abc", "d")"#);
+ assert_eval!(r#"!std.member([], "")"#);
+ assert_eval!(r#"std.member(["a", "b", "c"], "a")"#);
+ assert_eval!(r#"!std.member(["a", "b", "c"], "d")"#);
+ }
+
+ #[test]
+ fn test_count() {
+ assert_eval!(r#"std.assertEqual(std.count([], ""), 0)"#);
+ assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);
+ assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);
+ }
}
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth1{2 local std = self,3 local id = std.id,45 # Those functions aren't normally located in stdlib6 length:: $intrinsic(length),7 type:: $intrinsic(type),8 makeArray:: $intrinsic(makeArray),9 codepoint:: $intrinsic(codepoint),10 objectFieldsEx:: $intrinsic(objectFieldsEx),11 objectHasEx:: $intrinsic(objectHasEx),12 primitiveEquals:: $intrinsic(primitiveEquals),13 modulo:: $intrinsic(modulo),14 floor:: $intrinsic(floor),15 ceil:: $intrinsic(ceil),16 extVar:: $intrinsic(extVar),17 native:: $intrinsic(native),18 filter:: $intrinsic(filter),19 char:: $intrinsic(char),20 encodeUTF8:: $intrinsic(encodeUTF8),21 decodeUTF8:: $intrinsic(decodeUTF8),22 md5:: $intrinsic(md5),23 trace:: $intrinsic(trace),24 id:: $intrinsic(id),25 parseJson:: $intrinsic(parseJson),2627 log:: $intrinsic(log),28 pow:: $intrinsic(pow),29 sqrt:: $intrinsic(sqrt),3031 sin:: $intrinsic(sin),32 cos:: $intrinsic(cos),33 tan:: $intrinsic(tan),34 asin:: $intrinsic(asin),35 acos:: $intrinsic(acos),36 atan:: $intrinsic(atan),3738 exp:: $intrinsic(exp),39 mantissa:: $intrinsic(mantissa),40 exponent:: $intrinsic(exponent),4142 isString(v):: std.type(v) == 'string',43 isNumber(v):: std.type(v) == 'number',44 isBoolean(v):: std.type(v) == 'boolean',45 isObject(v):: std.type(v) == 'object',46 isArray(v):: std.type(v) == 'array',47 isFunction(v):: std.type(v) == 'function',4849 toString(a)::50 if std.type(a) == 'string' then a else '' + a,5152 substr:: $intrinsic(substr),5354 startsWith(a, b)::55 if std.length(a) < std.length(b) then56 false57 else58 std.substr(a, 0, std.length(b)) == b,5960 endsWith(a, b)::61 if std.length(a) < std.length(b) then62 false63 else64 std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6566 lstripChars(str, chars)::67 if std.length(str) > 0 && std.member(chars, str[0]) then68 std.lstripChars(str[1:], chars)69 else70 str,7172 rstripChars(str, chars)::73 local len = std.length(str);74 if len > 0 && std.member(chars, str[len - 1]) then75 std.rstripChars(str[:len - 1], chars)76 else77 str,7879 stripChars(str, chars)::80 std.lstripChars(std.rstripChars(str, chars), chars),8182 stringChars(str)::83 std.makeArray(std.length(str), function(i) str[i]),8485 local parse_nat(str, base) =86 assert base > 0 && base <= 16 : 'integer base %d invalid' % base;87 // These codepoints are in ascending order:88 local zero_code = std.codepoint('0');89 local upper_a_code = std.codepoint('A');90 local lower_a_code = std.codepoint('a');91 local addDigit(aggregate, char) =92 local code = std.codepoint(char);93 local digit = if code >= lower_a_code then94 code - lower_a_code + 1095 else if code >= upper_a_code then96 code - upper_a_code + 1097 else98 code - zero_code;99 assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];100 base * aggregate + digit;101 std.foldl(addDigit, std.stringChars(str), 0),102103 parseInt(str)::104 assert std.isString(str) : 'Expected string, got ' + std.type(str);105 assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];106 if str[0] == '-' then107 -parse_nat(str[1:], 10)108 else109 parse_nat(str, 10),110111 parseOctal(str)::112 assert std.isString(str) : 'Expected string, got ' + std.type(str);113 assert std.length(str) > 0 : 'Not an octal number: ""';114 parse_nat(str, 8),115116 parseHex(str)::117 assert std.isString(str) : 'Expected string, got ' + std.type(str);118 assert std.length(str) > 0 : 'Not hexadecimal: ""';119 parse_nat(str, 16),120121 split(str, c)::122 assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);123 assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);124 assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);125 std.splitLimit(str, c, -1),126127 splitLimit:: $intrinsic(splitLimit),128129 strReplace:: $intrinsic(strReplace),130131 asciiUpper:: $intrinsic(asciiUpper),132133 asciiLower:: $intrinsic(asciiLower),134135 range:: $intrinsic(range),136137 repeat(what, count)::138 local joiner =139 if std.isString(what) then ''140 else if std.isArray(what) then []141 else error 'std.repeat first argument must be an array or a string';142 std.join(joiner, std.makeArray(count, function(i) what)),143144 slice:: $intrinsic(slice),145146 member(arr, x)::147 if std.isArray(arr) then148 std.count(arr, x) > 0149 else if std.isString(arr) then150 std.length(std.findSubstr(x, arr)) > 0151 else error 'std.member first argument must be an array or a string',152153 count(arr, x):: std.length(std.filter(function(v) v == x, arr)),154155 mod:: $intrinsic(mod),156157 map:: $intrinsic(map),158159 mapWithIndex(func, arr)::160 if !std.isFunction(func) then161 error ('std.mapWithIndex first param must be function, got ' + std.type(func))162 else if !std.isArray(arr) && !std.isString(arr) then163 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))164 else165 std.makeArray(std.length(arr), function(i) func(i, arr[i])),166167 mapWithKey(func, obj)::168 if !std.isFunction(func) then169 error ('std.mapWithKey first param must be function, got ' + std.type(func))170 else if !std.isObject(obj) then171 error ('std.mapWithKey second param must be object, got ' + std.type(obj))172 else173 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },174175 flatMap:: $intrinsic(flatMap),176177 join:: $intrinsic(join),178179 lines(arr)::180 std.join('\n', arr + ['']),181182 deepJoin(arr)::183 if std.isString(arr) then184 arr185 else if std.isArray(arr) then186 std.join('', [std.deepJoin(x) for x in arr])187 else188 error 'Expected string or array, got %s' % std.type(arr),189190191 format:: $intrinsic(format),192193 foldr:: $intrinsic(foldr),194195 foldl:: $intrinsic(foldl),196197 filterMap(filter_func, map_func, arr)::198 if !std.isFunction(filter_func) then199 error ('std.filterMap first param must be function, got ' + std.type(filter_func))200 else if !std.isFunction(map_func) then201 error ('std.filterMap second param must be function, got ' + std.type(map_func))202 else if !std.isArray(arr) then203 error ('std.filterMap third param must be array, got ' + std.type(arr))204 else205 std.map(map_func, std.filter(filter_func, arr)),206207 assertEqual(a, b)::208 if a == b then209 true210 else211 error 'Assertion failed. ' + a + ' != ' + b,212213 abs(n)::214 if !std.isNumber(n) then215 error 'std.abs expected number, got ' + std.type(n)216 else217 if n > 0 then n else -n,218219 sign(n)::220 if !std.isNumber(n) then221 error 'std.sign expected number, got ' + std.type(n)222 else223 if n > 0 then224 1225 else if n < 0 then226 -1227 else 0,228229 max(a, b)::230 if !std.isNumber(a) then231 error 'std.max first param expected number, got ' + std.type(a)232 else if !std.isNumber(b) then233 error 'std.max second param expected number, got ' + std.type(b)234 else235 if a > b then a else b,236237 min(a, b)::238 if !std.isNumber(a) then239 error 'std.min first param expected number, got ' + std.type(a)240 else if !std.isNumber(b) then241 error 'std.min second param expected number, got ' + std.type(b)242 else243 if a < b then a else b,244245 clamp(x, minVal, maxVal)::246 if x < minVal then minVal247 else if x > maxVal then maxVal248 else x,249250 flattenArrays(arrs)::251 std.foldl(function(a, b) a + b, arrs, []),252253 manifestIni(ini)::254 local body_lines(body) =255 std.join([], [256 local value_or_values = body[k];257 if std.isArray(value_or_values) then258 ['%s = %s' % [k, value] for value in value_or_values]259 else260 ['%s = %s' % [k, value_or_values]]261262 for k in std.objectFields(body)263 ]);264265 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),266 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],267 all_sections = [268 section_lines(k, ini.sections[k])269 for k in std.objectFields(ini.sections)270 ];271 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),272273 manifestToml(value):: std.manifestTomlEx(value, ' '),274275 manifestTomlEx(value, indent)::276 local277 escapeStringToml = std.escapeStringJson,278 escapeKeyToml(key) =279 local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));280 if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),281 isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),282 isSection(v) = std.isObject(v) || isTableArray(v),283 renderValue(v, indexedPath, inline, cindent) =284 if v == true then285 'true'286 else if v == false then287 'false'288 else if v == null then289 error 'Tried to manifest "null" at ' + indexedPath290 else if std.isNumber(v) then291 '' + v292 else if std.isString(v) then293 escapeStringToml(v)294 else if std.isFunction(v) then295 error 'Tried to manifest function at ' + indexedPath296 else if std.isArray(v) then297 if std.length(v) == 0 then298 '[]'299 else300 local range = std.range(0, std.length(v) - 1);301 local new_indent = if inline then '' else cindent + indent;302 local separator = if inline then ' ' else '\n';303 local lines = ['[' + separator]304 + std.join([',' + separator],305 [306 [new_indent + renderValue(v[i], indexedPath + [i], true, '')]307 for i in range308 ])309 + [separator + (if inline then '' else cindent) + ']'];310 std.join('', lines)311 else if std.isObject(v) then312 local lines = ['{ ']313 + std.join([', '],314 [315 [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]316 for k in std.objectFields(v)317 ])318 + [' }'];319 std.join('', lines),320 renderTableInternal(v, path, indexedPath, cindent) =321 local kvp = std.flattenArrays([322 [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]323 for k in std.objectFields(v)324 if !isSection(v[k])325 ]);326 local sections = [std.join('\n', kvp)] + [327 (328 if std.isObject(v[k]) then329 renderTable(v[k], path + [k], indexedPath + [k], cindent)330 else331 renderTableArray(v[k], path + [k], indexedPath + [k], cindent)332 )333 for k in std.objectFields(v)334 if isSection(v[k])335 ];336 std.join('\n\n', sections),337 renderTable(v, path, indexedPath, cindent) =338 cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'339 + (if v == {} then '' else '\n')340 + renderTableInternal(v, path, indexedPath, cindent + indent),341 renderTableArray(v, path, indexedPath, cindent) =342 local range = std.range(0, std.length(v) - 1);343 local sections = [344 (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'345 + (if v[i] == {} then '' else '\n')346 + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))347 for i in range348 ];349 std.join('\n\n', sections);350 if std.isObject(value) then351 renderTableInternal(value, [], [], '')352 else353 error 'TOML body must be an object. Got ' + std.type(value),354355 escapeStringJson:: $intrinsic(escapeStringJson),356357 escapeStringPython(str)::358 std.escapeStringJson(str),359360 escapeStringBash(str_)::361 local str = std.toString(str_);362 local trans(ch) =363 if ch == "'" then364 "'\"'\"'"365 else366 ch;367 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),368369 escapeStringDollars(str_)::370 local str = std.toString(str_);371 local trans(ch) =372 if ch == '$' then373 '$$'374 else375 ch;376 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),377378 manifestJson(value):: std.manifestJsonEx(value, ' '),379380 manifestJsonEx:: $intrinsic(manifestJsonEx),381382 manifestYamlDoc(value, indent_array_in_object=false)::383 local aux(v, path, cindent) =384 if v == true then385 'true'386 else if v == false then387 'false'388 else if v == null then389 'null'390 else if std.isNumber(v) then391 '' + v392 else if std.isString(v) then393 local len = std.length(v);394 if len == 0 then395 '""'396 else if v[len - 1] == '\n' then397 local split = std.split(v, '\n');398 std.join('\n' + cindent + ' ', ['|'] + split[0:std.length(split) - 1])399 else400 std.escapeStringJson(v)401 else if std.isFunction(v) then402 error 'Tried to manifest function at ' + path403 else if std.isArray(v) then404 if std.length(v) == 0 then405 '[]'406 else407 local params(value) =408 if std.isArray(value) && std.length(value) > 0 then {409 // While we could avoid the new line, it yields YAML that is410 // hard to read, e.g.:411 // - - - 1412 // - 2413 // - - 3414 // - 4415 new_indent: cindent + ' ',416 space: '\n' + self.new_indent,417 } else if std.isObject(value) && std.length(value) > 0 then {418 new_indent: cindent + ' ',419 // In this case we can start on the same line as the - because the indentation420 // matches up then. The converse is not true, because fields are not always421 // 1 character long.422 space: ' ',423 } else {424 // In this case, new_indent is only used in the case of multi-line strings.425 new_indent: cindent,426 space: ' ',427 };428 local range = std.range(0, std.length(v) - 1);429 local parts = [430 '-' + param.space + aux(v[i], path + [i], param.new_indent)431 for i in range432 for param in [params(v[i])]433 ];434 std.join('\n' + cindent, parts)435 else if std.isObject(v) then436 if std.length(v) == 0 then437 '{}'438 else439 local params(value) =440 if std.isArray(value) && std.length(value) > 0 then {441 // Not indenting allows e.g.442 // ports:443 // - 80444 // instead of445 // ports:446 // - 80447 new_indent: if indent_array_in_object then cindent + ' ' else cindent,448 space: '\n' + self.new_indent,449 } else if std.isObject(value) && std.length(value) > 0 then {450 new_indent: cindent + ' ',451 space: '\n' + self.new_indent,452 } else {453 // In this case, new_indent is only used in the case of multi-line strings.454 new_indent: cindent,455 space: ' ',456 };457 local lines = [458 std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)459 for k in std.objectFields(v)460 for param in [params(v[k])]461 ];462 std.join('\n' + cindent, lines);463 aux(value, [], ''),464465 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::466 if !std.isArray(value) then467 error 'manifestYamlStream only takes arrays, got ' + std.type(value)468 else469 '---\n' + std.join(470 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]471 ) + if c_document_end then '\n...\n' else '\n',472473474 manifestPython(v)::475 if std.isObject(v) then476 local fields = [477 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]478 for k in std.objectFields(v)479 ];480 '{%s}' % [std.join(', ', fields)]481 else if std.isArray(v) then482 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]483 else if std.isString(v) then484 '%s' % [std.escapeStringPython(v)]485 else if std.isFunction(v) then486 error 'cannot manifest function'487 else if std.isNumber(v) then488 std.toString(v)489 else if v == true then490 'True'491 else if v == false then492 'False'493 else if v == null then494 'None',495496 manifestPythonVars(conf)::497 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];498 std.join('\n', vars + ['']),499500 manifestXmlJsonml(value)::501 if !std.isArray(value) then502 error 'Expected a JSONML value (an array), got %s' % std.type(value)503 else504 local aux(v) =505 if std.isString(v) then506 v507 else508 local tag = v[0];509 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);510 local attrs = if has_attrs then v[1] else {};511 local children = if has_attrs then v[2:] else v[1:];512 local attrs_str =513 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);514 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);515516 aux(value),517518 local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',519 local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },520521 base64:: $intrinsic(base64),522523 base64DecodeBytes:: $intrinsic(base64DecodeBytes),524525 base64Decode:: $intrinsic(base64Decode),526527 reverse:: $intrinsic(reverse),528529 sortImpl:: $intrinsic(sortImpl),530531 sort(arr, keyF=id)::532 std.sortImpl(arr, keyF),533534 uniq(arr, keyF=id)::535 local f(a, b) =536 if std.length(a) == 0 then537 [b]538 else if keyF(a[std.length(a) - 1]) == keyF(b) then539 a540 else541 a + [b];542 std.foldl(f, arr, []),543544 set(arr, keyF=id)::545 std.uniq(std.sort(arr, keyF), keyF),546547 setMember(x, arr, keyF=id)::548 // TODO(dcunnin): Binary chop for O(log n) complexity549 std.length(std.setInter([x], arr, keyF)) > 0,550551 setUnion(a, b, keyF=id)::552 // NOTE: order matters, values in `a` win553 local aux(a, b, i, j, acc) =554 if i >= std.length(a) then555 acc + b[j:]556 else if j >= std.length(b) then557 acc + a[i:]558 else559 local ak = keyF(a[i]);560 local bk = keyF(b[j]);561 if ak == bk then562 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict563 else if ak < bk then564 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict565 else566 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;567 aux(a, b, 0, 0, []),568569 setInter(a, b, keyF=id)::570 local aux(a, b, i, j, acc) =571 if i >= std.length(a) || j >= std.length(b) then572 acc573 else574 if keyF(a[i]) == keyF(b[j]) then575 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict576 else if keyF(a[i]) < keyF(b[j]) then577 aux(a, b, i + 1, j, acc) tailstrict578 else579 aux(a, b, i, j + 1, acc) tailstrict;580 aux(a, b, 0, 0, []) tailstrict,581582 setDiff(a, b, keyF=id)::583 local aux(a, b, i, j, acc) =584 if i >= std.length(a) then585 acc586 else if j >= std.length(b) then587 acc + a[i:]588 else589 if keyF(a[i]) == keyF(b[j]) then590 aux(a, b, i + 1, j + 1, acc) tailstrict591 else if keyF(a[i]) < keyF(b[j]) then592 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict593 else594 aux(a, b, i, j + 1, acc) tailstrict;595 aux(a, b, 0, 0, []) tailstrict,596597 mergePatch(target, patch)::598 if std.isObject(patch) then599 local target_object =600 if std.isObject(target) then target else {};601602 local target_fields =603 if std.isObject(target_object) then std.objectFields(target_object) else [];604605 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];606 local both_fields = std.setUnion(target_fields, std.objectFields(patch));607608 {609 [k]:610 if !std.objectHas(patch, k) then611 target_object[k]612 else if !std.objectHas(target_object, k) then613 std.mergePatch(null, patch[k]) tailstrict614 else615 std.mergePatch(target_object[k], patch[k]) tailstrict616 for k in std.setDiff(both_fields, null_fields)617 }618 else619 patch,620621 objectFields(o)::622 std.objectFieldsEx(o, false),623624 objectFieldsAll(o)::625 std.objectFieldsEx(o, true),626627 objectHas(o, f)::628 std.objectHasEx(o, f, false),629630 objectHasAll(o, f)::631 std.objectHasEx(o, f, true),632633 objectValues(o)::634 [o[k] for k in std.objectFields(o)],635636 objectValuesAll(o)::637 [o[k] for k in std.objectFieldsAll(o)],638639 equals:: $intrinsic(equals),640641 resolvePath(f, r)::642 local arr = std.split(f, '/');643 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),644645 prune(a)::646 local isContent(b) =647 if b == null then648 false649 else if std.isArray(b) then650 std.length(b) > 0651 else if std.isObject(b) then652 std.length(b) > 0653 else654 true;655 if std.isArray(a) then656 [std.prune(x) for x in a if isContent($.prune(x))]657 else if std.isObject(a) then {658 [x]: $.prune(a[x])659 for x in std.objectFields(a)660 if isContent(std.prune(a[x]))661 } else662 a,663664 findSubstr(pat, str)::665 if !std.isString(pat) then666 error 'findSubstr first parameter should be a string, got ' + std.type(pat)667 else if !std.isString(str) then668 error 'findSubstr second parameter should be a string, got ' + std.type(str)669 else670 local pat_len = std.length(pat);671 local str_len = std.length(str);672 if pat_len == 0 || str_len == 0 || pat_len > str_len then673 []674 else675 std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),676677 find(value, arr)::678 if !std.isArray(arr) then679 error 'find second parameter should be an array, got ' + std.type(arr)680 else681 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),682}1{2 local std = self,3 local id = std.id,45 # Those functions aren't normally located in stdlib6 length:: $intrinsic(length),7 type:: $intrinsic(type),8 makeArray:: $intrinsic(makeArray),9 codepoint:: $intrinsic(codepoint),10 objectFieldsEx:: $intrinsic(objectFieldsEx),11 objectHasEx:: $intrinsic(objectHasEx),12 primitiveEquals:: $intrinsic(primitiveEquals),13 modulo:: $intrinsic(modulo),14 floor:: $intrinsic(floor),15 ceil:: $intrinsic(ceil),16 extVar:: $intrinsic(extVar),17 native:: $intrinsic(native),18 filter:: $intrinsic(filter),19 char:: $intrinsic(char),20 encodeUTF8:: $intrinsic(encodeUTF8),21 decodeUTF8:: $intrinsic(decodeUTF8),22 md5:: $intrinsic(md5),23 trace:: $intrinsic(trace),24 id:: $intrinsic(id),25 parseJson:: $intrinsic(parseJson),2627 log:: $intrinsic(log),28 pow:: $intrinsic(pow),29 sqrt:: $intrinsic(sqrt),3031 sin:: $intrinsic(sin),32 cos:: $intrinsic(cos),33 tan:: $intrinsic(tan),34 asin:: $intrinsic(asin),35 acos:: $intrinsic(acos),36 atan:: $intrinsic(atan),3738 exp:: $intrinsic(exp),39 mantissa:: $intrinsic(mantissa),40 exponent:: $intrinsic(exponent),4142 isString(v):: std.type(v) == 'string',43 isNumber(v):: std.type(v) == 'number',44 isBoolean(v):: std.type(v) == 'boolean',45 isObject(v):: std.type(v) == 'object',46 isArray(v):: std.type(v) == 'array',47 isFunction(v):: std.type(v) == 'function',4849 toString(a)::50 if std.type(a) == 'string' then a else '' + a,5152 substr:: $intrinsic(substr),5354 startsWith(a, b)::55 if std.length(a) < std.length(b) then56 false57 else58 std.substr(a, 0, std.length(b)) == b,5960 endsWith(a, b)::61 if std.length(a) < std.length(b) then62 false63 else64 std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6566 lstripChars(str, chars)::67 if std.length(str) > 0 && std.member(chars, str[0]) then68 std.lstripChars(str[1:], chars)69 else70 str,7172 rstripChars(str, chars)::73 local len = std.length(str);74 if len > 0 && std.member(chars, str[len - 1]) then75 std.rstripChars(str[:len - 1], chars)76 else77 str,7879 stripChars(str, chars)::80 std.lstripChars(std.rstripChars(str, chars), chars),8182 stringChars(str)::83 std.makeArray(std.length(str), function(i) str[i]),8485 local parse_nat(str, base) =86 assert base > 0 && base <= 16 : 'integer base %d invalid' % base;87 // These codepoints are in ascending order:88 local zero_code = std.codepoint('0');89 local upper_a_code = std.codepoint('A');90 local lower_a_code = std.codepoint('a');91 local addDigit(aggregate, char) =92 local code = std.codepoint(char);93 local digit = if code >= lower_a_code then94 code - lower_a_code + 1095 else if code >= upper_a_code then96 code - upper_a_code + 1097 else98 code - zero_code;99 assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];100 base * aggregate + digit;101 std.foldl(addDigit, std.stringChars(str), 0),102103 parseInt(str)::104 assert std.isString(str) : 'Expected string, got ' + std.type(str);105 assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];106 if str[0] == '-' then107 -parse_nat(str[1:], 10)108 else109 parse_nat(str, 10),110111 parseOctal(str)::112 assert std.isString(str) : 'Expected string, got ' + std.type(str);113 assert std.length(str) > 0 : 'Not an octal number: ""';114 parse_nat(str, 8),115116 parseHex(str)::117 assert std.isString(str) : 'Expected string, got ' + std.type(str);118 assert std.length(str) > 0 : 'Not hexadecimal: ""';119 parse_nat(str, 16),120121 split(str, c)::122 assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);123 assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);124 assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);125 std.splitLimit(str, c, -1),126127 splitLimit:: $intrinsic(splitLimit),128129 strReplace:: $intrinsic(strReplace),130131 asciiUpper:: $intrinsic(asciiUpper),132133 asciiLower:: $intrinsic(asciiLower),134135 range:: $intrinsic(range),136137 repeat(what, count)::138 local joiner =139 if std.isString(what) then ''140 else if std.isArray(what) then []141 else error 'std.repeat first argument must be an array or a string';142 std.join(joiner, std.makeArray(count, function(i) what)),143144 slice:: $intrinsic(slice),145146 member:: $intrinsic(member),147148 count:: $intrinsic(count),149150 mod:: $intrinsic(mod),151152 map:: $intrinsic(map),153154 mapWithIndex(func, arr)::155 if !std.isFunction(func) then156 error ('std.mapWithIndex first param must be function, got ' + std.type(func))157 else if !std.isArray(arr) && !std.isString(arr) then158 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))159 else160 std.makeArray(std.length(arr), function(i) func(i, arr[i])),161162 mapWithKey(func, obj)::163 if !std.isFunction(func) then164 error ('std.mapWithKey first param must be function, got ' + std.type(func))165 else if !std.isObject(obj) then166 error ('std.mapWithKey second param must be object, got ' + std.type(obj))167 else168 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },169170 flatMap:: $intrinsic(flatMap),171172 join:: $intrinsic(join),173174 lines(arr)::175 std.join('\n', arr + ['']),176177 deepJoin(arr)::178 if std.isString(arr) then179 arr180 else if std.isArray(arr) then181 std.join('', [std.deepJoin(x) for x in arr])182 else183 error 'Expected string or array, got %s' % std.type(arr),184185186 format:: $intrinsic(format),187188 foldr:: $intrinsic(foldr),189190 foldl:: $intrinsic(foldl),191192 filterMap(filter_func, map_func, arr)::193 if !std.isFunction(filter_func) then194 error ('std.filterMap first param must be function, got ' + std.type(filter_func))195 else if !std.isFunction(map_func) then196 error ('std.filterMap second param must be function, got ' + std.type(map_func))197 else if !std.isArray(arr) then198 error ('std.filterMap third param must be array, got ' + std.type(arr))199 else200 std.map(map_func, std.filter(filter_func, arr)),201202 assertEqual(a, b)::203 if a == b then204 true205 else206 error 'Assertion failed. ' + a + ' != ' + b,207208 abs(n)::209 if !std.isNumber(n) then210 error 'std.abs expected number, got ' + std.type(n)211 else212 if n > 0 then n else -n,213214 sign(n)::215 if !std.isNumber(n) then216 error 'std.sign expected number, got ' + std.type(n)217 else218 if n > 0 then219 1220 else if n < 0 then221 -1222 else 0,223224 max(a, b)::225 if !std.isNumber(a) then226 error 'std.max first param expected number, got ' + std.type(a)227 else if !std.isNumber(b) then228 error 'std.max second param expected number, got ' + std.type(b)229 else230 if a > b then a else b,231232 min(a, b)::233 if !std.isNumber(a) then234 error 'std.min first param expected number, got ' + std.type(a)235 else if !std.isNumber(b) then236 error 'std.min second param expected number, got ' + std.type(b)237 else238 if a < b then a else b,239240 clamp(x, minVal, maxVal)::241 if x < minVal then minVal242 else if x > maxVal then maxVal243 else x,244245 flattenArrays(arrs)::246 std.foldl(function(a, b) a + b, arrs, []),247248 manifestIni(ini)::249 local body_lines(body) =250 std.join([], [251 local value_or_values = body[k];252 if std.isArray(value_or_values) then253 ['%s = %s' % [k, value] for value in value_or_values]254 else255 ['%s = %s' % [k, value_or_values]]256257 for k in std.objectFields(body)258 ]);259260 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),261 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],262 all_sections = [263 section_lines(k, ini.sections[k])264 for k in std.objectFields(ini.sections)265 ];266 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),267268 manifestToml(value):: std.manifestTomlEx(value, ' '),269270 manifestTomlEx(value, indent)::271 local272 escapeStringToml = std.escapeStringJson,273 escapeKeyToml(key) =274 local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));275 if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),276 isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),277 isSection(v) = std.isObject(v) || isTableArray(v),278 renderValue(v, indexedPath, inline, cindent) =279 if v == true then280 'true'281 else if v == false then282 'false'283 else if v == null then284 error 'Tried to manifest "null" at ' + indexedPath285 else if std.isNumber(v) then286 '' + v287 else if std.isString(v) then288 escapeStringToml(v)289 else if std.isFunction(v) then290 error 'Tried to manifest function at ' + indexedPath291 else if std.isArray(v) then292 if std.length(v) == 0 then293 '[]'294 else295 local range = std.range(0, std.length(v) - 1);296 local new_indent = if inline then '' else cindent + indent;297 local separator = if inline then ' ' else '\n';298 local lines = ['[' + separator]299 + std.join([',' + separator],300 [301 [new_indent + renderValue(v[i], indexedPath + [i], true, '')]302 for i in range303 ])304 + [separator + (if inline then '' else cindent) + ']'];305 std.join('', lines)306 else if std.isObject(v) then307 local lines = ['{ ']308 + std.join([', '],309 [310 [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]311 for k in std.objectFields(v)312 ])313 + [' }'];314 std.join('', lines),315 renderTableInternal(v, path, indexedPath, cindent) =316 local kvp = std.flattenArrays([317 [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]318 for k in std.objectFields(v)319 if !isSection(v[k])320 ]);321 local sections = [std.join('\n', kvp)] + [322 (323 if std.isObject(v[k]) then324 renderTable(v[k], path + [k], indexedPath + [k], cindent)325 else326 renderTableArray(v[k], path + [k], indexedPath + [k], cindent)327 )328 for k in std.objectFields(v)329 if isSection(v[k])330 ];331 std.join('\n\n', sections),332 renderTable(v, path, indexedPath, cindent) =333 cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'334 + (if v == {} then '' else '\n')335 + renderTableInternal(v, path, indexedPath, cindent + indent),336 renderTableArray(v, path, indexedPath, cindent) =337 local range = std.range(0, std.length(v) - 1);338 local sections = [339 (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'340 + (if v[i] == {} then '' else '\n')341 + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))342 for i in range343 ];344 std.join('\n\n', sections);345 if std.isObject(value) then346 renderTableInternal(value, [], [], '')347 else348 error 'TOML body must be an object. Got ' + std.type(value),349350 escapeStringJson:: $intrinsic(escapeStringJson),351352 escapeStringPython(str)::353 std.escapeStringJson(str),354355 escapeStringBash(str_)::356 local str = std.toString(str_);357 local trans(ch) =358 if ch == "'" then359 "'\"'\"'"360 else361 ch;362 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),363364 escapeStringDollars(str_)::365 local str = std.toString(str_);366 local trans(ch) =367 if ch == '$' then368 '$$'369 else370 ch;371 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),372373 manifestJson(value):: std.manifestJsonEx(value, ' '),374375 manifestJsonEx:: $intrinsic(manifestJsonEx),376377 manifestYamlDoc(value, indent_array_in_object=false)::378 local aux(v, path, cindent) =379 if v == true then380 'true'381 else if v == false then382 'false'383 else if v == null then384 'null'385 else if std.isNumber(v) then386 '' + v387 else if std.isString(v) then388 local len = std.length(v);389 if len == 0 then390 '""'391 else if v[len - 1] == '\n' then392 local split = std.split(v, '\n');393 std.join('\n' + cindent + ' ', ['|'] + split[0:std.length(split) - 1])394 else395 std.escapeStringJson(v)396 else if std.isFunction(v) then397 error 'Tried to manifest function at ' + path398 else if std.isArray(v) then399 if std.length(v) == 0 then400 '[]'401 else402 local params(value) =403 if std.isArray(value) && std.length(value) > 0 then {404 // While we could avoid the new line, it yields YAML that is405 // hard to read, e.g.:406 // - - - 1407 // - 2408 // - - 3409 // - 4410 new_indent: cindent + ' ',411 space: '\n' + self.new_indent,412 } else if std.isObject(value) && std.length(value) > 0 then {413 new_indent: cindent + ' ',414 // In this case we can start on the same line as the - because the indentation415 // matches up then. The converse is not true, because fields are not always416 // 1 character long.417 space: ' ',418 } else {419 // In this case, new_indent is only used in the case of multi-line strings.420 new_indent: cindent,421 space: ' ',422 };423 local range = std.range(0, std.length(v) - 1);424 local parts = [425 '-' + param.space + aux(v[i], path + [i], param.new_indent)426 for i in range427 for param in [params(v[i])]428 ];429 std.join('\n' + cindent, parts)430 else if std.isObject(v) then431 if std.length(v) == 0 then432 '{}'433 else434 local params(value) =435 if std.isArray(value) && std.length(value) > 0 then {436 // Not indenting allows e.g.437 // ports:438 // - 80439 // instead of440 // ports:441 // - 80442 new_indent: if indent_array_in_object then cindent + ' ' else cindent,443 space: '\n' + self.new_indent,444 } else if std.isObject(value) && std.length(value) > 0 then {445 new_indent: cindent + ' ',446 space: '\n' + self.new_indent,447 } else {448 // In this case, new_indent is only used in the case of multi-line strings.449 new_indent: cindent,450 space: ' ',451 };452 local lines = [453 std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)454 for k in std.objectFields(v)455 for param in [params(v[k])]456 ];457 std.join('\n' + cindent, lines);458 aux(value, [], ''),459460 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::461 if !std.isArray(value) then462 error 'manifestYamlStream only takes arrays, got ' + std.type(value)463 else464 '---\n' + std.join(465 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]466 ) + if c_document_end then '\n...\n' else '\n',467468469 manifestPython(v)::470 if std.isObject(v) then471 local fields = [472 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]473 for k in std.objectFields(v)474 ];475 '{%s}' % [std.join(', ', fields)]476 else if std.isArray(v) then477 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]478 else if std.isString(v) then479 '%s' % [std.escapeStringPython(v)]480 else if std.isFunction(v) then481 error 'cannot manifest function'482 else if std.isNumber(v) then483 std.toString(v)484 else if v == true then485 'True'486 else if v == false then487 'False'488 else if v == null then489 'None',490491 manifestPythonVars(conf)::492 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];493 std.join('\n', vars + ['']),494495 manifestXmlJsonml(value)::496 if !std.isArray(value) then497 error 'Expected a JSONML value (an array), got %s' % std.type(value)498 else499 local aux(v) =500 if std.isString(v) then501 v502 else503 local tag = v[0];504 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);505 local attrs = if has_attrs then v[1] else {};506 local children = if has_attrs then v[2:] else v[1:];507 local attrs_str =508 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);509 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);510511 aux(value),512513 local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',514 local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },515516 base64:: $intrinsic(base64),517518 base64DecodeBytes:: $intrinsic(base64DecodeBytes),519520 base64Decode:: $intrinsic(base64Decode),521522 reverse:: $intrinsic(reverse),523524 sortImpl:: $intrinsic(sortImpl),525526 sort(arr, keyF=id)::527 std.sortImpl(arr, keyF),528529 uniq(arr, keyF=id)::530 local f(a, b) =531 if std.length(a) == 0 then532 [b]533 else if keyF(a[std.length(a) - 1]) == keyF(b) then534 a535 else536 a + [b];537 std.foldl(f, arr, []),538539 set(arr, keyF=id)::540 std.uniq(std.sort(arr, keyF), keyF),541542 setMember(x, arr, keyF=id)::543 // TODO(dcunnin): Binary chop for O(log n) complexity544 std.length(std.setInter([x], arr, keyF)) > 0,545546 setUnion(a, b, keyF=id)::547 // NOTE: order matters, values in `a` win548 local aux(a, b, i, j, acc) =549 if i >= std.length(a) then550 acc + b[j:]551 else if j >= std.length(b) then552 acc + a[i:]553 else554 local ak = keyF(a[i]);555 local bk = keyF(b[j]);556 if ak == bk then557 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict558 else if ak < bk then559 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict560 else561 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;562 aux(a, b, 0, 0, []),563564 setInter(a, b, keyF=id)::565 local aux(a, b, i, j, acc) =566 if i >= std.length(a) || j >= std.length(b) then567 acc568 else569 if keyF(a[i]) == keyF(b[j]) then570 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict571 else if keyF(a[i]) < keyF(b[j]) then572 aux(a, b, i + 1, j, acc) tailstrict573 else574 aux(a, b, i, j + 1, acc) tailstrict;575 aux(a, b, 0, 0, []) tailstrict,576577 setDiff(a, b, keyF=id)::578 local aux(a, b, i, j, acc) =579 if i >= std.length(a) then580 acc581 else if j >= std.length(b) then582 acc + a[i:]583 else584 if keyF(a[i]) == keyF(b[j]) then585 aux(a, b, i + 1, j + 1, acc) tailstrict586 else if keyF(a[i]) < keyF(b[j]) then587 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict588 else589 aux(a, b, i, j + 1, acc) tailstrict;590 aux(a, b, 0, 0, []) tailstrict,591592 mergePatch(target, patch)::593 if std.isObject(patch) then594 local target_object =595 if std.isObject(target) then target else {};596597 local target_fields =598 if std.isObject(target_object) then std.objectFields(target_object) else [];599600 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];601 local both_fields = std.setUnion(target_fields, std.objectFields(patch));602603 {604 [k]:605 if !std.objectHas(patch, k) then606 target_object[k]607 else if !std.objectHas(target_object, k) then608 std.mergePatch(null, patch[k]) tailstrict609 else610 std.mergePatch(target_object[k], patch[k]) tailstrict611 for k in std.setDiff(both_fields, null_fields)612 }613 else614 patch,615616 objectFields(o)::617 std.objectFieldsEx(o, false),618619 objectFieldsAll(o)::620 std.objectFieldsEx(o, true),621622 objectHas(o, f)::623 std.objectHasEx(o, f, false),624625 objectHasAll(o, f)::626 std.objectHasEx(o, f, true),627628 objectValues(o)::629 [o[k] for k in std.objectFields(o)],630631 objectValuesAll(o)::632 [o[k] for k in std.objectFieldsAll(o)],633634 equals:: $intrinsic(equals),635636 resolvePath(f, r)::637 local arr = std.split(f, '/');638 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),639640 prune(a)::641 local isContent(b) =642 if b == null then643 false644 else if std.isArray(b) then645 std.length(b) > 0646 else if std.isObject(b) then647 std.length(b) > 0648 else649 true;650 if std.isArray(a) then651 [std.prune(x) for x in a if isContent($.prune(x))]652 else if std.isObject(a) then {653 [x]: $.prune(a[x])654 for x in std.objectFields(a)655 if isContent(std.prune(a[x]))656 } else657 a,658659 findSubstr(pat, str)::660 if !std.isString(pat) then661 error 'findSubstr first parameter should be a string, got ' + std.type(pat)662 else if !std.isString(str) then663 error 'findSubstr second parameter should be a string, got ' + std.type(str)664 else665 local pat_len = std.length(pat);666 local str_len = std.length(str);667 if pat_len == 0 || str_len == 0 || pat_len > str_len then668 []669 else670 std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),671672 find(value, arr)::673 if !std.isArray(arr) then674 error 'find second parameter should be an array, got ' + std.type(arr)675 else676 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),677}