src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

array.go (1489B)


      1 package xml
      2 
      3 // arrayMemberWrapper is the default member wrapper tag name for XML Array type
      4 var arrayMemberWrapper = StartElement{
      5 	Name: Name{Local: "member"},
      6 }
      7 
      8 // Array represents the encoding of a XML array type
      9 type Array struct {
     10 	w       writer
     11 	scratch *[]byte
     12 
     13 	// member start element is the array member wrapper start element
     14 	memberStartElement StartElement
     15 
     16 	// isFlattened indicates if the array is a flattened array.
     17 	isFlattened bool
     18 }
     19 
     20 // newArray returns an array encoder.
     21 // It also takes in the  member start element, array start element.
     22 // It takes in a isFlattened bool, indicating that an array is flattened array.
     23 //
     24 // A wrapped array ["value1", "value2"] is represented as
     25 // `<List><member>value1</member><member>value2</member></List>`.
     26 
     27 // A flattened array `someList: ["value1", "value2"]` is represented as
     28 // `<someList>value1</someList><someList>value2</someList>`.
     29 func newArray(w writer, scratch *[]byte, memberStartElement StartElement, arrayStartElement StartElement, isFlattened bool) *Array {
     30 	var memberWrapper = memberStartElement
     31 	if isFlattened {
     32 		memberWrapper = arrayStartElement
     33 	}
     34 
     35 	return &Array{
     36 		w:                  w,
     37 		scratch:            scratch,
     38 		memberStartElement: memberWrapper,
     39 		isFlattened:        isFlattened,
     40 	}
     41 }
     42 
     43 // Member adds a new member to the XML array.
     44 // It returns a Value encoder.
     45 func (a *Array) Member() Value {
     46 	v := newValue(a.w, a.scratch, a.memberStartElement)
     47 	v.isFlattened = a.isFlattened
     48 	return v
     49 }