Skip to content

Commit a5d55fb

Browse files
committed
init set
1 parent afe7826 commit a5d55fb

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/iop/cls/IOP/Message.cls

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,30 +127,39 @@ Method SetValueAt(
127127
pKey As %String = "") As %Status
128128
{
129129
Set tSC = $$$OK
130-
Set tJSON = {}.%FromJSON(..json)
131-
Set tPath = ..ConvertPath(pPropertyPath)
132-
130+
// if pAction is set, use jsonpath to set the value
131+
// if pAction is clear, use jsonpath to remove the value
132+
// if pAction is append, use jsonpath to append the value
133+
// if pAction is insert, use jsonpath to insert the value, using pKey as the key
133134
Try {
134-
If pAction = "set" {
135-
Do tJSON.%Set(tPath, pValue)
135+
// Convert pPropertyPath to a a jsonpath
136+
Set tPath = ..ConvertPath(pPropertyPath)
137+
138+
Set pyjson = ##class(%SYS.Python).Import("json")
139+
Set jp = ##class(%SYS.Python).Import("jsonpath_ng")
140+
Set builtins = ##class(%SYS.Python).Builtins()
141+
142+
Set tJSON = pyjson.loads(..json)
143+
144+
Set parser = jp.parse(tPath)
145+
if pAction = "set" {
146+
Set tJSON = parser.update(tJSON, pValue)
136147
}
137-
ElseIf pAction = "append" {
138-
Set current = tJSON.%Get(tPath)
139-
If current = "" {
140-
Do tJSON.%Set(tPath, pValue)
141-
}
142-
Else {
143-
Do tJSON.%Set(tPath, current _ pValue)
144-
}
148+
ElseIf pAction = "clear" {
149+
Set tJSON = parser.remove(tJSON)
145150
}
146-
ElseIf pAction = "remove" {
147-
Do tJSON.%Remove(tPath)
151+
ElseIf pAction = "append" {
152+
Set tJSON = parser.append(tJSON, pValue)
148153
}
149-
Else {
150-
Set tSC = $$$ERROR($$$GeneralError, "Invalid action: " _ pAction)
154+
ElseIf pAction = "insert" {
155+
Set tJSON = parser.insert(tJSON, pValue, pKey)
151156
}
152-
}
153-
Catch ex {
157+
158+
Set tResult = pyjson.dumps(tJSON)
159+
Set ..json = tResult
160+
Set ..classname = ..DocType
161+
162+
} Catch ex {
154163
Set tSC = ex.AsStatus()
155164
}
156165
Return tSC

src/tests/test_iop_dtl.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import pytest
33
import iris
4+
import json
45
from iop._utils import _Utils
56
from registerFilesIop.message import SimpleMessage, ComplexMessage
67

@@ -39,6 +40,16 @@ def test_get_value_at(iop_message, json_data, classname, path, expected):
3940
result = iop_message.GetValueAt(path)
4041
assert result == expected
4142

43+
@pytest.mark.parametrize("json_data,path,value,action,key,expected_json", [
44+
('{"string":"Foo", "integer":42}', 'string', 'Bar', 'set', None, '{"string":"Bar", "integer":42}'),
45+
(r'{"post":{"Title":"Foo"}}', 'post.Title', 'Bar', 'set', None, r'{"post":{"Title":"Bar"}}')
46+
])
47+
def test_set_value_at(iop_message, json_data, path, value, action, key, expected_json):
48+
iop_message.json = json_data
49+
iop_message.classname = 'foo'
50+
iop_message.SetValueAt(value, path, action, key)
51+
assert json.loads(iop_message.json) == json.loads(expected_json)
52+
4253
@pytest.mark.parametrize("json_data,classname,transform_class,expected_value", [
4354
(
4455
'{"string":"Foo", "integer":42}',

0 commit comments

Comments
 (0)