aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/build.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/build.go')
-rw-r--r--vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/build.go296
1 files changed, 296 insertions, 0 deletions
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/build.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/build.go
new file mode 100644
index 0000000..7091b45
--- /dev/null
+++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/build.go
@@ -0,0 +1,296 @@
1// Package xmlutil provides XML serialization of AWS requests and responses.
2package xmlutil
3
4import (
5 "encoding/base64"
6 "encoding/xml"
7 "fmt"
8 "reflect"
9 "sort"
10 "strconv"
11 "time"
12
13 "github.com/aws/aws-sdk-go/private/protocol"
14)
15
16// BuildXML will serialize params into an xml.Encoder.
17// Error will be returned if the serialization of any of the params or nested values fails.
18func BuildXML(params interface{}, e *xml.Encoder) error {
19 b := xmlBuilder{encoder: e, namespaces: map[string]string{}}
20 root := NewXMLElement(xml.Name{})
21 if err := b.buildValue(reflect.ValueOf(params), root, ""); err != nil {
22 return err
23 }
24 for _, c := range root.Children {
25 for _, v := range c {
26 return StructToXML(e, v, false)
27 }
28 }
29 return nil
30}
31
32// Returns the reflection element of a value, if it is a pointer.
33func elemOf(value reflect.Value) reflect.Value {
34 for value.Kind() == reflect.Ptr {
35 value = value.Elem()
36 }
37 return value
38}
39
40// A xmlBuilder serializes values from Go code to XML
41type xmlBuilder struct {
42 encoder *xml.Encoder
43 namespaces map[string]string
44}
45
46// buildValue generic XMLNode builder for any type. Will build value for their specific type
47// struct, list, map, scalar.
48//
49// Also takes a "type" tag value to set what type a value should be converted to XMLNode as. If
50// type is not provided reflect will be used to determine the value's type.
51func (b *xmlBuilder) buildValue(value reflect.Value, current *XMLNode, tag reflect.StructTag) error {
52 value = elemOf(value)
53 if !value.IsValid() { // no need to handle zero values
54 return nil
55 } else if tag.Get("location") != "" { // don't handle non-body location values
56 return nil
57 }
58
59 t := tag.Get("type")
60 if t == "" {
61 switch value.Kind() {
62 case reflect.Struct:
63 t = "structure"
64 case reflect.Slice:
65 t = "list"
66 case reflect.Map:
67 t = "map"
68 }
69 }
70
71 switch t {
72 case "structure":
73 if field, ok := value.Type().FieldByName("_"); ok {
74 tag = tag + reflect.StructTag(" ") + field.Tag
75 }
76 return b.buildStruct(value, current, tag)
77 case "list":
78 return b.buildList(value, current, tag)
79 case "map":
80 return b.buildMap(value, current, tag)
81 default:
82 return b.buildScalar(value, current, tag)
83 }
84}
85
86// buildStruct adds a struct and its fields to the current XMLNode. All fields any any nested
87// types are converted to XMLNodes also.
88func (b *xmlBuilder) buildStruct(value reflect.Value, current *XMLNode, tag reflect.StructTag) error {
89 if !value.IsValid() {
90 return nil
91 }
92
93 fieldAdded := false
94
95 // unwrap payloads
96 if payload := tag.Get("payload"); payload != "" {
97 field, _ := value.Type().FieldByName(payload)
98 tag = field.Tag
99 value = elemOf(value.FieldByName(payload))
100
101 if !value.IsValid() {
102 return nil
103 }
104 }
105
106 child := NewXMLElement(xml.Name{Local: tag.Get("locationName")})
107
108 // there is an xmlNamespace associated with this struct
109 if prefix, uri := tag.Get("xmlPrefix"), tag.Get("xmlURI"); uri != "" {
110 ns := xml.Attr{
111 Name: xml.Name{Local: "xmlns"},
112 Value: uri,
113 }
114 if prefix != "" {
115 b.namespaces[prefix] = uri // register the namespace
116 ns.Name.Local = "xmlns:" + prefix
117 }
118
119 child.Attr = append(child.Attr, ns)
120 }
121
122 t := value.Type()
123 for i := 0; i < value.NumField(); i++ {
124 member := elemOf(value.Field(i))
125 field := t.Field(i)
126
127 if field.PkgPath != "" {
128 continue // ignore unexported fields
129 }
130 if field.Tag.Get("ignore") != "" {
131 continue
132 }
133
134 mTag := field.Tag
135 if mTag.Get("location") != "" { // skip non-body members
136 continue
137 }
138
139 if protocol.CanSetIdempotencyToken(value.Field(i), field) {
140 token := protocol.GetIdempotencyToken()
141 member = reflect.ValueOf(token)
142 }
143
144 memberName := mTag.Get("locationName")
145 if memberName == "" {
146 memberName = field.Name
147 mTag = reflect.StructTag(string(mTag) + ` locationName:"` + memberName + `"`)
148 }
149 if err := b.buildValue(member, child, mTag); err != nil {
150 return err
151 }
152
153 fieldAdded = true
154 }
155
156 if fieldAdded { // only append this child if we have one ore more valid members
157 current.AddChild(child)
158 }
159
160 return nil
161}
162
163// buildList adds the value's list items to the current XMLNode as children nodes. All
164// nested values in the list are converted to XMLNodes also.
165func (b *xmlBuilder) buildList(value reflect.Value, current *XMLNode, tag reflect.StructTag) error {
166 if value.IsNil() { // don't build omitted lists
167 return nil
168 }
169
170 // check for unflattened list member
171 flattened := tag.Get("flattened") != ""
172
173 xname := xml.Name{Local: tag.Get("locationName")}
174 if flattened {
175 for i := 0; i < value.Len(); i++ {
176 child := NewXMLElement(xname)
177 current.AddChild(child)
178 if err := b.buildValue(value.Index(i), child, ""); err != nil {
179 return err
180 }
181 }
182 } else {
183 list := NewXMLElement(xname)
184 current.AddChild(list)
185
186 for i := 0; i < value.Len(); i++ {
187 iname := tag.Get("locationNameList")
188 if iname == "" {
189 iname = "member"
190 }
191
192 child := NewXMLElement(xml.Name{Local: iname})
193 list.AddChild(child)
194 if err := b.buildValue(value.Index(i), child, ""); err != nil {
195 return err
196 }
197 }
198 }
199
200 return nil
201}
202
203// buildMap adds the value's key/value pairs to the current XMLNode as children nodes. All
204// nested values in the map are converted to XMLNodes also.
205//
206// Error will be returned if it is unable to build the map's values into XMLNodes
207func (b *xmlBuilder) buildMap(value reflect.Value, current *XMLNode, tag reflect.StructTag) error {
208 if value.IsNil() { // don't build omitted maps
209 return nil
210 }
211
212 maproot := NewXMLElement(xml.Name{Local: tag.Get("locationName")})
213 current.AddChild(maproot)
214 current = maproot
215
216 kname, vname := "key", "value"
217 if n := tag.Get("locationNameKey"); n != "" {
218 kname = n
219 }
220 if n := tag.Get("locationNameValue"); n != "" {
221 vname = n
222 }
223
224 // sorting is not required for compliance, but it makes testing easier
225 keys := make([]string, value.Len())
226 for i, k := range value.MapKeys() {
227 keys[i] = k.String()
228 }
229 sort.Strings(keys)
230
231 for _, k := range keys {
232 v := value.MapIndex(reflect.ValueOf(k))
233
234 mapcur := current
235 if tag.Get("flattened") == "" { // add "entry" tag to non-flat maps
236 child := NewXMLElement(xml.Name{Local: "entry"})
237 mapcur.AddChild(child)
238 mapcur = child
239 }
240
241 kchild := NewXMLElement(xml.Name{Local: kname})
242 kchild.Text = k
243 vchild := NewXMLElement(xml.Name{Local: vname})
244 mapcur.AddChild(kchild)
245 mapcur.AddChild(vchild)
246
247 if err := b.buildValue(v, vchild, ""); err != nil {
248 return err
249 }
250 }
251
252 return nil
253}
254
255// buildScalar will convert the value into a string and append it as a attribute or child
256// of the current XMLNode.
257//
258// The value will be added as an attribute if tag contains a "xmlAttribute" attribute value.
259//
260// Error will be returned if the value type is unsupported.
261func (b *xmlBuilder) buildScalar(value reflect.Value, current *XMLNode, tag reflect.StructTag) error {
262 var str string
263 switch converted := value.Interface().(type) {
264 case string:
265 str = converted
266 case []byte:
267 if !value.IsNil() {
268 str = base64.StdEncoding.EncodeToString(converted)
269 }
270 case bool:
271 str = strconv.FormatBool(converted)
272 case int64:
273 str = strconv.FormatInt(converted, 10)
274 case int:
275 str = strconv.Itoa(converted)
276 case float64:
277 str = strconv.FormatFloat(converted, 'f', -1, 64)
278 case float32:
279 str = strconv.FormatFloat(float64(converted), 'f', -1, 32)
280 case time.Time:
281 const ISO8601UTC = "2006-01-02T15:04:05Z"
282 str = converted.UTC().Format(ISO8601UTC)
283 default:
284 return fmt.Errorf("unsupported value for param %s: %v (%s)",
285 tag.Get("locationName"), value.Interface(), value.Type().Name())
286 }
287
288 xname := xml.Name{Local: tag.Get("locationName")}
289 if tag.Get("xmlAttribute") != "" { // put into current node's attribute list
290 attr := xml.Attr{Name: xname, Value: str}
291 current.Attr = append(current.Attr, attr)
292 } else { // regular text node
293 current.AddChild(&XMLNode{Name: xname, Text: str})
294 }
295 return nil
296}