Skip to content

Commit

Permalink
Merge branch 'refactor_message'
Browse files Browse the repository at this point in the history
  • Loading branch information
grongierisc committed Jul 29, 2024
2 parents a167c44 + a151a85 commit 8a1c8d9
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 27 deletions.
12 changes: 9 additions & 3 deletions src/iop/_business_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ def _serialize_message(self,message):
msg = iris.cls('IOP.Message')._New()
msg.classname = module + "." + classname

stream = _Utils.string_to_stream(json_string)
msg.jstr = stream
if hasattr(message, 'buffer') and len(json_string) > msg.buffer:
msg.json = _Utils.string_to_stream(json_string)
else:
msg.json = json_string

return msg

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

string = _Utils.stream_to_string(serial.jstr)
string = ""
if (serial.type == 'Stream'):
string = _Utils.stream_to_string(serial.json)
else:
string = serial.json

jdict = json.loads(string, cls=IrisJSONDecoder)
msg = self._dataclass_from_dict(msg,jdict)
Expand Down
100 changes: 80 additions & 20 deletions src/iop/cls/IOP/Message.cls
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,105 @@ Class IOP.Message Extends (Ens.MessageBody, %CSP.Page, %XML.Adaptor)

Parameter BUFFER = 64000;

Property buffer As %String(MAXLEN = "") [ Calculated, Transient ];

Property classname As %String(MAXLEN = "");

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

Property json As %String(MAXLEN = "");
Property json As %String(MAXLEN = "") [ Calculated, Transient ];

Property jsonStream As %Stream.GlobalCharacter [ Internal, ReadOnly ];

Property jsonString As %String(MAXLEN = 64000) [ Internal, ReadOnly ];

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

// for retrocompatibility

Property type As %String(MAXLEN = 6) [ ReadOnly ];

Method bufferGet()
{
Quit ..#BUFFER
}

Method %OnNew(classname) As %Status [ Private, ServerOnly = 1 ]
{
set ..classname = $g(classname)
Quit $$$OK
}

Method jsonGet()
Method jstrGet()
{
QUIT ..GetObjectJson()
set rsp = $$$NULLOREF
// Get as stream no matter what
if ..type="String" {
Set rsp = ##class(%Stream.GlobalCharacter).%New()
Set sc = rsp.Write(..jsonString)
}
elseif ..type="Stream" {
set rsp = ..jsonStream
}
Quit rsp
}

Method jsonSet(value) As %Status
Method jstrSet(pInput) As %Status
{
set ..jsonObject = {}.%FromJSON(value)
set ..jstr = ##class(%Stream.GlobalCharacter).%New()
do ..jstr.Write(value)
return $$$OK
// Set as stream no matter what
set sc = ..jsonSet(pInput)
if $$$ISERR(sc) { Quit sc }
if ..type="String" {
set stream = ##class(%Stream.GlobalCharacter).%New()
Set sc = stream.Write(..jsonString)
set r%jsonStream = stream
set i%type = "Stream"
}
Quit sc
}

Method %DispatchGetProperty(property As %String) As %ObjectHandle
Method jsonGet()
{
quit $property(..jsonObject,property)
Quit $Case(..type
, "String":..jsonString
, "Stream":..jsonStream
, :$$$NULLOREF)
}

Method %DispatchSetProperty(
property As %String,
value)
Method jsonSet(pInput) As %Status
{
set $property(..jsonObject,property) = value
quit
Set tOldStream=$Case(..type
, "String":..jsonString
, "Stream":..jsonStream
, :$$$NULLOREF)
Quit:tOldStream=pInput $$$OK
Do:..type'="" Clear() Set i%type=""
If $ISOBJECT(pInput) && pInput.%Extends("%Stream.GlobalCharacter") {
Set r%jsonStream=pInput, i%type="Stream"
}
If $IsObject(pInput) && 'pInput.%Extends("%Stream.GlobalCharacter") {
Throw ##class(%Exception.General).%New("Invalid input type, must be a %Stream.GlobalCharacter")
}
Else {
Set i%jsonString=pInput, i%type="String"
}
Quit $$$OK
Clear()
If ..type="String" { Set r%jsonString="" }
ElseIf ..type="Stream" { Set r%jsonStream=$$$NULLOREF }
Quit
}

Method GetObjectJson(ByRef atEnd)
{
set atEnd = 1
set json = ..jsonObject.%ToJSON()
if json = "{}" {
d ..jstr.Rewind()
set json = ..jstr.Read(..#BUFFER)
set atEnd = ..jstr.AtEnd
if ..type = "String" {
set json = ..jsonString
}
elseif ..type = "Stream" {
do ..jsonStream.Rewind()
set json = ..jsonStream.Read(..#BUFFER)
set atEnd = ..jsonStream.AtEnd
}
QUIT json
}
Expand Down Expand Up @@ -115,6 +166,15 @@ Storage Default
<Value name="3">
<Value>jstr</Value>
</Value>
<Value name="4">
<Value>jsonStream</Value>
</Value>
<Value name="5">
<Value>type</Value>
</Value>
<Value name="6">
<Value>jsonString</Value>
</Value>
</Data>
<Data name="jsonObject">
<Attribute>jsonObject</Attribute>
Expand Down
11 changes: 11 additions & 0 deletions src/iop/cls/IOP/Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
Class IOP.Test Extends %Persistent
{

/// Description
ClassMethod TEST() As %Status
{
set msg = ##class(IOP.Message).%New()
set msg.classname = "IOP.Message"
set msg.json = "{""name"":""John""}"
set tset = msg.json
set tst = msg.jstr
QUIT $$$OK
}

/// Register
ClassMethod Register() As %Status
{
Expand Down
8 changes: 4 additions & 4 deletions src/tests/test_iop_business_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_dispatch_serializer():
rsp = bh._dispatch_serializer(message)

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

def test_dispatch_serializer_none():
bh = _BusinessHost()
Expand Down Expand Up @@ -156,14 +156,14 @@ def test_serialize_message():
result.jstr.Rewind()
stream = result.jstr.Read()
assert result.classname == 'registerFilesIop.message.TestSimpleMessage'
assert result.json == '{"integer": 1, "string": "test"}'
assert result.GetObjectJson() == '{"integer": 1, "string": "test"}'
assert stream == '{"integer": 1, "string": "test"}'

def test_deseialize_message():
bh = _BusinessHost()
msg = TestSimpleMessage(integer=1, string='test')
result = bh._serialize_message(msg)
assert result.json == '{"integer": 1, "string": "test"}'
assert result.GetObjectJson() == '{"integer": 1, "string": "test"}'
msg = bh._deserialize_message(result)
assert msg.integer == 1
assert msg.string == 'test'
Expand All @@ -172,7 +172,7 @@ def test_deseialize_message_japanese():
bh = _BusinessHost()
msg = TestSimpleMessage(integer=1, string='あいうえお')
result = bh._serialize_message(msg)
assert result.json == '{"integer": 1, "string": "あいうえお"}'
assert result.GetObjectJson() == '{"integer": 1, "string": "あいうえお"}'
msg = bh._deserialize_message(result)
assert msg.integer == 1
assert msg.string == 'あいうえお'
Expand Down
10 changes: 10 additions & 0 deletions src/tests/test_iop_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import iris

def test_iop_message_set_json():
# test set_json
iop_message = iris.cls('IOP.Message')._New()
iop_message.json = 'test'
assert iop_message.jstr.Read() == 'test'
assert iop_message.type == 'String'
assert iop_message.jsonString == 'test'
assert iop_message.json == 'test'

0 comments on commit 8a1c8d9

Please sign in to comment.