@@ -115,12 +115,7 @@ def to_proto(self, proto=None) -> InputFieldProto:
115
115
# proto.is_param = self.is_param
116
116
117
117
if self .default is not None :
118
- if isinstance (self .default , str ) or isinstance (self .default , bool ) or isinstance (
119
- self .default , (int , float )):
120
- proto .default = str (self .default )
121
- else :
122
- import json
123
- proto .default = json .dumps (self .default )
118
+ proto = self .set_default (proto , self .default )
124
119
125
120
return proto
126
121
@@ -169,17 +164,57 @@ def from_proto(cls, proto):
169
164
170
165
@classmethod
171
166
def set_default (cls , proto = None , default = None ):
172
-
173
- if proto is None :
174
- proto = InputFieldProto ()
175
- if default is not None :
176
- if isinstance (default , str ) or isinstance (default , bool ) or isinstance (
177
- default , (int , float )):
178
- proto .default = str (default )
179
- else :
180
- import json
167
+ try :
168
+ import json
169
+ if proto is None :
170
+ proto = InputFieldProto ()
171
+ if default is not None :
181
172
proto .default = json .dumps (default )
182
- return proto
173
+ return proto
174
+ except Exception :
175
+ if default is not None :
176
+ proto .default = str (default )
177
+ return proto
178
+ except Exception as e :
179
+ raise ValueError (
180
+ f"Error setting default value of type, { type (default )} and value: { default } : { e } " )
181
+
182
+ @classmethod
183
+ def get_default (cls , proto ):
184
+ default_str = proto .default
185
+ default = None
186
+ import json
187
+ try :
188
+ # Attempt to parse as JSON first (for complex types)
189
+ return json .loads (default_str )
190
+ except json .JSONDecodeError :
191
+ pass
192
+ # Check for boolean values stored as "True" or "False"
193
+ if proto .type == resources_pb2 .ModelTypeField .DataType .BOOL :
194
+ try :
195
+ default = bool (default_str )
196
+ except ValueError :
197
+ pass
198
+ # Try to parse as integer
199
+ elif proto .type == resources_pb2 .ModelTypeField .DataType .INT :
200
+ try :
201
+ default = int (default_str )
202
+ except ValueError :
203
+ pass
204
+
205
+ # Try to parse as float
206
+ elif proto .type == resources_pb2 .ModelTypeField .DataType .FLOAT :
207
+ try :
208
+ default = float (default_str )
209
+ except ValueError :
210
+ pass
211
+ elif proto .type == resources_pb2 .ModelTypeField .DataType .STR :
212
+ default = default_str
213
+
214
+ if default is None :
215
+ # If all parsing fails, return the string value
216
+ default = default_str
217
+ return default
183
218
184
219
185
220
class DataConverter :
@@ -211,54 +246,68 @@ def _convert_field(cls, old_data: resources_pb2.Data,
211
246
if data_type == resources_pb2 .ModelTypeField .DataType .STR :
212
247
if old_data .HasField ('text' ):
213
248
new_data .string_value = old_data .text .raw
249
+ old_data .ClearField ('text' )
214
250
return new_data
215
251
elif data_type == resources_pb2 .ModelTypeField .DataType .IMAGE :
216
252
if old_data .HasField ('image' ):
217
253
new_data .image .CopyFrom (old_data .image )
254
+ # Clear the old field to avoid duplication
255
+ old_data .ClearField ('image' )
218
256
return new_data
219
257
elif data_type == resources_pb2 .ModelTypeField .DataType .VIDEO :
220
258
if old_data .HasField ('video' ):
221
259
new_data .video .CopyFrom (old_data .video )
260
+ old_data .ClearField ('video' )
222
261
return new_data
223
262
elif data_type == resources_pb2 .ModelTypeField .DataType .BOOL :
224
263
if old_data .bool_value is not False :
225
264
new_data .bool_value = old_data .bool_value
265
+ old_data .bool_value = False
226
266
return new_data
227
267
elif data_type == resources_pb2 .ModelTypeField .DataType .INT :
228
268
if old_data .int_value != 0 :
229
269
new_data .int_value = old_data .int_value
270
+ old_data .int_value = 0
230
271
return new_data
231
272
elif data_type == resources_pb2 .ModelTypeField .DataType .FLOAT :
232
273
if old_data .float_value != 0.0 :
233
274
new_data .float_value = old_data .float_value
275
+ old_data .float_value = 0.0
234
276
return new_data
235
277
elif data_type == resources_pb2 .ModelTypeField .DataType .BYTES :
236
278
if old_data .bytes_value != b"" :
237
279
new_data .bytes_value = old_data .bytes_value
280
+ old_data .bytes_value = b""
238
281
return new_data
239
282
elif data_type == resources_pb2 .ModelTypeField .DataType .NDARRAY :
240
283
if old_data .HasField ('ndarray' ):
241
284
new_data .ndarray .CopyFrom (old_data .ndarray )
285
+ old_data .ClearField ('ndarray' )
242
286
return new_data
243
287
elif data_type == resources_pb2 .ModelTypeField .DataType .TEXT :
244
288
if old_data .HasField ('text' ):
245
289
new_data .text .CopyFrom (old_data .text )
290
+ old_data .ClearField ('text' )
246
291
return new_data
247
292
elif data_type == resources_pb2 .ModelTypeField .DataType .AUDIO :
248
293
if old_data .HasField ('audio' ):
249
294
new_data .audio .CopyFrom (old_data .audio )
295
+ old_data .ClearField ('audio' )
250
296
return new_data
251
297
elif data_type == resources_pb2 .ModelTypeField .DataType .CONCEPT :
252
298
if old_data .concepts :
253
299
new_data .concepts .extend (old_data .concepts )
300
+ old_data .ClearField ('concepts' )
254
301
return new_data
255
302
elif data_type == resources_pb2 .ModelTypeField .DataType .REGION :
256
303
if old_data .regions :
257
304
new_data .regions .extend (old_data .regions )
305
+ old_data .ClearField ('regions' )
258
306
return new_data
259
307
elif data_type == resources_pb2 .ModelTypeField .DataType .FRAME :
260
308
if old_data .frames :
261
309
new_data .frames .extend (old_data .frames )
310
+ old_data .ClearField ('frames' )
262
311
return new_data
263
312
elif data_type == resources_pb2 .ModelTypeField .DataType .LIST :
264
313
if not field .type_args :
0 commit comments