@@ -581,6 +581,37 @@ def serialize(
581
581
def deserialize (self , element : etree .Element ) -> Optional [List [Any ]]:
582
582
return [value for value in element .text .split ()]
583
583
584
+ class AttributeSerializer (Serializer ):
585
+ def __init__ (
586
+ self , model : Type ['pxml.BaseXmlModel' ], model_field : pd .fields .ModelField , ctx : Serializer .Context ,
587
+ ):
588
+ assert len (model_field .sub_fields ) == 1
589
+ if issubclass (model_field .type_ , pxml .BaseXmlModel ):
590
+ raise errors .ModelFieldError (
591
+ model .__name__ , model_field .name , "Inline list value should be of scalar type" ,
592
+ )
593
+
594
+ ns_attrs = model .__xml_ns_attrs__
595
+ name = ctx .entity_name or model_field .alias
596
+ ns = ctx .entity_ns or (ctx .parent_ns if ns_attrs else None )
597
+ nsmap = ctx .parent_nsmap
598
+
599
+ self .attr_name = QName .from_alias (tag = name , ns = ns , nsmap = nsmap , is_attr = True ).uri
600
+
601
+
602
+ def serialize (
603
+ self , element : etree .Element , value : Any , * , encoder : XmlEncoder , skip_empty : bool = False ,
604
+ ) -> Optional [etree .Element ]:
605
+ if value is None or skip_empty and len (value ) == 0 :
606
+ return element
607
+
608
+ encoded = " " .join (encoder .encode (val ) for val in value )
609
+ element .set (self .attr_name , encoded )
610
+ return element
611
+
612
+ def deserialize (self , element : etree .Element ) -> Optional [List [Any ]]:
613
+ return [value for value in element .get (self .attr_name ).split ()]
614
+
584
615
class ElementSerializer (Serializer ):
585
616
def __init__ (
586
617
self , model : Type ['pxml.BaseXmlModel' ], model_field : pd .fields .ModelField , ctx : Serializer .Context ,
@@ -658,9 +689,10 @@ def build(
658
689
elif field_location is Location .MISSING :
659
690
return cls .TextSerializer (model , model_field , ctx )
660
691
elif field_location is Location .ATTRIBUTE :
661
- raise errors .ModelFieldError (
662
- model .__name__ , model_field .name , "attributes of collection type are not supported" ,
663
- )
692
+ return cls .AttributeSerializer (model , model_field , ctx )
693
+ # raise errors.ModelFieldError(
694
+ # model.__name__, model_field.name, "attributes of collection type are not supported",
695
+ # )
664
696
else :
665
697
raise AssertionError ("unreachable" )
666
698
0 commit comments