aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/functions-json.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2024-02-20 15:40:02 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2024-02-20 16:08:14 +0200
commit0efae7db7b5870246f1e294a5fedaa69e9c90331 (patch)
treefd53e7b4b416a63c8e082dead3ef1d6496fa872a /libbuild2/functions-json.cxx
parent6ff1cf35f78a24d52603d84eac9349b3d4670c6c (diff)
Add json_map and json_set buildfile value types
These expose the std::map<json_value,json_value> and std::set<json_value> types to buildfiles. New functions: $size(<json-set>) $size(<json-map>) $keys(<json-map>) Note that the $keys() function returns the list of map key as a json array. For example: m = [json_map] 2@([json] a@1 b@2) 1@([json] 1 2) s = [json_set] ([json] x@1 y@2) ([json] a@1 b@2) print ($m[2][b]) # 2 print ($s[([json] y@2 x@1)]) # true
Diffstat (limited to 'libbuild2/functions-json.cxx')
-rw-r--r--libbuild2/functions-json.cxx23
1 files changed, 23 insertions, 0 deletions
diff --git a/libbuild2/functions-json.cxx b/libbuild2/functions-json.cxx
index 5715e13..e06d9a5 100644
--- a/libbuild2/functions-json.cxx
+++ b/libbuild2/functions-json.cxx
@@ -308,5 +308,28 @@ namespace build2
}
};
#endif
+
+ // $size(<json-set>)
+ // $size(<json-map>)
+ //
+ // Return the number of elements in the sequence.
+ //
+ f["size"] += [] (set<json_value> v) {return v.size ();};
+ f["size"] += [] (map<json_value, json_value> v) {return v.size ();};
+
+ // $keys(<json-map>)
+ //
+ // Return the list of keys in a json map as a json array.
+ //
+ // Note that the result is sorted in ascending order.
+ //
+ f["keys"] += [](map<json_value, json_value> v)
+ {
+ json_value r (json_type::array);
+ r.array.reserve (v.size ());
+ for (pair<const json_value, json_value>& p: v)
+ r.array.push_back (p.first); // @@ PERF: use C++17 map::extract() to steal.
+ return r;
+ };
}
}