Browse Source

codec: xml: update ideas

Ugorji Nwoke 8 years ago
parent
commit
6c3b8cc25d
1 changed files with 8 additions and 2 deletions
  1. 8 2
      codec/xml.go

+ 8 - 2
codec/xml.go

@@ -38,7 +38,8 @@ Extend the struct tag. See representative example:
   }
   }
 
 
 Based on this, we encode
 Based on this, we encode
-  - fields as elements, BUT encode as attributes if struct tag contains ",attr".
+  - fields as elements, BUT
+    encode as attributes if struct tag contains ",attr" and is a scalar (bool, number or string)
   - text as entity-escaped text, BUT encode as CDATA if struct tag contains ",cdata".
   - text as entity-escaped text, BUT encode as CDATA if struct tag contains ",cdata".
 
 
 In this mode, we only encode as attribute if ",attr" is found, and only encode as CDATA
 In this mode, we only encode as attribute if ",attr" is found, and only encode as CDATA
@@ -63,6 +64,7 @@ To handle namespaces:
           switch {
           switch {
             case !nsAware: panic(...)
             case !nsAware: panic(...)
             case strings.endsWith("nsabc"): x.abc()
             case strings.endsWith("nsabc"): x.abc()
+            case strings.endsWith("nsdef"): x.def()
             default: panic(...)
             default: panic(...)
           }
           }
         }
         }
@@ -178,7 +180,7 @@ An XML document is a name, a map of attributes and a list of children.
 Consequently, we cannot "DecodeNaked" into a map[string]interface{} (for example).
 Consequently, we cannot "DecodeNaked" into a map[string]interface{} (for example).
 We have to "DecodeNaked" into something that resembles XML data.
 We have to "DecodeNaked" into something that resembles XML data.
 
 
-To support DecodeNaked (decode into nil interface{}) we have to define some "supporting" types:
+To support DecodeNaked (decode into nil interface{}), we have to define some "supporting" types:
     type Name struct { // Prefered. Less allocations due to conversions.
     type Name struct { // Prefered. Less allocations due to conversions.
       Local string
       Local string
       Space string
       Space string
@@ -190,6 +192,8 @@ To support DecodeNaked (decode into nil interface{}) we have to define some "sup
     }
     }
 Only two "supporting" types are exposed for XML: Name and Element.
 Only two "supporting" types are exposed for XML: Name and Element.
 
 
+// ------------------
+
 We considered 'type Name string' where Name is like "Space Local" (space-separated).
 We considered 'type Name string' where Name is like "Space Local" (space-separated).
 We decided against it, because each creation of a name would lead to
 We decided against it, because each creation of a name would lead to
 double allocation (first convert []byte to string, then concatenate them into a string).
 double allocation (first convert []byte to string, then concatenate them into a string).
@@ -215,6 +219,8 @@ intelligent accessor methods to extract information and for performance.
     }
     }
     func (x *Element) child(i) interface{} // returns string or *Element
     func (x *Element) child(i) interface{} // returns string or *Element
 
 
+// ------------------
+
 Per XML spec and our default handling, white space is insignificant between elements,
 Per XML spec and our default handling, white space is insignificant between elements,
 specifically between parent-child or siblings. White space occuring alone between start
 specifically between parent-child or siblings. White space occuring alone between start
 and end element IS significant. However, if xml:space='preserve', then we 'preserve'
 and end element IS significant. However, if xml:space='preserve', then we 'preserve'