Skip to content

Commit ffa1afc

Browse files
committed
Add CopyValues method and unit tests for message transformations
1 parent a5d55fb commit ffa1afc

File tree

4 files changed

+99
-26
lines changed

4 files changed

+99
-26
lines changed

src/iop/cls/IOP/Message.cls

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Method GetValueAt(
5656
Set matches = parser.find(tJSON)
5757

5858
Set tResult = ""
59-
59+
zw
6060
// Return the first match
6161
if matches."__len__"() = 1 {
6262
Set match = matches."__getitem__"(0)
@@ -120,6 +120,30 @@ ClassMethod ProcessBrackets(pPath As %String) As %String
120120
Return tPath
121121
}
122122

123+
Method CopyValues(
124+
pSource As Ens.VDoc.Interface,
125+
pSourcePath As %String,
126+
pTargetPath As %String,
127+
pAction As %String,
128+
pKey As %String,
129+
pEmptyFieldAsNull As %Boolean = 0,
130+
pIgnoreMissingSource As %Boolean = 0,
131+
pGenerateEmptySegments As %Boolean = 0) As %Status
132+
{
133+
Set tSC = $$$OK
134+
Try {
135+
// Get source value
136+
Set tValue = pSource.GetValueAt(pSourcePath, "String", .tSC, 0)
137+
Return:$$$ISERR(tSC) tSC
138+
139+
// Set target value
140+
Set tSC = ..SetValueAt(tValue, pTargetPath, pAction, pKey)
141+
} Catch ex {
142+
Set tSC = ex.AsStatus()
143+
}
144+
Return tSC
145+
}
146+
123147
Method SetValueAt(
124148
pValue As %String = "",
125149
pPropertyPath As %String = "",
@@ -128,9 +152,6 @@ Method SetValueAt(
128152
{
129153
Set tSC = $$$OK
130154
// 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
134155
Try {
135156
// Convert pPropertyPath to a a jsonpath
136157
Set tPath = ..ConvertPath(pPropertyPath)
@@ -139,26 +160,20 @@ Method SetValueAt(
139160
Set jp = ##class(%SYS.Python).Import("jsonpath_ng")
140161
Set builtins = ##class(%SYS.Python).Builtins()
141162

163+
if ..json = "" {
164+
Set ..json = "{}"
165+
}
142166
Set tJSON = pyjson.loads(..json)
143167

144168
Set parser = jp.parse(tPath)
145169
if pAction = "set" {
146-
Set tJSON = parser.update(tJSON, pValue)
147-
}
148-
ElseIf pAction = "clear" {
149-
Set tJSON = parser.remove(tJSON)
150-
}
151-
ElseIf pAction = "append" {
152-
Set tJSON = parser.append(tJSON, pValue)
153-
}
154-
ElseIf pAction = "insert" {
155-
Set tJSON = parser.insert(tJSON, pValue, pKey)
170+
Set tJSON = parser."update_or_create"(tJSON, pValue)
156171
}
157172

158173
Set tResult = pyjson.dumps(tJSON)
159174
Set ..json = tResult
160175
Set ..classname = ..DocType
161-
176+
162177
} Catch ex {
163178
Set tSC = ex.AsStatus()
164179
}

src/tests/cls/SimpleMessageSet.cls

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Class UnitTest.SimpleMessageSet Extends Ens.DataTransformDTL [ DependsOn = (IOP.Message, IOP.Message) ]
2+
{
3+
4+
Parameter GENERATEEMPTYSEGMENTS = 0;
5+
6+
Parameter IGNOREMISSINGSOURCE = 1;
7+
8+
Parameter REPORTERRORS = 1;
9+
10+
Parameter TREATEMPTYREPEATINGFIELDASNULL = 0;
11+
12+
XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
13+
{
14+
<transform sourceClass='Ens.StringRequest' targetClass='IOP.Message' sourceDocType='registerFilesIop.message.SimpleMessage' targetDocType='registerFilesIop.message.SimpleMessage' create='new' language='objectscript' >
15+
<assign value='source.StringValue' property='target.{string}' action='set' />
16+
</transform>
17+
}
18+
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Class UnitTest.SimpleMessageSetVDoc Extends Ens.DataTransformDTL [ DependsOn = (IOP.Message, IOP.Message) ]
2+
{
3+
4+
Parameter GENERATEEMPTYSEGMENTS = 0;
5+
6+
Parameter IGNOREMISSINGSOURCE = 1;
7+
8+
Parameter REPORTERRORS = 1;
9+
10+
Parameter TREATEMPTYREPEATINGFIELDASNULL = 0;
11+
12+
XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
13+
{
14+
<transform sourceClass='IOP.Message' targetClass='IOP.Message' sourceDocType='registerFilesIop.message.SimpleMessage' targetDocType='registerFilesIop.message.SimpleMessage' create='new' language='objectscript' >
15+
<assign value='source.{string}' property='target.{string}' action='set' />
16+
</transform>
17+
}
18+
19+
}

src/tests/test_iop_dtl.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,17 @@ def test_register_message_schema(message_class, expected_name):
2525
assert iop_schema.Category == expected_name
2626
assert iop_schema.Name == expected_name
2727

28-
@pytest.mark.parametrize("json_data,classname,path,expected", [
29-
('{"string":"Foo", "integer":42}', 'registerFilesIop.message.SimpleMessage', 'string', 'Foo'),
30-
('{"post":{"Title":"Foo"}, "string":"bar", "list_str":["Foo","Bar"]}', 'registerFilesIop.message.ComplexMessage', 'post.Title', 'Foo'),
31-
('{"post":{"Title":"Foo"}, "list_post":[{"Title":"Bar"},{"Title":"Foo"}]}', 'registerFilesIop.message.ComplexMessage', 'list_post(2).Title', 'Foo'),
32-
('{"list_str":["Foo","Bar"]}', 'registerFilesIop.message.ComplexMessage', 'list_str(2)', 'Bar'),
33-
('{"list_str":["Foo","Bar"]}', 'registerFilesIop.message.ComplexMessage', 'list_str()', ['Foo','Bar']),
34-
('{"list_str":["Foo","Bar"]}', 'registerFilesIop.message.ComplexMessage', 'list_str', ['Foo','Bar']),
35-
('{"list":["Foo","sub_list":["Bar","Baz"]]}', 'registerFilesIop.message.ComplexMessage', 'list().sub_list(2)', 'Baz'),
28+
@pytest.mark.parametrize("json_data,path,expected", [
29+
('{"string":"Foo", "integer":42}', 'string', 'Foo'),
30+
('{"post":{"Title":"Foo"}, "string":"bar", "list_str":["Foo","Bar"]}', 'post.Title', 'Foo'),
31+
('{"post":{"Title":"Foo"}, "list_post":[{"Title":"Bar"},{"Title":"Foo"}]}', 'list_post(2).Title', 'Foo'),
32+
('{"list_str":["Foo","Bar"]}', 'list_str(2)', 'Bar'),
33+
('{"list_str":["Foo","Bar"]}', 'list_str()', ['Foo','Bar']),
34+
('{"list_str":["Foo","Bar"]}', 'list_str', ['Foo','Bar']),
35+
('{"list":["Foo",["Bar","Baz"]]}', 'list(2)(2)', 'Baz'),
3636
])
37-
def test_get_value_at(iop_message, json_data, classname, path, expected):
37+
def test_get_value_at(iop_message, json_data, path, expected):
3838
iop_message.json = json_data
39-
iop_message.classname = classname
4039
result = iop_message.GetValueAt(path)
4140
assert result == expected
4241

@@ -70,7 +69,7 @@ def test_set_value_at(iop_message, json_data, path, value, action, key, expected
7069
'Foo'
7170
)
7271
])
73-
def test_transform(load_cls_files, iop_message, json_data, classname, transform_class, expected_value):
72+
def test_get_transform(load_cls_files, iop_message, json_data, classname, transform_class, expected_value):
7473
ref = iris.ref(None)
7574
iop_message.json = json_data
7675
iop_message.classname = classname
@@ -79,3 +78,24 @@ def test_transform(load_cls_files, iop_message, json_data, classname, transform_
7978
result = ref.value
8079

8180
assert result.StringValue == expected_value
81+
82+
def test_set_transform(load_cls_files):
83+
ref = iris.ref(None)
84+
message = iris.cls('Ens.StringRequest')._New()
85+
message.StringValue = 'Foo'
86+
87+
_Utils.raise_on_error(iris.cls('UnitTest.SimpleMessageSet').Transform(message, ref))
88+
result = ref.value
89+
90+
assert json.loads(result.json) == json.loads('{"string":"Foo"}')
91+
92+
def test_set_transform_vdoc(load_cls_files, iop_message):
93+
ref = iris.ref(None)
94+
iop_message.json = '{"string":"Foo", "integer":42}'
95+
iop_message.classname = 'registerFilesIop.message.SimpleMessage'
96+
97+
_Utils.raise_on_error(iris.cls('UnitTest.SimpleMessageSetVDoc').Transform(iop_message, ref))
98+
result = ref.value
99+
100+
assert json.loads(result.json) == json.loads('{"string":"Foo"}')
101+
assert result.classname == 'registerFilesIop.message.SimpleMessage'

0 commit comments

Comments
 (0)