@@ -117,6 +117,106 @@ function getTypeFromSerializationModule(_typeString::AbstractString)
117
117
nothing
118
118
end
119
119
120
+ # # =============================================================================
121
+ # # Transcoding / Unmarshal types helper
122
+ # # =============================================================================
123
+
124
+ """
125
+ $SIGNATURES
126
+ Should be a highly reusable function for any transcoding of intermediate type (or dict) to a desired output type.
127
+
128
+ examples
129
+ ```julia
130
+ Base.@kwdef struct HardType
131
+ name::String
132
+ time::DateTime = now(UTC)
133
+ val::Float64 = 0.0
134
+ end
135
+
136
+ # slight human overhead for each type to ignore extraneous field construction
137
+ # TODO, devnote drop this requirement with filter of _names in transcodeType
138
+ HardType(;
139
+ name::String,
140
+ time::DateTime = now(UTC),
141
+ val::Float64 = 0.0,
142
+ ignorekws...
143
+ ) = HardType(name,time,val)
144
+
145
+ # somehow one gets an intermediate type
146
+ imt = IntermediateType(
147
+ v"1.0",
148
+ "NotUsedYet",
149
+ "test",
150
+ now(UTC),
151
+ 1.0
152
+ )
153
+ # or dict (testing string keys)
154
+ imd = Dict(
155
+ "_version" => v"1.0",
156
+ "_type" => "NotUsedYet",
157
+ "name" => "test",
158
+ "time" => now(UTC),
159
+ "val" => 1.0
160
+ )
161
+ # ordered dict (testing symbol keys)
162
+ iod = OrderedDict(
163
+ :_version => v"1.0",
164
+ :_type => "NotUsedYet",
165
+ :name => "test",
166
+ :time => now(UTC),
167
+ # :val => 1.0
168
+ )
169
+
170
+ # do the transcoding to a slighly different hard type
171
+ T1 = transcodeType(HardType, imt)
172
+ T2 = transcodeType(HardType, imd)
173
+ T3 = transcodeType(HardType, iod)
174
+ ```
175
+ """
176
+ function transcodeType (
177
+ :: Type{T} ,
178
+ inObj
179
+ ) where T
180
+ #
181
+ # specializations as inner functions (don't have to be inners)
182
+ # these few special cases came up with examples below, note recursions
183
+ _instance (S:: Type , x) = S (x)
184
+ _instance (_:: Type{S} , x:: S ) where S = x # if ambiguous, delete and do alternative `_instance(S::Type, x) = S===Any ? x : S(x)`
185
+ _instance (S:: Type{I} , x:: AbstractString ) where I <: Number = Base. parse (I, x)
186
+ _instance (S:: Type{E} , x:: AbstractVector ) where E <: AbstractVector = _instance .(eltype (E),x)
187
+ _instance (S:: Type{<:AbstractDict{K,V}} , x:: AbstractDict ) where {K,V} = (tup= (Symbol .(keys (x)) .=> _instance .(V,values (x)) ) ; S (tup... ) )
188
+
189
+ # what the struct wants
190
+ _types = fieldtypes (T)
191
+ _names = fieldnames (T)
192
+ # (closure) resolve random ordering problem
193
+ _getIdx (s:: Symbol ) = findfirst (x-> x== s, _names)
194
+ # (closure) create an instance of a field
195
+ makething (k:: Symbol , v) = begin
196
+ idx = _getIdx (k)
197
+ if ! isnothing (idx)
198
+ # this field is in the output type and should be included
199
+ k => _instance (_types[_getIdx (k)], v)
200
+ else
201
+ # this field should be ignored in the output type
202
+ Symbol (:VOID_ ,rand (1 : 1000000 )) => nothing
203
+ end
204
+ end
205
+ # zip together keys/fields and values for either dict or intermediate type
206
+ _srckeys (s:: AbstractDict ) = keys (s)
207
+ _srckeys (s) = fieldnames (typeof (s))
208
+ _srcvals (s:: AbstractDict ) = values (s)
209
+ _srcvals (s) = map (k-> getproperty (s,k), _srckeys (s))
210
+ # NOTE, improvement, filter extraneous fields not in _names
211
+ arr = [makething (Symbol (k),v) for (k,v) in zip (_srckeys (inObj),_srcvals (inObj))]
212
+ filter! (s-> s[1 ] in _names, arr)
213
+ # create dict provided fields into a NamedTuple as a type stable "pre-struct"
214
+ nt = (;arr... )
215
+ # use keyword constructors provided by Base.@kwdef to resolve random ordering, incomplete dicts, and defaults
216
+ T (;nt... )
217
+ end
218
+
219
+
120
220
121
221
# #==============================================================================
122
222
# # Variable Packing and unpacking
@@ -128,7 +228,7 @@ function packVariable(v::DFGVariable)
128
228
props[" nstime" ] = string (v. nstime. value)
129
229
props[" tags" ] = v. tags # JSON2.write(v.tags)
130
230
props[" ppeDict" ] = v. ppeDict # JSON2.write(v.ppeDict)
131
- props[" solverDataDict" ] = (Dict (keys (v. solverDataDict) .=> map (vnd -> packVariableNodeData (dfg, vnd), values (v. solverDataDict)))) # JSON2.write
231
+ props[" solverDataDict" ] = (Dict (keys (v. solverDataDict) .=> map (vnd -> packVariableNodeData (vnd), values (v. solverDataDict)))) # JSON2.write
132
232
props[" smallData" ] = v. smallData # JSON2.write(v.smallData)
133
233
props[" solvable" ] = v. solvable
134
234
props[" variableType" ] = typeModuleName (getVariableType (v))
0 commit comments