diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2024-02-20 09:37:50 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2024-02-20 16:01:40 +0200 |
commit | 6ff1cf35f78a24d52603d84eac9349b3d4670c6c (patch) | |
tree | 3c549345e303b25617b70e96800f8d1fbb7a9595 /libbuild2 | |
parent | c2d2a1ac0ac41a068c4bf09f8236a61d576e74f5 (diff) |
Make json value type prepend non-overriding for consistency with map
Diffstat (limited to 'libbuild2')
-rw-r--r-- | libbuild2/json.cxx | 16 | ||||
-rw-r--r-- | libbuild2/json.hxx | 18 | ||||
-rw-r--r-- | libbuild2/variable.hxx | 6 |
3 files changed, 26 insertions, 14 deletions
diff --git a/libbuild2/json.cxx b/libbuild2/json.cxx index 6295b59..4ed1631 100644 --- a/libbuild2/json.cxx +++ b/libbuild2/json.cxx @@ -511,7 +511,7 @@ namespace build2 } void json_value:: - append (json_value&& v) + append (json_value&& v, bool override) { if (type == json_type::null) { @@ -584,10 +584,10 @@ namespace build2 { return m.name == o.name; })); - if (i != object.end ()) - i->value = move (m.value); - else + if (i == object.end ()) object.push_back (move (m)); + else if (override) + i->value = move (m.value); } } @@ -602,7 +602,7 @@ namespace build2 } void json_value:: - prepend (json_value&& v) + prepend (json_value&& v, bool override) { if (type == json_type::null) { @@ -675,10 +675,10 @@ namespace build2 { return m.name == o.name; })); - if (i != object.end ()) - i->value = move (m.value); - else + if (i == object.end ()) object.insert (object.begin (), move (m)); + else if (override) + i->value = move (m.value); } } diff --git a/libbuild2/json.hxx b/libbuild2/json.hxx index 538c87d..96596e3 100644 --- a/libbuild2/json.hxx +++ b/libbuild2/json.hxx @@ -170,16 +170,22 @@ namespace build2 // values are incompatible. Note that for numbers this can also lead to // the change of the value type. // - // Append/prepend to an object overrides existing members. Append/prepend - // an array to an array splices in the array elements rather than adding - // an element of the array type. + // Append/prepend an array to an array splices in the array elements + // rather than adding an element of the array type. + // + // By default, append to an object overrides existing members while + // prepend does not. In a sense, whatever appears last is kept, which is + // consistent with what we expect to happen when specifying the same name + // repeatedly (provided it's not considered invalid) in a text + // representation (e.g., {"a":1,"a":2}). Position-wise, both append and + // prepend retain the positions of existing members with append inserting + // new ones at the end while prepend -- at the beginning. // void - append (json_value&&); + append (json_value&&, bool override = true); void - prepend (json_value&&); - + prepend (json_value&&, bool override = false); // Array element access. // diff --git a/libbuild2/variable.hxx b/libbuild2/variable.hxx index b157806..d754edf 100644 --- a/libbuild2/variable.hxx +++ b/libbuild2/variable.hxx @@ -1237,6 +1237,12 @@ namespace build2 // of what looks like harmless looseness in a few contexts (such as the // $json.member_*() functions). // + // Note that similar to map, JSON object append/+= is overriding while + // prepend/=+ is not. In a sense, whatever appears last (from left to right) + // is kept, which is consistent with what we expect to happen when + // specifying the same name repeatedly (provided it's not considered + // invalid) in a representation (e.g., {"a":1,"a":2}). + // template <> struct LIBBUILD2_SYMEXPORT value_traits<json_value> { |