Skip to content

Commit

Permalink
Merge pull request #143 from ihor-hrytskiv/feat/rfc6901Decode
Browse files Browse the repository at this point in the history
feat: implementing rfc6901Decode
  • Loading branch information
Peefy authored Jun 19, 2024
2 parents 7176fe4 + 4c30c60 commit 11b0ad7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 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.4"
version = "0.0.5"
description = "`jsonpatch` is a module for applying JSON patches (RFC 6902) for KCL values."

16 changes: 11 additions & 5 deletions jsonpatch/main.k
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ get_obj_by_key = lambda obj: any, key: str -> any {
ty = typeof(obj)
# Schema
if ty not in KCL_BUILTIN_TYPES:
result = obj[key]
result = obj[_rfc6901Decode(key)]
# Config
elif ty == "dict":
result = obj[key]
result = obj[_rfc6901Decode(key)]
# List
elif ty == "list":
idx = _get_list_index_from_key(key)
Expand Down Expand Up @@ -51,6 +51,12 @@ _get_obj_n = lambda obj: any, elements: [str], n: int -> any {
result
}

# From http://tools.ietf.org/html/rfc6901#section-4 :
# Transforming '~1' to '/', and then '~0' to '~'.
_rfc6901Decode = lambda string: str {
result = string.replace("~1","/").replace("~0","~")
}

_build_patch_obj_n = lambda obj: {str:}, value: any, elements: [str], n: int -> {str:} {
assert n >= 0
result = Undefined
Expand All @@ -66,18 +72,18 @@ _build_patch_obj_n = lambda obj: {str:}, value: any, elements: [str], n: int ->
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)
"${_rfc6901Decode(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
"${_rfc6901Decode(elements[n])}": next_val
}
# No Next value
else:
result = {
"${elements[n]}" = value
"${_rfc6901Decode(elements[n])}" = value
}
result
}
Expand Down
7 changes: 7 additions & 0 deletions jsonpatch/main_test.k
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
test_json_patch = lambda {
data = {
"labels": {
"org.io/team-name": "core"
}
"firstName": "John",
"lastName": "Doe",
"age": 30,
Expand All @@ -22,10 +25,14 @@ test_json_patch = lambda {
}
phoneNumbers0type: str = get_obj(data, "phoneNumbers/0/type")
addressCity: str = get_obj(data, "address/city")
teamName: str = get_obj(data, "labels/org.io~1team-name")
newType = set_obj(data, "phoneNumbers/0/type", "school")
newState = set_obj(data, "address/state", "WA")
newTeam = set_obj(data, "labels/org.io~1team-name", "security")
assert phoneNumbers0type == "home"
assert addressCity == "New York"
assert teamName == "core"
assert newType["phoneNumbers"][0]["type"] == "school"
assert newState["address"]["state"] == "WA"
assert newTeam["labels"]["org.io/team-name"] == "security"
}

0 comments on commit 11b0ad7

Please sign in to comment.