# jsonkit: work with nested data (maps and lists, like the result of json_parse). # Pure: it needs no permissions, so it can be installed with no `allow` line. # # use jsonkit from "cresset_packages/jsonkit.crs" # data = {"user": {"tags": ["a", "b"]}} # print(jsonkit.dig(data, "user.tags.1", "none")) # b # Follow a path like "user.tags.1" through maps and lists. Numbers pick list items. # Gives back `fallback` when any step is missing, so it never stops your program. fn dig(data, path, fallback = none): current = data for step in path.split("."): if type(current) == "map": if not current.has(step): return fallback current = current[step] elif type(current) == "list": index = to_int(step) if index.is_err(): return fallback i = index.unwrap() if i < 0: i += current.len() if i < 0 or i >= current.len(): return fallback current = current[i] else: return fallback return current # a copy of a map with only the listed keys fn pick(data, keys): out = {} for key in keys: if data.has(key): out[key] = data[key] return out # a copy of a map without the listed keys fn omit(data, keys): out = {} for key in data.keys(): if key not in keys: out[key] = data[key] return out # put two maps together; where both have a key and both values are maps, merge those too. # Otherwise the second map wins. fn deep_merge(a, b): out = {} for key in a.keys(): out[key] = a[key] for key in b.keys(): if out.has(key) and type(out[key]) == "map" and type(b[key]) == "map": out[key] = deep_merge(out[key], b[key]) else: out[key] = b[key] return out # nested maps become one flat map with dotted keys: {"a": {"b": 1}} becomes {"a.b": 1} fn flatten_keys(data, prefix = ""): out = {} for key in data.keys(): name = key if prefix != "": name = "{prefix}.{key}" if type(data[key]) == "map": inner = flatten_keys(data[key], name) for inner_key in inner.keys(): out[inner_key] = inner[inner_key] else: out[name] = data[key] return out # a copy of the data with the value at the path replaced, creating maps along the way when needed fn set_at(data, path, value): parts = path.split(".") out = {} for key in data.keys(): out[key] = data[key] first = parts[0] if parts.len() == 1: out[first] = value return out child = {} if out.has(first) and type(out[first]) == "map": child = out[first] out[first] = set_at(child, parts.drop(1).join("."), value) return out test "dig follows maps and lists": data = {"user": {"name": "Ada", "tags": ["a", "b", "c"]}, "n": 0} assert_eq(dig(data, "user.name"), "Ada") assert_eq(dig(data, "user.tags.1"), "b") assert_eq(dig(data, "user.tags.-1"), "c") assert_eq(dig(data, "n"), 0) test "dig falls back instead of failing": data = {"user": {"tags": ["a"]}} assert_eq(dig(data, "user.missing"), none) assert_eq(dig(data, "user.missing", "?"), "?") assert_eq(dig(data, "user.tags.5", "?"), "?") assert_eq(dig(data, "user.tags.x", "?"), "?") assert_eq(dig(data, "user.tags.0.deeper", "?"), "?") test "pick and omit": data = {"a": 1, "b": 2, "c": 3} assert_eq(pick(data, ["a", "c", "zzz"]), {"a": 1, "c": 3}) assert_eq(omit(data, ["b"]), {"a": 1, "c": 3}) assert_eq(omit({}, ["x"]), {}) test "deep merge keeps both sides": a = {"x": 1, "cfg": {"debug": false, "level": 1}} b = {"y": 2, "cfg": {"level": 5, "extra": true}} assert_eq(deep_merge(a, b), {"x": 1, "cfg": {"debug": false, "level": 5, "extra": true}, "y": 2}) assert_eq(deep_merge({"a": [1]}, {"a": [2]}), {"a": [2]}) test "flatten keys": assert_eq(flatten_keys({"a": {"b": 1, "c": {"d": 2}}, "e": 3}), {"a.b": 1, "a.c.d": 2, "e": 3}) assert_eq(flatten_keys({}), {}) test "set_at builds along the path": assert_eq(set_at({}, "a.b.c", 1), {"a": {"b": {"c": 1}}}) assert_eq(set_at({"a": {"x": 1}}, "a.y", 2), {"a": {"x": 1, "y": 2}}) original = {"a": {"x": 1}} set_at(original, "a.x", 99) assert_eq(original, {"a": {"x": 1}})