Skip to content
Myr

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

5 files+67 added−0 removed

modified:docs/content/manual/dev/manual.yml+12 added
1475 unmodified lines
1476 - program: '.[] | tonumber'1477 input: '[1, "1"]'1478 output: ['1', '1']14791480 - title: "`toboolean`"1481 body: |14821483 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.14861487 examples:1488 - program: '.[] | toboolean'1489 input: '["true", "false", true, false]'1490 output: ['true', 'false', 'true', 'false']14911492 - title: "`tostring`"1493 body: |14941495 The `tostring` function prints its input as a
2352 unmodified lines
modified:src/builtin.c+18 added
461 unmodified lines
462 }463 return type_error(input, "cannot be parsed as a number");464}465466static 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}482483static 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 lines
1902#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 lines
modified:tests/jq.test+15 added
654 unmodified lines
6551 + tonumber + ("10" | tonumber)656465715658659map(toboolean)660["false","true",false,true]661[false,true,false,true]662663.[] | 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"673674[{"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 lines
modified:tests/man.test+7 added
442 unmodified lines
443[1, "1"]44414451446447.[] | toboolean448["true", "false", true, false]449true450false451true452false453454.[] | tostring455[1, "1", [1]]456"1"457"1"
533 unmodified lines
modified:jq.1.prebuilt+15 added
1555 unmodified lines
1556.fi1557.1558.IP "" 01559.1560.SS "toboolean"1561The \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.nf15661567jq \'\.[] | toboolean\'1568 ["true", "false", true, false]1569=> true, false, true, false1570.1571.fi1572.1573.IP "" 01574.1575.SS "tostring"1576The \fBtostring\fR function prints its input as a string\. Strings are left unchanged, and all other values are JSON\-encoded\.1577.1578.IP "" 4
2713 unmodified lines