Skip to content

Commit 4c30c60

Browse files
committed
feat: implementing rfc6901Decode
Signed-off-by: Ihor Hrytskiv <[email protected]>
1 parent 7176fe4 commit 4c30c60

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

jsonpatch/kcl.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
22
name = "jsonpatch"
3-
version = "0.0.4"
3+
version = "0.0.5"
44
description = "`jsonpatch` is a module for applying JSON patches (RFC 6902) for KCL values."
55

jsonpatch/main.k

+11-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ get_obj_by_key = lambda obj: any, key: str -> any {
1919
ty = typeof(obj)
2020
# Schema
2121
if ty not in KCL_BUILTIN_TYPES:
22-
result = obj[key]
22+
result = obj[_rfc6901Decode(key)]
2323
# Config
2424
elif ty == "dict":
25-
result = obj[key]
25+
result = obj[_rfc6901Decode(key)]
2626
# List
2727
elif ty == "list":
2828
idx = _get_list_index_from_key(key)
@@ -51,6 +51,12 @@ _get_obj_n = lambda obj: any, elements: [str], n: int -> any {
5151
result
5252
}
5353

54+
# From http://tools.ietf.org/html/rfc6901#section-4 :
55+
# Transforming '~1' to '/', and then '~0' to '~'.
56+
_rfc6901Decode = lambda string: str {
57+
result = string.replace("~1","/").replace("~0","~")
58+
}
59+
5460
_build_patch_obj_n = lambda obj: {str:}, value: any, elements: [str], n: int -> {str:} {
5561
assert n >= 0
5662
result = Undefined
@@ -66,18 +72,18 @@ _build_patch_obj_n = lambda obj: {str:}, value: any, elements: [str], n: int ->
6672
idx = next_key
6773
assert 0 <= idx < len(current_obj), "value not found for path: {}".format(current_path + "/" + elements[n + 1])
6874
result = {
69-
"${elements[n]}": [None] * idx + [next_val] + [None] * (len(current_obj) - 1 - idx)
75+
"${_rfc6901Decode(elements[n])}": [None] * idx + [next_val] + [None] * (len(current_obj) - 1 - idx)
7076
}
7177
# Config key
7278
else:
7379
next_val = _build_patch_obj_n(obj, value, elements, n + 1)
7480
result = {
75-
"${elements[n]}": next_val
81+
"${_rfc6901Decode(elements[n])}": next_val
7682
}
7783
# No Next value
7884
else:
7985
result = {
80-
"${elements[n]}" = value
86+
"${_rfc6901Decode(elements[n])}" = value
8187
}
8288
result
8389
}

jsonpatch/main_test.k

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
test_json_patch = lambda {
22
data = {
3+
"labels": {
4+
"org.io/team-name": "core"
5+
}
36
"firstName": "John",
47
"lastName": "Doe",
58
"age": 30,
@@ -22,10 +25,14 @@ test_json_patch = lambda {
2225
}
2326
phoneNumbers0type: str = get_obj(data, "phoneNumbers/0/type")
2427
addressCity: str = get_obj(data, "address/city")
28+
teamName: str = get_obj(data, "labels/org.io~1team-name")
2529
newType = set_obj(data, "phoneNumbers/0/type", "school")
2630
newState = set_obj(data, "address/state", "WA")
31+
newTeam = set_obj(data, "labels/org.io~1team-name", "security")
2732
assert phoneNumbers0type == "home"
2833
assert addressCity == "New York"
34+
assert teamName == "core"
2935
assert newType["phoneNumbers"][0]["type"] == "school"
3036
assert newState["address"]["state"] == "WA"
37+
assert newTeam["labels"]["org.io/team-name"] == "security"
3138
}

0 commit comments

Comments
 (0)