Skip to content

Commit 8a1c8d9

Browse files
committed
Merge branch 'refactor_message'
2 parents a167c44 + a151a85 commit 8a1c8d9

File tree

5 files changed

+114
-27
lines changed

5 files changed

+114
-27
lines changed

src/iop/_business_host.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,10 @@ def _serialize_message(self,message):
265265
msg = iris.cls('IOP.Message')._New()
266266
msg.classname = module + "." + classname
267267

268-
stream = _Utils.string_to_stream(json_string)
269-
msg.jstr = stream
268+
if hasattr(message, 'buffer') and len(json_string) > msg.buffer:
269+
msg.json = _Utils.string_to_stream(json_string)
270+
else:
271+
msg.json = json_string
270272

271273
return msg
272274

@@ -321,7 +323,11 @@ def _deserialize_message(self,serial):
321323
except Exception:
322324
raise ImportError("Class not found: " + classname)
323325

324-
string = _Utils.stream_to_string(serial.jstr)
326+
string = ""
327+
if (serial.type == 'Stream'):
328+
string = _Utils.stream_to_string(serial.json)
329+
else:
330+
string = serial.json
325331

326332
jdict = json.loads(string, cls=IrisJSONDecoder)
327333
msg = self._dataclass_from_dict(msg,jdict)

src/iop/cls/IOP/Message.cls

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,105 @@ Class IOP.Message Extends (Ens.MessageBody, %CSP.Page, %XML.Adaptor)
77

88
Parameter BUFFER = 64000;
99

10+
Property buffer As %String(MAXLEN = "") [ Calculated, Transient ];
11+
1012
Property classname As %String(MAXLEN = "");
1113

1214
Property jsonObject As %DynamicObject(XMLPROJECTION = "None");
1315

14-
Property json As %String(MAXLEN = "");
16+
Property json As %String(MAXLEN = "") [ Calculated, Transient ];
17+
18+
Property jsonStream As %Stream.GlobalCharacter [ Internal, ReadOnly ];
19+
20+
Property jsonString As %String(MAXLEN = 64000) [ Internal, ReadOnly ];
1521

1622
Property jstr As %Stream.GlobalCharacter [ Internal, Private ];
1723

24+
// for retrocompatibility
25+
26+
Property type As %String(MAXLEN = 6) [ ReadOnly ];
27+
28+
Method bufferGet()
29+
{
30+
Quit ..#BUFFER
31+
}
32+
1833
Method %OnNew(classname) As %Status [ Private, ServerOnly = 1 ]
1934
{
2035
set ..classname = $g(classname)
2136
Quit $$$OK
2237
}
2338

24-
Method jsonGet()
39+
Method jstrGet()
2540
{
26-
QUIT ..GetObjectJson()
41+
set rsp = $$$NULLOREF
42+
// Get as stream no matter what
43+
if ..type="String" {
44+
Set rsp = ##class(%Stream.GlobalCharacter).%New()
45+
Set sc = rsp.Write(..jsonString)
46+
}
47+
elseif ..type="Stream" {
48+
set rsp = ..jsonStream
49+
}
50+
Quit rsp
2751
}
2852

29-
Method jsonSet(value) As %Status
53+
Method jstrSet(pInput) As %Status
3054
{
31-
set ..jsonObject = {}.%FromJSON(value)
32-
set ..jstr = ##class(%Stream.GlobalCharacter).%New()
33-
do ..jstr.Write(value)
34-
return $$$OK
55+
// Set as stream no matter what
56+
set sc = ..jsonSet(pInput)
57+
if $$$ISERR(sc) { Quit sc }
58+
if ..type="String" {
59+
set stream = ##class(%Stream.GlobalCharacter).%New()
60+
Set sc = stream.Write(..jsonString)
61+
set r%jsonStream = stream
62+
set i%type = "Stream"
63+
}
64+
Quit sc
3565
}
3666

37-
Method %DispatchGetProperty(property As %String) As %ObjectHandle
67+
Method jsonGet()
3868
{
39-
quit $property(..jsonObject,property)
69+
Quit $Case(..type
70+
, "String":..jsonString
71+
, "Stream":..jsonStream
72+
, :$$$NULLOREF)
4073
}
4174

