aboutsummaryrefslogtreecommitdiff
path: root/libbuild2
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2024-02-06 16:51:24 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2024-02-07 15:02:57 +0200
commitfcc239ecdbd1467a4ac8b17a353e1b0ae7fd63a0 (patch)
tree93d8ebe044272ddb03ce4dac64ed11f870967c01 /libbuild2
parentac2974380f1986480b3a974d97dd120f81c3e2af (diff)
Map JSON null in subscript/iteration to [null] instead of empty
This in fact feels more natural in the "for consumption" model and also helps with the nested subscript semantics.
Diffstat (limited to 'libbuild2')
-rw-r--r--libbuild2/variable.cxx47
1 files changed, 29 insertions, 18 deletions
diff --git a/libbuild2/variable.cxx b/libbuild2/variable.cxx
index 1785e56..1c899f9 100644
--- a/libbuild2/variable.cxx
+++ b/libbuild2/variable.cxx
@@ -1905,9 +1905,10 @@ namespace build2
return l.as<json_value> ().compare (r.as<json_value> ());
}
- // Return null value if the index/name is out of range.
+ // Return the value as well as the indication of whether the index/name is
+ // in range.
//
- static value
+ static pair<value, bool>
json_subscript_impl (const value& val, value* val_data,
uint64_t i, const string& n, bool index)
{
@@ -1918,13 +1919,12 @@ namespace build2
if (index)
{
if (i >= (jv.type == json_type::array ? jv.array.size () :
- jv.type == json_type::object ? jv.object.size () : 1))
- return value ();
+ jv.type == json_type::object ? jv.object.size () :
+ jv.type == json_type::null ? 0 : 1))
+ return make_pair (value (), false);
switch (jv.type)
{
- case json_type::null:
- return value (); // JSON null has no elements.
case json_type::boolean:
case json_type::signed_number:
case json_type::unsigned_number:
@@ -1963,6 +1963,8 @@ namespace build2
: json_member (m));
break;
}
+ case json_type::null:
+ assert (false);
}
}
else
@@ -1975,7 +1977,7 @@ namespace build2
}));
if (i == jv.object.end ())
- return value ();
+ return make_pair (value (), false);
// Steal the member value if possible.
//
@@ -1996,20 +1998,29 @@ namespace build2
// @@ TODO: split this function into two (index/name) once get rid of this.
//
#if 1
+ value r;
switch (jr.type)
{
- case json_type::null: return value (names {});
- case json_type::boolean: return value (jr.boolean);
- case json_type::signed_number: return value (jr.signed_number);
+ // Seeing that we are reversing for consumption, it feels natural to
+ // reverse JSON null to our [null] rather than empty. This, in
+ // particular, helps nested subscript.
+ //
+#if 0
+ case json_type::null: r = value (names {}); break;
+#else
+ case json_type::null: r = value (); break;
+#endif
+ case json_type::boolean: r = value (jr.boolean); break;
+ case json_type::signed_number: r = value (jr.signed_number); break;
case json_type::unsigned_number:
- case json_type::hexadecimal_number: return value (jr.unsigned_number);
- case json_type::string: return value (move (jr.string));
+ case json_type::hexadecimal_number: r = value (jr.unsigned_number); break;
+ case json_type::string: r = value (move (jr.string)); break;
case json_type::array:
- case json_type::object: break;
+ case json_type::object: r = value (move (jr)); break;
}
#endif
- return value (move (jr));
+ return make_pair (move (r), true);
}
static value
@@ -2081,7 +2092,7 @@ namespace build2
}
value r (jv != nullptr
- ? json_subscript_impl (val, val_data, i, n, index)
+ ? json_subscript_impl (val, val_data, i, n, index).first
: value ());
// Typify null values so that we get called for nested subscripts.
@@ -2100,12 +2111,12 @@ namespace build2
//
for (uint64_t i (0);; ++i)
{
- value e (json_subscript_impl (val, nullptr, i, {}, true));
+ pair<value, bool> e (json_subscript_impl (val, nullptr, i, {}, true));
- if (e.null)
+ if (!e.second)
break;
- f (move (e), i == 0);
+ f (move (e.first), i == 0);
}
}