@@ -218,67 +218,71 @@ def _wrong_type_value(xs_type, value):
218
218
type (None ): '' ,
219
219
}
220
220
221
+ # entries of xs-types each declaring:
222
+ # - a corresponding python type
223
+ # - a function to turn a string into that type
224
+ # - a function to turn that type into a text-value
221
225
xs_types_map = {
222
226
'xs:string' : {
223
227
'type' : _str ,
224
- 'typed_constructor ' : _str ,
225
- 'text_constructor ' : _str ,
228
+ 'to_type ' : _str ,
229
+ 'to_text ' : _str ,
226
230
},
227
231
'xs:integer' : {
228
232
'type' : int ,
229
- 'typed_constructor ' : int ,
230
- 'text_constructor ' : _str ,
233
+ 'to_type ' : int ,
234
+ 'to_text ' : _str ,
231
235
},
232
236
'xs:short' : {
233
237
'type' : int ,
234
- 'typed_constructor ' : int ,
235
- 'text_constructor ' : _str ,
238
+ 'to_type ' : int ,
239
+ 'to_text ' : _str ,
236
240
},
237
241
'xs:int' : {
238
242
'type' : int ,
239
- 'typed_constructor ' : int ,
240
- 'text_constructor ' : _str ,
243
+ 'to_type ' : int ,
244
+ 'to_text ' : _str ,
241
245
},
242
246
'xs:long' : {
243
247
'type' : int ,
244
- 'typed_constructor ' : int ,
245
- 'text_constructor ' : _str ,
248
+ 'to_type ' : int ,
249
+ 'to_text ' : _str ,
246
250
},
247
251
'xs:float' : {
248
252
'type' : float ,
249
- 'typed_constructor ' : float ,
250
- 'text_constructor ' : _str ,
253
+ 'to_type ' : float ,
254
+ 'to_text ' : _str ,
251
255
},
252
256
'xs:double' : {
253
257
'type' : float ,
254
- 'typed_constructor ' : float ,
255
- 'text_constructor ' : _str ,
258
+ 'to_type ' : float ,
259
+ 'to_text ' : _str ,
256
260
},
257
261
'xs:boolean' : {
258
262
'type' : bool ,
259
- 'typed_constructor ' : lambda x : {
263
+ 'to_type ' : lambda x : {
260
264
'true' : True ,
261
265
'false' : False ,
262
266
}[_str (x ).lower ()],
263
- 'text_constructor ' : lambda x : _str (x ).lower (),
267
+ 'to_text ' : lambda x : _str (x ).lower (),
264
268
},
265
269
'xs:base64Binary' : {
266
270
'type' : _str ,
267
- 'typed_constructor ' : _str ,
268
- 'text_constructor ' : lambda x :
271
+ 'to_type ' : _str ,
272
+ 'to_text ' : lambda x :
269
273
_b64_encode_fn (x .encode ())
270
274
if base64encode
271
275
else x ,
272
276
},
273
277
'xs:anyType' : {
274
278
'type' : type (value ),
275
- 'typed_constructor ' : lambda x : x ,
276
- 'text_constructor ' : lambda x : x ,
279
+ 'to_type ' : lambda x : x ,
280
+ 'to_text ' : lambda x : x ,
277
281
},
278
282
'' : {
279
283
'type' : type (None ),
280
- 'typed_constructor ' : lambda x : None ,
281
- 'text_constructor ' : lambda x : '' ,
284
+ 'to_type ' : lambda x : None ,
285
+ 'to_text ' : lambda x : '' ,
282
286
},
283
287
}
284
288
@@ -289,13 +293,13 @@ def _wrong_type_value(xs_type, value):
289
293
or xs_type_from_type .get (type (value ), type (None ))
290
294
xs_type_map = xs_types_map .get (xs_type , {})
291
295
valid_type = xs_type_map .get ('type' , type (None ))
292
- to_typed = xs_type_map .get ('typed_constructor ' , str )
293
- to_text = xs_type_map .get ('text_constructor ' , str )
296
+ to_type = xs_type_map .get ('to_type ' , str )
297
+ to_text = xs_type_map .get ('to_text ' , str )
294
298
295
299
# cast to correct type before type-checking
296
300
if type (value ) is _str and valid_type is not _str :
297
301
try :
298
- value = to_typed (value )
302
+ value = to_type (value )
299
303
except (TypeError , ValueError , KeyError ) as e :
300
304
# the cast failed
301
305
_wrong_type_value (xs_type = xs_type , value = value )
0 commit comments