map.go (1538B)
1 package xml 2 3 // mapEntryWrapper is the default member wrapper start element for XML Map entry 4 var mapEntryWrapper = StartElement{ 5 Name: Name{Local: "entry"}, 6 } 7 8 // Map represents the encoding of a XML map type 9 type Map struct { 10 w writer 11 scratch *[]byte 12 13 // member start element is the map entry wrapper start element 14 memberStartElement StartElement 15 16 // isFlattened returns true if the map is a flattened map 17 isFlattened bool 18 } 19 20 // newMap returns a map encoder which sets the default map 21 // entry wrapper to `entry`. 22 // 23 // A map `someMap : {{key:"abc", value:"123"}}` is represented as 24 // `<someMap><entry><key>abc<key><value>123</value></entry></someMap>`. 25 func newMap(w writer, scratch *[]byte) *Map { 26 return &Map{ 27 w: w, 28 scratch: scratch, 29 memberStartElement: mapEntryWrapper, 30 } 31 } 32 33 // newFlattenedMap returns a map encoder which sets the map 34 // entry wrapper to the passed in memberWrapper`. 35 // 36 // A flattened map `someMap : {{key:"abc", value:"123"}}` is represented as 37 // `<someMap><key>abc<key><value>123</value></someMap>`. 38 func newFlattenedMap(w writer, scratch *[]byte, memberWrapper StartElement) *Map { 39 return &Map{ 40 w: w, 41 scratch: scratch, 42 memberStartElement: memberWrapper, 43 isFlattened: true, 44 } 45 } 46 47 // Entry returns a Value encoder with map's element. 48 // It writes the member wrapper start tag for each entry. 49 func (m *Map) Entry() Value { 50 v := newValue(m.w, m.scratch, m.memberStartElement) 51 v.isFlattened = m.isFlattened 52 return v 53 }