42-
Method %DispatchSetProperty(
43-
property As %String,
44-
value)
75+
Method jsonSet(pInput) As %Status
4576
{
46-
set $property(..jsonObject,property) = value
47-
quit
77+
Set tOldStream=$Case(..type
78+
, "String":..jsonString
79+
, "Stream":..jsonStream
80+
, :$$$NULLOREF)
81+
Quit:tOldStream=pInput $$$OK
82+
Do:..type'="" Clear() Set i%type=""
83+
If $ISOBJECT(pInput) && pInput.%Extends("%Stream.GlobalCharacter") {
84+
Set r%jsonStream=pInput, i%type="Stream"
85+
}
86+
If $IsObject(pInput) && 'pInput.%Extends("%Stream.GlobalCharacter") {
87+
Throw ##class(%Exception.General).%New("Invalid input type, must be a %Stream.GlobalCharacter")
88+
}
89+
Else {
90+
Set i%jsonString=pInput, i%type="String"
91+
}
92+
Quit $$$OK
93+
Clear()
94+
If ..type="String" { Set r%jsonString="" }
95+
ElseIf ..type="Stream" { Set r%jsonStream=$$$NULLOREF }
96+
Quit
4897
}
4998

5099
Method GetObjectJson(ByRef atEnd)
51100
{
52101
set atEnd = 1
53-
set json = ..jsonObject.%ToJSON()
54-
if json = "{}" {
55-
d ..jstr.Rewind()
56-
set json = ..jstr.Read(..#BUFFER)
57-
set atEnd = ..jstr.AtEnd
102+
if ..type = "String" {
103+
set json = ..jsonString
104+
}
105+
elseif ..type = "Stream" {
106+
do ..jsonStream.Rewind()
107+
set json = ..jsonStream.Read(..#BUFFER)
108+
set atEnd = ..jsonStream.AtEnd
58109
}
59110
QUIT json
60111
}
@@ -115,6 +166,15 @@ Storage Default
115166
<Value name="3">
116167
<Value>jstr</Value>
117168
</Value>
169+
<Value name="4">
170+
<Value>jsonStream</Value>
171+
</Value>
172+
<Value name="5">
173+
<Value>type</Value>
174+
</Value>
175+
<Value name="6">
176+
<Value>jsonString</Value>
177+
</Value>
118178
</Data>
119179
<Data name="jsonObject">
120180
<Attribute>jsonObject</Attribute>

src/iop/cls/IOP/Test.cls

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
Class IOP.Test Extends %Persistent
33
{
44

5+
/// Description
6+
ClassMethod TEST() As %Status
7+
{
8+
set msg = ##class(IOP.Message).%New()
9+
set msg.classname = "IOP.Message"
10+
set msg.json = "{""name"":""John""}"
11+
set tset = msg.json
12+
set tst = msg.jstr
13+
QUIT $$$OK
14+
}
15+
516
/// Register
617
ClassMethod Register() As %Status
718
{

src/tests/test_iop_business_host.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_dispatch_serializer():
5454
rsp = bh._dispatch_serializer(message)
5555

5656
assert rsp.classname == 'registerFilesIop.message.TestSimpleMessage'
57-
assert rsp.json == '{"integer": 1, "string": "test"}'
57+
assert rsp.GetObjectJson() == '{"integer": 1, "string": "test"}'
5858

5959
def test_dispatch_serializer_none():
6060
bh = _BusinessHost()
@@ -156,14 +156,14 @@ def test_serialize_message():
156156
result.jstr.Rewind()
157157
stream = result.jstr.Read()
158158
assert result.classname == 'registerFilesIop.message.TestSimpleMessage'
159-
assert result.json == '{"integer": 1, "string": "test"}'
159+
assert result.GetObjectJson() == '{"integer": 1, "string": "test"}'
160160
assert stream == '{"integer": 1, "string": "test"}'
161161

162162
def test_deseialize_message():
163163
bh = _BusinessHost()
164164
msg = TestSimpleMessage(integer=1, string='test')
165165
result = bh._serialize_message(msg)
166-
assert result.json == '{"integer": 1, "string": "test"}'
166+
assert result.GetObjectJson() == '{"integer": 1, "string": "test"}'
167167
msg = bh._deserialize_message(result)
168168
assert msg.integer == 1
169169
assert msg.string == 'test'
@@ -172,7 +172,7 @@ def test_deseialize_message_japanese():
172172
bh = _BusinessHost()
173173
msg = TestSimpleMessage(integer=1, string='あいうえお')
174174
result = bh._serialize_message(msg)
175-
assert result.json == '{"integer": 1, "string": "あいうえお"}'
175+
assert result.GetObjectJson() == '{"integer": 1, "string": "あいうえお"}'
176176
msg = bh._deserialize_message(result)
177177
assert msg.integer == 1
178178
assert msg.string == 'あいうえお'

src/tests/test_iop_message.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import iris
2+
3+
def test_iop_message_set_json():
4+
# test set_json
5+
iop_message = iris.cls('IOP.Message')._New()
6+
iop_message.json = 'test'
7+
assert iop_message.jstr.Read() == 'test'
8+
assert iop_message.type == 'String'
9+
assert iop_message.jsonString == 'test'
10+
assert iop_message.json == 'test'

0 commit comments

Comments
 (0)