Skip to content

Commit

Permalink
Merge pull request #140 from ihor-hrytskiv/fix/set-obj
Browse files Browse the repository at this point in the history
fix: set_obj
  • Loading branch information
Peefy authored Jun 11, 2024
2 parents b236ac6 + 2711161 commit 9a1a2e7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion jsonpatch/kcl.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "jsonpatch"
version = "0.0.3"
version = "0.0.4"
description = "`jsonpatch` is a module for applying JSON patches (RFC 6902) for KCL values."

3 changes: 2 additions & 1 deletion jsonpatch/main.k
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ _build_patch_obj_n = lambda obj: {str:}, value: any, elements: [str], n: int ->
assert current_obj not in NULL_CONSTANTS, "list value not found for path: ${current_path}"
if n + 1 < len(elements):
next_key = _get_list_index_from_key(elements[n + 1])
next_val = _build_patch_obj_n(obj, value, elements, n + 2)
# List key
if next_key not in NULL_CONSTANTS:
next_val = _build_patch_obj_n(obj, value, elements, n + 2)
idx = next_key
assert 0 <= idx < len(current_obj), "value not found for path: {}".format(current_path + "/" + elements[n + 1])
result = {
"${elements[n]}": [None] * idx + [next_val] + [None] * (len(current_obj) - 1 - idx)
}
# Config key
else:
next_val = _build_patch_obj_n(obj, value, elements, n + 1)
result = {
"${elements[n]}": next_val
}
Expand Down
8 changes: 6 additions & 2 deletions jsonpatch/main_test.k
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ test_json_patch = lambda {
]
}
phoneNumbers0type: str = get_obj(data, "phoneNumbers/0/type")
newObj = set_obj(data, "phoneNumbers/0/type", "school")
addressCity: str = get_obj(data, "address/city")
newType = set_obj(data, "phoneNumbers/0/type", "school")
newState = set_obj(data, "address/state", "WA")
assert phoneNumbers0type == "home"
assert newObj["phoneNumbers"][0]["type"] == "school"
assert addressCity == "New York"
assert newType["phoneNumbers"][0]["type"] == "school"
assert newState["address"]["state"] == "WA"
}

0 comments on commit 9a1a2e7

Please sign in to comment.