aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2024-02-07 13:45:29 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2024-02-07 15:02:57 +0200
commit82fca76bdd4183593702d80c5c41520e8b9d9ad0 (patch)
treec566dc4b4cee05bad487266e4cea963a8f6a1426
parentadc9a34ee759e31dc1f7f9b98629042be3819815 (diff)
Tweak $json.*() function names and semantics
-rw-r--r--libbuild2/functions-json.cxx48
-rw-r--r--tests/function/json/testscript38
2 files changed, 56 insertions, 30 deletions
diff --git a/libbuild2/functions-json.cxx b/libbuild2/functions-json.cxx
index d7265ba..ae5bd77 100644
--- a/libbuild2/functions-json.cxx
+++ b/libbuild2/functions-json.cxx
@@ -14,7 +14,7 @@ using namespace std;
namespace build2
{
static size_t
- find_index (const json_value& a, value v)
+ array_find_index (const json_value& a, value v)
{
if (a.type != json_type::array)
fail << "expected json array instead of " << to_string (a.type)
@@ -97,7 +97,7 @@ namespace build2
fail << "json object member expected instead of " << v.type << endf;
};
- // $size(<json>)
+ // $value_size(<json>)
//
// Return the size of a JSON value.
//
@@ -109,7 +109,7 @@ namespace build2
// string. To get the length call `$string.size()` instead by casting the
// JSON value to the `string` value type.
//
- f["size"] += [] (json_value v) -> size_t
+ f["value_size"] += [] (json_value v) -> size_t
{
// Note: should be consistent with value_traits<json_value>::empty(),
// json_subscript().
@@ -129,24 +129,50 @@ namespace build2
return 1;
};
- // $find(<json-array>, <json>)
+ // $array_size(<json-array>)
//
- // Return true if the JSON array contains the specified JSON value.
+ // Return the number of elements in the JSON array. If the JSON `null`
+ // value is passed instead, assume it is a missing array and return `0`.
//
- f["find"] += [](json_value a, value v)
+ f["array_size"] += [] (json_value a) -> size_t
{
- size_t i (find_index (a, move (v)));
+ if (a.type == json_type::null)
+ return 0;
+
+ if (a.type == json_type::array)
+ return a.array.size ();
+
+ fail << "expected json array instead of " << to_string (a.type) << endf;
+ };
+
+ // $array_find(<json-array>, <json>)
+ //
+ // Return true if the JSON array contains the specified JSON value. If the
+ // JSON `null` value is passed instead, assume it is a missing array and
+ // return `false`.
+ //
+ f["array_find"] += [] (json_value a, value v)
+ {
+ if (a.type == json_type::null)
+ return false;
+
+ size_t i (array_find_index (a, move (v)));
return i != a.array.size (); // We now know it's an array.
};
- // $find_index(<json-array>, <json>)
+ // $array_find_index(<json-array>, <json>)
//
// Return the index of the first element in the JSON array that is equal
- // to the specified JSON value or `$size(json-array)` if none is found.
+ // to the specified JSON value or `$array_size(<json-array>)` if none is
+ // found. If the JSON `null` value is passed instead, assume it is a
+ // missing array and return `0`.
//
- f["find_index"] += [](json_value a, value v)
+ f["array_find_index"] += [](json_value a, value v) -> size_t
{
- return find_index (a, move (v));
+ if (a.type == json_type::null)
+ return 0;
+
+ return array_find_index (a, move (v));
};
#ifndef BUILD2_BOOTSTRAP
diff --git a/tests/function/json/testscript b/tests/function/json/testscript
index fdfc9ab..9f8c2e9 100644
--- a/tests/function/json/testscript
+++ b/tests/function/json/testscript
@@ -58,13 +58,13 @@ EOO
: size
:
$* <<EOI >>EOO
-print $size([json] null)
-print $size([json] true)
-print $size([json] 123)
-print $size([json] abc)
+print $value_size([json] null)
+print $value_size([json] true)
+print $value_size([json] 123)
+print $value_size([json] abc)
print $size([string] ([json] abc)) # @@ Should be 3 (quoted, type hint).
-print $size([json] 1 2 3)
-print $size([json] one@1 two@2 three@3)
+print $value_size([json] 1 2 3)
+print $value_size([json] one@1 two@2 three@3)
EOI
0
1
@@ -79,19 +79,19 @@ EOO
:
$* <<EOI >>EOO
j = [json] 1 ([json] one@1 two@2) 2 true 3 null 4 abc -5 null ([json] 1 2 3)
-print $find_index($j, null)
-print $find_index($j, true)
-print $find_index($j, 3)
-print $find_index($j, 0x4)
-print $find_index($j, -5)
-print $find_index($j, abc)
-print $find_index($j, [json] 1 2 3)
-print $find_index($j, [json] two@2 one@1)
-print $find_index($j, [json] 1 2)
-print $find_index($j, [json] one@1)
-print $find_index($j, [json] one@1 two@2 three@3)
-print $find_index($j, [json] one@1 TWO@3)
-print $find_index($j, [json] one@1 two@3)
+print $array_find_index($j, null)
+print $array_find_index($j, true)
+print $array_find_index($j, 3)
+print $array_find_index($j, 0x4)
+print $array_find_index($j, -5)
+print $array_find_index($j, abc)
+print $array_find_index($j, [json] 1 2 3)
+print $array_find_index($j, [json] two@2 one@1)
+print $array_find_index($j, [json] 1 2)
+print $array_find_index($j, [json] one@1)
+print $array_find_index($j, [json] one@1 two@2 three@3)
+print $array_find_index($j, [json] one@1 TWO@3)
+print $array_find_index($j, [json] one@1 two@3)
EOI
5
3