Adds "toboolean" builtin function for converting strings to booleans (#2098)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Brahm Lower · committed by GitHub · parent 92daac9 · Browse files · 5 files·+67 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+67 added−0 removed
- tests:+22 added−0 removed
- other:+45 added−0 removed
Provenance
- Commit
c350566- Tree
ae3976b- Author
Brahm Lower <bplower@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
92daac9
5 files changed+67 added−0 removed
modified:docs/content/manual/dev/manual.yml+12 added
1475 unmodified lines1476 - program: '.[] | tonumber'1477 input: '[1, "1"]'1478 output: ['1', '1']1479 1480+ - title: "`toboolean`"1481+ body: |1482+1483+ The `toboolean` function parses its input as a boolean. It1484+ will convert correctly-formatted strings to their boolean1485+ equivalent, leave booleans alone, and give an error on all other input.1486+1487+ examples:1488+ - program: '.[] | toboolean'1489+ input: '["true", "false", true, false]'1490+ output: ['true', 'false', 'true', 'false']1491+1492 - title: "`tostring`"1493 body: |1494 1495 The `tostring` function prints its input as a2352 unmodified linesmodified:src/builtin.c+18 added
461 unmodified lines462 }463 return type_error(input, "cannot be parsed as a number");464 }465 466+static jv f_toboolean(jq_state *jq, jv input) {467+ if (jv_get_kind(input) == JV_KIND_TRUE || jv_get_kind(input) == JV_KIND_FALSE) {468+ return input;469+ }470+ if (jv_get_kind(input) == JV_KIND_STRING) {471+ const char *s = jv_string_value(input);472+ if (strcmp(s, "true") == 0) {473+ jv_free(input);474+ return jv_true();475+ } else if (strcmp(s, "false") == 0) {476+ jv_free(input);477+ return jv_false();478+ }479+ }480+ return type_error(input, "cannot be parsed as a boolean");481+}482+483 static jv f_length(jq_state *jq, jv input) {484 if (jv_get_kind(input) == JV_KIND_ARRAY) {485 return jv_number(jv_array_length(input));486 } else if (jv_get_kind(input) == JV_KIND_OBJECT) {1415 unmodified lines1902 #undef BINOP1903 CFUNC(f_dump, "tojson", 1),1904 CFUNC(f_json_parse, "fromjson", 1),1905 CFUNC(f_tonumber, "tonumber", 1),1906+ CFUNC(f_toboolean, "toboolean", 1),1907 CFUNC(f_tostring, "tostring", 1),1908 CFUNC(f_keys, "keys", 1),1909 CFUNC(f_keys_unsorted, "keys_unsorted", 1),1910 CFUNC(f_startswith, "startswith", 2),164 unmodified linesmodified:tests/jq.test+15 added
654 unmodified lines655 1 + tonumber + ("10" | tonumber)656 4657 15658 659+map(toboolean)660+["false","true",false,true]661+[false,true,false,true]662+663+.[] | try toboolean catch .664+[null,0,"tru","truee","fals","falsee",[],{}]665+"null (null) cannot be parsed as a boolean"666+"number (0) cannot be parsed as a boolean"667+"string (\"tru\") cannot be parsed as a boolean"668+"string (\"truee\") cannot be parsed as a boolean"669+"string (\"fals\") cannot be parsed as a boolean"670+"string (\"falsee\") cannot be parsed as a boolean"671+"array ([]) cannot be parsed as a boolean"672+"object ({}) cannot be parsed as a boolean"673+674 [{"a":42},.object,10,.num,false,true,null,"b",[1,4]] | .[] as $x | [$x == .[]]675 {"object": {"a":42}, "num":10.0}676 [true, true, false, false, false, false, false, false, false]677 [true, true, false, false, false, false, false, false, false]1742 unmodified linesmodified:tests/man.test+7 added
442 unmodified lines443 [1, "1"]444 1445 1446 447+.[] | toboolean448+["true", "false", true, false]449+true450+false451+true452+false453+454 .[] | tostring455 [1, "1", [1]]456 "1"457 "1"533 unmodified linesmodified:jq.1.prebuilt+15 added
1555 unmodified lines1556 .fi1557 .1558 .IP "" 01559 .1560+.SS "toboolean"1561+The \fBtoboolean\fR function parses its input as a boolean\. It will convert correctly\-formatted strings to their boolean equivalent, leave booleans alone, and give an error on all other input\.1562+.1563+.IP "" 41564+.1565+.nf1566+1567+jq \'\.[] | toboolean\'1568+ ["true", "false", true, false]1569+=> true, false, true, false1570+.1571+.fi1572+.1573+.IP "" 01574+.1575 .SS "tostring"1576 The \fBtostring\fR function prints its input as a string\. Strings are left unchanged, and all other values are JSON\-encoded\.1577 .1578 .IP "" 42713 unmodified lines