@@ -11,8 +11,12 @@ Property %Type As %String(MAXLEN = 1000);
11
11
/// Default serialization for methods
12
12
Property %Serialization As %Integer ;
13
13
14
+ /// Automatically escape values on property set
14
15
Property %EscapeOnSet As %Boolean [ InitialExpression = {$$$YES} ];
15
16
17
+ /// Automatically escape values on method call
18
+ Property %EscapeOnCall As %Boolean [ InitialExpression = {$$$NO} ];
19
+
16
20
/// do ##class(isc.py.init.Test).Initialize(,1)
17
21
/// set obj = ##class(isc.py.gw.DynamicObject).%New("Person", "p1", , "'Ed'", "25", "'Test'")
18
22
/// w obj.name
@@ -46,8 +50,12 @@ Method %DispatchGetProperty(property As %String) [ ServerOnly = 1 ]
46
50
$$$TOE(sc , ##class (isc.py.Main ).GetVariableInfo (..%Variable _ " ." _ property , ..%Serialization , .defined , .type , .length ))
47
51
throw :'defined ##class (%Exception.General ).%New (" <PROPERTY>" , property )
48
52
49
- $$$TOE(sc , ##class (isc.py.Main ).SimpleString (" zzzproperty=" _ ..%Variable _ " ." _ property , " zzzproperty" , ..%Serialization , .zzzproperty ))
50
- $$$TOE(sc , ##class (isc.py.Main ).SimpleString (" del zzzproperty" ))
53
+ if ..%IsPrimitive (type ) = $$$YES {
54
+ $$$TOE(sc , ##class (isc.py.Main ).SimpleString (" zzzproperty=" _ ..%Variable _ " ." _ property , " zzzproperty" , ..%Serialization , .zzzproperty ))
55
+ $$$TOE(sc , ##class (isc.py.Main ).SimpleString (" del zzzproperty" ))
56
+ } else {
57
+ set zzzproperty = ..%New (, ..%Variable _ " ." _ property )
58
+ }
51
59
52
60
quit zzzproperty
53
61
}
@@ -65,27 +73,37 @@ Method %DispatchSetProperty(property As %String, val) [ ServerOnly = 1 ]
65
73
} else {
66
74
set value = val
67
75
}
68
- set arguments = $lb (..%Variable ,##class (isc.py.util.Converter ).EscapeString (property ), value )
76
+ set arguments = $lb (..%Variable , ##class (isc.py.util.Converter ).EscapeString (property ), value )
69
77
70
78
$$$TOE(sc , ##class (isc.py.Main ).ExecuteFunction (" setattr" , arguments ))
71
79
}
72
80
73
- /// Call python function
81
+ /// Call python method
74
82
Method %DispatchMethod (method As %String , args ...) [ ServerOnly = 1 ]
75
83
{
76
84
for i =1 :1 :$g (args ) {
77
85
set val = args (i )
78
86
if ($isObject (val ) && val .%Extends (" isc.py.gw.DynamicObject" )) {
79
87
set value = val .%Variable
80
- } elseif (..%EscapeOnSet ) {
88
+ } elseif (..%EscapeOnCall ) {
81
89
set value = ##class (isc.py.util.Converter ).EscapeString (val )
82
90
} else {
83
91
set value = val
84
92
}
85
93
86
94
set args (i ) = value
87
95
}
88
- $$$TOE(sc , ##class (isc.py.Main ).ExecuteFunctionArgs (..%Variable _ " ." _ method , ,..%Serialization , .result , args ...))
96
+
97
+ set variable = " variable" _ $random (100000000 )
98
+
99
+ $$$TOE(sc , ##class (isc.py.Main ).ExecuteFunctionArgs (..%Variable _ " ." _ method , variable ,..%Serialization , .result , args ...))
100
+
101
+ $$$TOE(sc , ##class (isc.py.Main ).GetVariableInfo (variable , , .defined , .type ))
102
+
103
+ if ..%IsPrimitive (type ) = $$$NO {
104
+ set result = ..%New (,variable )
105
+ }
106
+
89
107
quit result
90
108
}
91
109
@@ -126,5 +144,37 @@ Method %Destroy() [ CodeMode = expression ]
126
144
##class (isc.py.Main ).SimpleString (" del " _ ..%Variable )
127
145
}
128
146
147
+ /// Get object as serialized string
148
+ Method %GetString (serialization As %Integer = {..%Serialization }) As %String
149
+ {
150
+ set sc = ..%ToStream (serialization , .stream , $$$YES)
151
+ quit :$$$ISERR(sc ) " "
152
+ if $isObject (stream ) {
153
+ set stream = stream .Read ($$$MaxStringLength)
154
+ }
155
+
156
+ quit stream
157
+ }
158
+
159
+ /// Get object as serialized JSON string
160
+ Method %GetJSON () As %String
161
+ {
162
+ set sc = ..%ToJSON (.stream , $$$YES)
163
+ quit :$$$ISERR(sc ) " "
164
+ if $isObject (stream ) {
165
+ set stream = stream .Read ($$$MaxStringLength)
166
+ }
167
+
168
+ quit stream
169
+ }
170
+
171
+ /// Primitive types are returned serialized.
172
+ /// For non-primitive types ProxyObject is returned.
173
+ ClassMethod %IsPrimitive (type As %String ) As %Boolean
174
+ {
175
+ quit :$lf ($lb (" str" , " int" , " bool" , " float" ), type )>0 $$$YES
176
+ quit $$$NO
177
+ }
178
+
129
179
}
130
180
0 commit comments