diff options
Diffstat (limited to 'vendor/github.com/aws/aws-sdk-go/private/protocol')
13 files changed, 1804 insertions, 0 deletions
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/idempotency.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/idempotency.go new file mode 100644 index 0000000..53831df --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/idempotency.go | |||
@@ -0,0 +1,75 @@ | |||
1 | package protocol | ||
2 | |||
3 | import ( | ||
4 | "crypto/rand" | ||
5 | "fmt" | ||
6 | "reflect" | ||
7 | ) | ||
8 | |||
9 | // RandReader is the random reader the protocol package will use to read | ||
10 | // random bytes from. This is exported for testing, and should not be used. | ||
11 | var RandReader = rand.Reader | ||
12 | |||
13 | const idempotencyTokenFillTag = `idempotencyToken` | ||
14 | |||
15 | // CanSetIdempotencyToken returns true if the struct field should be | ||
16 | // automatically populated with a Idempotency token. | ||
17 | // | ||
18 | // Only *string and string type fields that are tagged with idempotencyToken | ||
19 | // which are not already set can be auto filled. | ||
20 | func CanSetIdempotencyToken(v reflect.Value, f reflect.StructField) bool { | ||
21 | switch u := v.Interface().(type) { | ||
22 | // To auto fill an Idempotency token the field must be a string, | ||
23 | // tagged for auto fill, and have a zero value. | ||
24 | case *string: | ||
25 | return u == nil && len(f.Tag.Get(idempotencyTokenFillTag)) != 0 | ||
26 | case string: | ||
27 | return len(u) == 0 && len(f.Tag.Get(idempotencyTokenFillTag)) != 0 | ||
28 | } | ||
29 | |||
30 | return false | ||
31 | } | ||
32 | |||
33 | // GetIdempotencyToken returns a randomly generated idempotency token. | ||
34 | func GetIdempotencyToken() string { | ||
35 | b := make([]byte, 16) | ||
36 | RandReader.Read(b) | ||
37 | |||
38 | return UUIDVersion4(b) | ||
39 | } | ||
40 | |||
41 | // SetIdempotencyToken will set the value provided with a Idempotency Token. | ||
42 | // Given that the value can be set. Will panic if value is not setable. | ||
43 | func SetIdempotencyToken(v reflect.Value) { | ||
44 | if v.Kind() == reflect.Ptr { | ||
45 | if v.IsNil() && v.CanSet() { | ||
46 | v.Set(reflect.New(v.Type().Elem())) | ||
47 | } | ||
48 | v = v.Elem() | ||
49 | } | ||
50 | v = reflect.Indirect(v) | ||
51 | |||
52 | if !v.CanSet() { | ||
53 | panic(fmt.Sprintf("unable to set idempotnecy token %v", v)) | ||
54 | } | ||
55 | |||
56 | b := make([]byte, 16) | ||
57 | _, err := rand.Read(b) | ||
58 | if err != nil { | ||
59 | // TODO handle error | ||
60 | return | ||
61 | } | ||
62 | |||
63 | v.Set(reflect.ValueOf(UUIDVersion4(b))) | ||
64 | } | ||
65 | |||
66 | // UUIDVersion4 returns a Version 4 random UUID from the byte slice provided | ||
67 | func UUIDVersion4(u []byte) string { | ||
68 | // https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29 | ||
69 | // 13th character is "4" | ||
70 | u[6] = (u[6] | 0x40) & 0x4F | ||
71 | // 17th character is "8", "9", "a", or "b" | ||
72 | u[8] = (u[8] | 0x80) & 0xBF | ||
73 | |||
74 | return fmt.Sprintf(`%X-%X-%X-%X-%X`, u[0:4], u[4:6], u[6:8], u[8:10], u[10:]) | ||
75 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/query/build.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/query/build.go new file mode 100644 index 0000000..18169f0 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/query/build.go | |||
@@ -0,0 +1,36 @@ | |||
1 | // Package query provides serialization of AWS query requests, and responses. | ||
2 | package query | ||
3 | |||
4 | //go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/input/query.json build_test.go | ||
5 | |||
6 | import ( | ||
7 | "net/url" | ||
8 | |||
9 | "github.com/aws/aws-sdk-go/aws/awserr" | ||
10 | "github.com/aws/aws-sdk-go/aws/request" | ||
11 | "github.com/aws/aws-sdk-go/private/protocol/query/queryutil" | ||
12 | ) | ||
13 | |||
14 | // BuildHandler is a named request handler for building query protocol requests | ||
15 | var BuildHandler = request.NamedHandler{Name: "awssdk.query.Build", Fn: Build} | ||
16 | |||
17 | // Build builds a request for an AWS Query service. | ||
18 | func Build(r *request.Request) { | ||
19 | body := url.Values{ | ||
20 | "Action": {r.Operation.Name}, | ||
21 | "Version": {r.ClientInfo.APIVersion}, | ||
22 | } | ||
23 | if err := queryutil.Parse(body, r.Params, false); err != nil { | ||
24 | r.Error = awserr.New("SerializationError", "failed encoding Query request", err) | ||
25 | return | ||
26 | } | ||
27 | |||
28 | if r.ExpireTime == 0 { | ||
29 | r.HTTPRequest.Method = "POST" | ||
30 | r.HTTPRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8") | ||
31 | r.SetBufferBody([]byte(body.Encode())) | ||
32 | } else { // This is a pre-signed request | ||
33 | r.HTTPRequest.Method = "GET" | ||
34 | r.HTTPRequest.URL.RawQuery = body.Encode() | ||
35 | } | ||
36 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/query/queryutil/queryutil.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/query/queryutil/queryutil.go new file mode 100644 index 0000000..524ca95 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/query/queryutil/queryutil.go | |||
@@ -0,0 +1,237 @@ | |||
1 | package queryutil | ||
2 | |||
3 | import ( | ||
4 | "encoding/base64" | ||
5 | "fmt" | ||
6 | "net/url" | ||
7 | "reflect" | ||
8 | "sort" | ||
9 | "strconv" | ||
10 | "strings" | ||
11 | "time" | ||
12 | |||
13 | "github.com/aws/aws-sdk-go/private/protocol" | ||
14 | ) | ||
15 | |||
16 | // Parse parses an object i and fills a url.Values object. The isEC2 flag | ||
17 | // indicates if this is the EC2 Query sub-protocol. | ||
18 | func Parse(body url.Values, i interface{}, isEC2 bool) error { | ||
19 | q := queryParser{isEC2: isEC2} | ||
20 | return q.parseValue(body, reflect.ValueOf(i), "", "") | ||
21 | } | ||
22 | |||
23 | func elemOf(value reflect.Value) reflect.Value { | ||
24 | for value.Kind() == reflect.Ptr { | ||
25 | value = value.Elem() | ||
26 | } | ||
27 | return value | ||
28 | } | ||
29 | |||
30 | type queryParser struct { | ||
31 | isEC2 bool | ||
32 | } | ||
33 | |||
34 | func (q *queryParser) parseValue(v url.Values, value reflect.Value, prefix string, tag reflect.StructTag) error { | ||
35 | value = elemOf(value) | ||
36 | |||
37 | // no need to handle zero values | ||
38 | if !value.IsValid() { | ||
39 | return nil | ||
40 | } | ||
41 | |||
42 | t := tag.Get("type") | ||
43 | if t == "" { | ||
44 | switch value.Kind() { | ||
45 | case reflect.Struct: | ||
46 | t = "structure" | ||
47 | case reflect.Slice: | ||
48 | t = "list" | ||
49 | case reflect.Map: | ||
50 | t = "map" | ||
51 | } | ||
52 | } | ||
53 | |||
54 | switch t { | ||
55 | case "structure": | ||
56 | return q.parseStruct(v, value, prefix) | ||
57 | case "list": | ||
58 | return q.parseList(v, value, prefix, tag) | ||
59 | case "map": | ||
60 | return q.parseMap(v, value, prefix, tag) | ||
61 | default: | ||
62 | return q.parseScalar(v, value, prefix, tag) | ||
63 | } | ||
64 | } | ||
65 | |||
66 | func (q *queryParser) parseStruct(v url.Values, value reflect.Value, prefix string) error { | ||
67 | if !value.IsValid() { | ||
68 | return nil | ||
69 | } | ||
70 | |||
71 | t := value.Type() | ||
72 | for i := 0; i < value.NumField(); i++ { | ||
73 | elemValue := elemOf(value.Field(i)) | ||
74 | field := t.Field(i) | ||
75 | |||
76 | if field.PkgPath != "" { | ||
77 | continue // ignore unexported fields | ||
78 | } | ||
79 | if field.Tag.Get("ignore") != "" { | ||
80 | continue | ||
81 | } | ||
82 | |||
83 | if protocol.CanSetIdempotencyToken(value.Field(i), field) { | ||
84 | token := protocol.GetIdempotencyToken() | ||
85 | elemValue = reflect.ValueOf(token) | ||
86 | } | ||
87 | |||
88 | var name string | ||
89 | if q.isEC2 { | ||
90 | name = field.Tag.Get("queryName") | ||
91 | } | ||
92 | if name == "" { | ||
93 | if field.Tag.Get("flattened") != "" && field.Tag.Get("locationNameList") != "" { | ||
94 | name = field.Tag.Get("locationNameList") | ||
95 | } else if locName := field.Tag.Get("locationName"); locName != "" { | ||
96 | name = locName | ||
97 | } | ||
98 | if name != "" && q.isEC2 { | ||
99 | name = strings.ToUpper(name[0:1]) + name[1:] | ||
100 | } | ||
101 | } | ||
102 | if name == "" { | ||
103 | name = field.Name | ||
104 | } | ||
105 | |||
106 | if prefix != "" { | ||
107 | name = prefix + "." + name | ||
108 | } | ||
109 | |||
110 | if err := q.parseValue(v, elemValue, name, field.Tag); err != nil { | ||
111 | return err | ||
112 | } | ||
113 | } | ||
114 | return nil | ||
115 | } | ||
116 | |||
117 | func (q *queryParser) parseList(v url.Values, value reflect.Value, prefix string, tag reflect.StructTag) error { | ||
118 | // If it's empty, generate an empty value | ||
119 | if !value.IsNil() && value.Len() == 0 { | ||
120 | v.Set(prefix, "") | ||
121 | return nil | ||
122 | } | ||
123 | |||
124 | // check for unflattened list member | ||
125 | if !q.isEC2 && tag.Get("flattened") == "" { | ||
126 | if listName := tag.Get("locationNameList"); listName == "" { | ||
127 | prefix += ".member" | ||
128 | } else { | ||
129 | prefix += "." + listName | ||
130 | } | ||
131 | } | ||
132 | |||
133 | for i := 0; i < value.Len(); i++ { | ||
134 | slicePrefix := prefix | ||
135 | if slicePrefix == "" { | ||
136 | slicePrefix = strconv.Itoa(i + 1) | ||
137 | } else { | ||
138 | slicePrefix = slicePrefix + "." + strconv.Itoa(i+1) | ||
139 | } | ||
140 | if err := q.parseValue(v, value.Index(i), slicePrefix, ""); err != nil { | ||
141 | return err | ||
142 | } | ||
143 | } | ||
144 | return nil | ||
145 | } | ||
146 | |||
147 | func (q *queryParser) parseMap(v url.Values, value reflect.Value, prefix string, tag reflect.StructTag) error { | ||
148 | // If it's empty, generate an empty value | ||
149 | if !value.IsNil() && value.Len() == 0 { | ||
150 | v.Set(prefix, "") | ||
151 | return nil | ||
152 | } | ||
153 | |||
154 | // check for unflattened list member | ||
155 | if !q.isEC2 && tag.Get("flattened") == "" { | ||
156 | prefix += ".entry" | ||
157 | } | ||
158 | |||
159 | // sort keys for improved serialization consistency. | ||
160 | // this is not strictly necessary for protocol support. | ||
161 | mapKeyValues := value.MapKeys() | ||
162 | mapKeys := map[string]reflect.Value{} | ||
163 | mapKeyNames := make([]string, len(mapKeyValues)) | ||
164 | for i, mapKey := range mapKeyValues { | ||
165 | name := mapKey.String() | ||
166 | mapKeys[name] = mapKey | ||
167 | mapKeyNames[i] = name | ||
168 | } | ||
169 | sort.Strings(mapKeyNames) | ||
170 | |||
171 | for i, mapKeyName := range mapKeyNames { | ||
172 | mapKey := mapKeys[mapKeyName] | ||
173 | mapValue := value.MapIndex(mapKey) | ||
174 | |||
175 | kname := tag.Get("locationNameKey") | ||
176 | if kname == "" { | ||
177 | kname = "key" | ||
178 | } | ||
179 | vname := tag.Get("locationNameValue") | ||
180 | if vname == "" { | ||
181 | vname = "value" | ||
182 | } | ||
183 | |||
184 | // serialize key | ||
185 | var keyName string | ||
186 | if prefix == "" { | ||
187 | keyName = strconv.Itoa(i+1) + "." + kname | ||
188 | } else { | ||
189 | keyName = prefix + "." + strconv.Itoa(i+1) + "." + kname | ||
190 | } | ||
191 | |||
192 | if err := q.parseValue(v, mapKey, keyName, ""); err != nil { | ||
193 | return err | ||
194 | } | ||
195 | |||
196 | // serialize value | ||
197 | var valueName string | ||
198 | if prefix == "" { | ||
199 | valueName = strconv.Itoa(i+1) + "." + vname | ||
200 | } else { | ||
201 | valueName = prefix + "." + strconv.Itoa(i+1) + "." + vname | ||
202 | } | ||
203 | |||
204 | if err := q.parseValue(v, mapValue, valueName, ""); err != nil { | ||
205 | return err | ||
206 | } | ||
207 | } | ||
208 | |||
209 | return nil | ||
210 | } | ||
211 | |||
212 | func (q *queryParser) parseScalar(v url.Values, r reflect.Value, name string, tag reflect.StructTag) error { | ||
213 | switch value := r.Interface().(type) { | ||
214 | case string: | ||
215 | v.Set(name, value) | ||
216 | case []byte: | ||
217 | if !r.IsNil() { | ||
218 | v.Set(name, base64.StdEncoding.EncodeToString(value)) | ||
219 | } | ||
220 | case bool: | ||
221 | v.Set(name, strconv.FormatBool(value)) | ||
222 | case int64: | ||
223 | v.Set(name, strconv.FormatInt(value, 10)) | ||
224 | case int: | ||
225 | v.Set(name, strconv.Itoa(value)) | ||
226 | case float64: | ||
227 | v.Set(name, strconv.FormatFloat(value, 'f', -1, 64)) | ||
228 | case float32: | ||
229 | v.Set(name, strconv.FormatFloat(float64(value), 'f', -1, 32)) | ||
230 | case time.Time: | ||
231 | const ISO8601UTC = "2006-01-02T15:04:05Z" | ||
232 | v.Set(name, value.UTC().Format(ISO8601UTC)) | ||
233 | default: | ||
234 | return fmt.Errorf("unsupported value for param %s: %v (%s)", name, r.Interface(), r.Type().Name()) | ||
235 | } | ||
236 | return nil | ||
237 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/query/unmarshal.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/query/unmarshal.go new file mode 100644 index 0000000..e0f4d5a --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/query/unmarshal.go | |||
@@ -0,0 +1,35 @@ | |||
1 | package query | ||
2 | |||
3 | //go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/output/query.json unmarshal_test.go | ||
4 | |||
5 | import ( | ||
6 | "encoding/xml" | ||
7 | |||
8 | "github.com/aws/aws-sdk-go/aws/awserr" | ||
9 | "github.com/aws/aws-sdk-go/aws/request" | ||
10 | "github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil" | ||
11 | ) | ||
12 | |||
13 | // UnmarshalHandler is a named request handler for unmarshaling query protocol requests | ||
14 | var UnmarshalHandler = request.NamedHandler{Name: "awssdk.query.Unmarshal", Fn: Unmarshal} | ||
15 | |||
16 | // UnmarshalMetaHandler is a named request handler for unmarshaling query protocol request metadata | ||
17 | var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.query.UnmarshalMeta", Fn: UnmarshalMeta} | ||
18 | |||
19 | // Unmarshal unmarshals a response for an AWS Query service. | ||
20 | func Unmarshal(r *request.Request) { | ||
21 | defer r.HTTPResponse.Body.Close() | ||
22 | if r.DataFilled() { | ||
23 | decoder := xml.NewDecoder(r.HTTPResponse.Body) | ||
24 | err := xmlutil.UnmarshalXML(r.Data, decoder, r.Operation.Name+"Result") | ||
25 | if err != nil { | ||
26 | r.Error = awserr.New("SerializationError", "failed decoding Query response", err) | ||
27 | return | ||
28 | } | ||
29 | } | ||
30 | } | ||
31 | |||
32 | // UnmarshalMeta unmarshals header response values for an AWS Query service. | ||
33 | func UnmarshalMeta(r *request.Request) { | ||
34 | r.RequestID = r.HTTPResponse.Header.Get("X-Amzn-Requestid") | ||
35 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/query/unmarshal_error.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/query/unmarshal_error.go new file mode 100644 index 0000000..f214296 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/query/unmarshal_error.go | |||
@@ -0,0 +1,66 @@ | |||
1 | package query | ||
2 | |||
3 | import ( | ||
4 | "encoding/xml" | ||
5 | "io/ioutil" | ||
6 | |||
7 | "github.com/aws/aws-sdk-go/aws/awserr" | ||
8 | "github.com/aws/aws-sdk-go/aws/request" | ||
9 | ) | ||
10 | |||
11 | type xmlErrorResponse struct { | ||
12 | XMLName xml.Name `xml:"ErrorResponse"` | ||
13 | Code string `xml:"Error>Code"` | ||
14 | Message string `xml:"Error>Message"` | ||
15 | RequestID string `xml:"RequestId"` | ||
16 | } | ||
17 | |||
18 | type xmlServiceUnavailableResponse struct { | ||
19 | XMLName xml.Name `xml:"ServiceUnavailableException"` | ||
20 | } | ||
21 | |||
22 | // UnmarshalErrorHandler is a name request handler to unmarshal request errors | ||
23 | var UnmarshalErrorHandler = request.NamedHandler{Name: "awssdk.query.UnmarshalError", Fn: UnmarshalError} | ||
24 | |||
25 | // UnmarshalError unmarshals an error response for an AWS Query service. | ||
26 | func UnmarshalError(r *request.Request) { | ||
27 | defer r.HTTPResponse.Body.Close() | ||
28 | |||
29 | bodyBytes, err := ioutil.ReadAll(r.HTTPResponse.Body) | ||
30 | if err != nil { | ||
31 | r.Error = awserr.New("SerializationError", "failed to read from query HTTP response body", err) | ||
32 | return | ||
33 | } | ||
34 | |||
35 | // First check for specific error | ||
36 | resp := xmlErrorResponse{} | ||
37 | decodeErr := xml.Unmarshal(bodyBytes, &resp) | ||
38 | if decodeErr == nil { | ||
39 | reqID := resp.RequestID | ||
40 | if reqID == "" { | ||
41 | reqID = r.RequestID | ||
42 | } | ||
43 | r.Error = awserr.NewRequestFailure( | ||
44 | awserr.New(resp.Code, resp.Message, nil), | ||
45 | r.HTTPResponse.StatusCode, | ||
46 | reqID, | ||
47 | ) | ||
48 | return | ||
49 | } | ||
50 | |||
51 | // Check for unhandled error | ||
52 | servUnavailResp := xmlServiceUnavailableResponse{} | ||
53 | unavailErr := xml.Unmarshal(bodyBytes, &servUnavailResp) | ||
54 | if unavailErr == nil { | ||
55 | r.Error = awserr.NewRequestFailure( | ||
56 | awserr.New("ServiceUnavailableException", "service is unavailable", nil), | ||
57 | r.HTTPResponse.StatusCode, | ||
58 | r.RequestID, | ||
59 | ) | ||
60 | return | ||
61 | } | ||
62 | |||
63 | // Failed to retrieve any error message from the response body | ||
64 | r.Error = awserr.New("SerializationError", | ||
65 | "failed to decode query XML error response", decodeErr) | ||
66 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/rest/build.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/rest/build.go new file mode 100644 index 0000000..7161835 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/rest/build.go | |||
@@ -0,0 +1,290 @@ | |||
1 | // Package rest provides RESTful serialization of AWS requests and responses. | ||
2 | package rest | ||
3 | |||
4 | import ( | ||
5 | "bytes" | ||
6 | "encoding/base64" | ||
7 | "encoding/json" | ||
8 | "fmt" | ||
9 | "io" | ||
10 | "net/http" | ||
11 | "net/url" | ||
12 | "path" | ||
13 | "reflect" | ||
14 | "strconv" | ||
15 | "strings" | ||
16 | "time" | ||
17 | |||
18 | "github.com/aws/aws-sdk-go/aws" | ||
19 | "github.com/aws/aws-sdk-go/aws/awserr" | ||
20 | "github.com/aws/aws-sdk-go/aws/request" | ||
21 | ) | ||
22 | |||
23 | // RFC822 returns an RFC822 formatted timestamp for AWS protocols | ||
24 | const RFC822 = "Mon, 2 Jan 2006 15:04:05 GMT" | ||
25 | |||
26 | // Whether the byte value can be sent without escaping in AWS URLs | ||
27 | var noEscape [256]bool | ||
28 | |||
29 | var errValueNotSet = fmt.Errorf("value not set") | ||
30 | |||
31 | func init() { | ||
32 | for i := 0; i < len(noEscape); i++ { | ||
33 | // AWS expects every character except these to be escaped | ||
34 | noEscape[i] = (i >= 'A' && i <= 'Z') || | ||
35 | (i >= 'a' && i <= 'z') || | ||
36 | (i >= '0' && i <= '9') || | ||
37 | i == '-' || | ||
38 | i == '.' || | ||
39 | i == '_' || | ||
40 | i == '~' | ||
41 | } | ||
42 | } | ||
43 | |||
44 | // BuildHandler is a named request handler for building rest protocol requests | ||
45 | var BuildHandler = request.NamedHandler{Name: "awssdk.rest.Build", Fn: Build} | ||
46 | |||
47 | // Build builds the REST component of a service request. | ||
48 | func Build(r *request.Request) { | ||
49 | if r.ParamsFilled() { | ||
50 | v := reflect.ValueOf(r.Params).Elem() | ||
51 | buildLocationElements(r, v, false) | ||
52 | buildBody(r, v) | ||
53 | } | ||
54 | } | ||
55 | |||
56 | // BuildAsGET builds the REST component of a service request with the ability to hoist | ||
57 | // data from the body. | ||
58 | func BuildAsGET(r *request.Request) { | ||
59 | if r.ParamsFilled() { | ||
60 | v := reflect.ValueOf(r.Params).Elem() | ||
61 | buildLocationElements(r, v, true) | ||
62 | buildBody(r, v) | ||
63 | } | ||
64 | } | ||
65 | |||
66 | func buildLocationElements(r *request.Request, v reflect.Value, buildGETQuery bool) { | ||
67 | query := r.HTTPRequest.URL.Query() | ||
68 | |||
69 | // Setup the raw path to match the base path pattern. This is needed | ||
70 | // so that when the path is mutated a custom escaped version can be | ||
71 | // stored in RawPath that will be used by the Go client. | ||
72 | r.HTTPRequest.URL.RawPath = r.HTTPRequest.URL.Path | ||
73 | |||
74 | for i := 0; i < v.NumField(); i++ { | ||
75 | m := v.Field(i) | ||
76 | if n := v.Type().Field(i).Name; n[0:1] == strings.ToLower(n[0:1]) { | ||
77 | continue | ||
78 | } | ||
79 | |||
80 | if m.IsValid() { | ||
81 | field := v.Type().Field(i) | ||
82 | name := field.Tag.Get("locationName") | ||
83 | if name == "" { | ||
84 | name = field.Name | ||
85 | } | ||
86 | if kind := m.Kind(); kind == reflect.Ptr { | ||
87 | m = m.Elem() | ||
88 | } else if kind == reflect.Interface { | ||
89 | if !m.Elem().IsValid() { | ||
90 | continue | ||
91 | } | ||
92 | } | ||
93 | if !m.IsValid() { | ||
94 | continue | ||
95 | } | ||
96 | if field.Tag.Get("ignore") != "" { | ||
97 | continue | ||
98 | } | ||
99 | |||
100 | var err error | ||
101 | switch field.Tag.Get("location") { | ||
102 | case "headers": // header maps | ||
103 | err = buildHeaderMap(&r.HTTPRequest.Header, m, field.Tag) | ||
104 | case "header": | ||
105 | err = buildHeader(&r.HTTPRequest.Header, m, name, field.Tag) | ||
106 | case "uri": | ||
107 | err = buildURI(r.HTTPRequest.URL, m, name, field.Tag) | ||
108 | case "querystring": | ||
109 | err = buildQueryString(query, m, name, field.Tag) | ||
110 | default: | ||
111 | if buildGETQuery { | ||
112 | err = buildQueryString(query, m, name, field.Tag) | ||
113 | } | ||
114 | } | ||
115 | r.Error = err | ||
116 | } | ||
117 | if r.Error != nil { | ||
118 | return | ||
119 | } | ||
120 | } | ||
121 | |||
122 | r.HTTPRequest.URL.RawQuery = query.Encode() | ||
123 | if !aws.BoolValue(r.Config.DisableRestProtocolURICleaning) { | ||
124 | cleanPath(r.HTTPRequest.URL) | ||
125 | } | ||
126 | } | ||
127 | |||
128 | func buildBody(r *request.Request, v reflect.Value) { | ||
129 | if field, ok := v.Type().FieldByName("_"); ok { | ||
130 | if payloadName := field.Tag.Get("payload"); payloadName != "" { | ||
131 | pfield, _ := v.Type().FieldByName(payloadName) | ||
132 | if ptag := pfield.Tag.Get("type"); ptag != "" && ptag != "structure" { | ||
133 | payload := reflect.Indirect(v.FieldByName(payloadName)) | ||
134 | if payload.IsValid() && payload.Interface() != nil { | ||
135 | switch reader := payload.Interface().(type) { | ||
136 | case io.ReadSeeker: | ||
137 | r.SetReaderBody(reader) | ||
138 | case []byte: | ||
139 | r.SetBufferBody(reader) | ||
140 | case string: | ||
141 | r.SetStringBody(reader) | ||
142 | default: | ||
143 | r.Error = awserr.New("SerializationError", | ||
144 | "failed to encode REST request", | ||
145 | fmt.Errorf("unknown payload type %s", payload.Type())) | ||
146 | } | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | |||
153 | func buildHeader(header *http.Header, v reflect.Value, name string, tag reflect.StructTag) error { | ||
154 | str, err := convertType(v, tag) | ||
155 | if err == errValueNotSet { | ||
156 | return nil | ||
157 | } else if err != nil { | ||
158 | return awserr.New("SerializationError", "failed to encode REST request", err) | ||
159 | } | ||
160 | |||
161 | header.Add(name, str) | ||
162 | |||
163 | return nil | ||
164 | } | ||
165 | |||
166 | func buildHeaderMap(header *http.Header, v reflect.Value, tag reflect.StructTag) error { | ||
167 | prefix := tag.Get("locationName") | ||
168 | for _, key := range v.MapKeys() { | ||
169 | str, err := convertType(v.MapIndex(key), tag) | ||
170 | if err == errValueNotSet { | ||
171 | continue | ||
172 | } else if err != nil { | ||
173 | return awserr.New("SerializationError", "failed to encode REST request", err) | ||
174 | |||
175 | } | ||
176 | |||
177 | header.Add(prefix+key.String(), str) | ||
178 | } | ||
179 | return nil | ||
180 | } | ||
181 | |||
182 | func buildURI(u *url.URL, v reflect.Value, name string, tag reflect.StructTag) error { | ||
183 | value, err := convertType(v, tag) | ||
184 | if err == errValueNotSet { | ||
185 | return nil | ||
186 | } else if err != nil { | ||
187 | return awserr.New("SerializationError", "failed to encode REST request", err) | ||
188 | } | ||
189 | |||
190 | u.Path = strings.Replace(u.Path, "{"+name+"}", value, -1) | ||
191 | u.Path = strings.Replace(u.Path, "{"+name+"+}", value, -1) | ||
192 | |||
193 | u.RawPath = strings.Replace(u.RawPath, "{"+name+"}", EscapePath(value, true), -1) | ||
194 | u.RawPath = strings.Replace(u.RawPath, "{"+name+"+}", EscapePath(value, false), -1) | ||
195 | |||
196 | return nil | ||
197 | } | ||
198 | |||
199 | func buildQueryString(query url.Values, v reflect.Value, name string, tag reflect.StructTag) error { | ||
200 | switch value := v.Interface().(type) { | ||
201 | case []*string: | ||
202 | for _, item := range value { | ||
203 | query.Add(name, *item) | ||
204 | } | ||
205 | case map[string]*string: | ||
206 | for key, item := range value { | ||
207 | query.Add(key, *item) | ||
208 | } | ||
209 | case map[string][]*string: | ||
210 | for key, items := range value { | ||
211 | for _, item := range items { | ||
212 | query.Add(key, *item) | ||
213 | } | ||
214 | } | ||
215 | default: | ||
216 | str, err := convertType(v, tag) | ||
217 | if err == errValueNotSet { | ||
218 | return nil | ||
219 | } else if err != nil { | ||
220 | return awserr.New("SerializationError", "failed to encode REST request", err) | ||
221 | } | ||
222 | query.Set(name, str) | ||
223 | } | ||
224 | |||
225 | return nil | ||
226 | } | ||
227 | |||
228 | func cleanPath(u *url.URL) { | ||
229 | hasSlash := strings.HasSuffix(u.Path, "/") | ||
230 | |||
231 | // clean up path, removing duplicate `/` | ||
232 | u.Path = path.Clean(u.Path) | ||
233 | u.RawPath = path.Clean(u.RawPath) | ||
234 | |||
235 | if hasSlash && !strings.HasSuffix(u.Path, "/") { | ||
236 | u.Path += "/" | ||
237 | u.RawPath += "/" | ||
238 | } | ||
239 | } | ||
240 | |||
241 | // EscapePath escapes part of a URL path in Amazon style | ||
242 | func EscapePath(path string, encodeSep bool) string { | ||
243 | var buf bytes.Buffer | ||
244 | for i := 0; i < len(path); i++ { | ||
245 | c := path[i] | ||
246 | if noEscape[c] || (c == '/' && !encodeSep) { | ||
247 | buf.WriteByte(c) | ||
248 | } else { | ||
249 | fmt.Fprintf(&buf, "%%%02X", c) | ||
250 | } | ||
251 | } | ||
252 | return buf.String() | ||
253 | } | ||
254 | |||
255 | func convertType(v reflect.Value, tag reflect.StructTag) (string, error) { | ||
256 | v = reflect.Indirect(v) | ||
257 | if !v.IsValid() { | ||
258 | return "", errValueNotSet | ||
259 | } | ||
260 | |||
261 | var str string | ||
262 | switch value := v.Interface().(type) { | ||
263 | case string: | ||
264 | str = value | ||
265 | case []byte: | ||
266 | str = base64.StdEncoding.EncodeToString(value) | ||
267 | case bool: | ||
268 | str = strconv.FormatBool(value) | ||
269 | case int64: | ||
270 | str = strconv.FormatInt(value, 10) | ||
271 | case float64: | ||
272 | str = strconv.FormatFloat(value, 'f', -1, 64) | ||
273 | case time.Time: | ||
274 | str = value.UTC().Format(RFC822) | ||
275 | case aws.JSONValue: | ||
276 | b, err := json.Marshal(value) | ||
277 | if err != nil { | ||
278 | return "", err | ||
279 | } | ||
280 | if tag.Get("location") == "header" { | ||
281 | str = base64.StdEncoding.EncodeToString(b) | ||
282 | } else { | ||
283 | str = string(b) | ||
284 | } | ||
285 | default: | ||
286 | err := fmt.Errorf("Unsupported value for param %v (%s)", v.Interface(), v.Type()) | ||
287 | return "", err | ||
288 | } | ||
289 | return str, nil | ||
290 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/rest/payload.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/rest/payload.go new file mode 100644 index 0000000..4366de2 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/rest/payload.go | |||
@@ -0,0 +1,45 @@ | |||
1 | package rest | ||
2 | |||
3 | import "reflect" | ||
4 | |||
5 | // PayloadMember returns the payload field member of i if there is one, or nil. | ||
6 | func PayloadMember(i interface{}) interface{} { | ||
7 | if i == nil { | ||
8 | return nil | ||
9 | } | ||
10 | |||
11 | v := reflect.ValueOf(i).Elem() | ||
12 | if !v.IsValid() { | ||
13 | return nil | ||
14 | } | ||
15 | if field, ok := v.Type().FieldByName("_"); ok { | ||
16 | if payloadName := field.Tag.Get("payload"); payloadName != "" { | ||
17 | field, _ := v.Type().FieldByName(payloadName) | ||
18 | if field.Tag.Get("type") != "structure" { | ||
19 | return nil | ||
20 | } | ||
21 | |||
22 | payload := v.FieldByName(payloadName) | ||
23 | if payload.IsValid() || (payload.Kind() == reflect.Ptr && !payload.IsNil()) { | ||
24 | return payload.Interface() | ||
25 | } | ||
26 | } | ||
27 | } | ||
28 | return nil | ||
29 | } | ||
30 | |||
31 | // PayloadType returns the type of a payload field member of i if there is one, or "". | ||
32 | func PayloadType(i interface{}) string { | ||
33 | v := reflect.Indirect(reflect.ValueOf(i)) | ||
34 | if !v.IsValid() { | ||
35 | return "" | ||
36 | } | ||
37 | if field, ok := v.Type().FieldByName("_"); ok { | ||
38 | if payloadName := field.Tag.Get("payload"); payloadName != "" { | ||
39 | if member, ok := v.Type().FieldByName(payloadName); ok { | ||
40 | return member.Tag.Get("type") | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | return "" | ||
45 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/rest/unmarshal.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/rest/unmarshal.go new file mode 100644 index 0000000..7a779ee --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/rest/unmarshal.go | |||
@@ -0,0 +1,227 @@ | |||
1 | package rest | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "encoding/base64" | ||
6 | "encoding/json" | ||
7 | "fmt" | ||
8 | "io" | ||
9 | "io/ioutil" | ||
10 | "net/http" | ||
11 | "reflect" | ||
12 | "strconv" | ||
13 | "strings" | ||
14 | "time" | ||
15 | |||
16 | "github.com/aws/aws-sdk-go/aws" | ||
17 | "github.com/aws/aws-sdk-go/aws/awserr" | ||
18 | "github.com/aws/aws-sdk-go/aws/request" | ||
19 | ) | ||
20 | |||
21 | // UnmarshalHandler is a named request handler for unmarshaling rest protocol requests | ||
22 | var UnmarshalHandler = request.NamedHandler{Name: "awssdk.rest.Unmarshal", Fn: Unmarshal} | ||
23 | |||
24 | // UnmarshalMetaHandler is a named request handler for unmarshaling rest protocol request metadata | ||
25 | var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.rest.UnmarshalMeta", Fn: UnmarshalMeta} | ||
26 | |||
27 | // Unmarshal unmarshals the REST component of a response in a REST service. | ||
28 | func Unmarshal(r *request.Request) { | ||
29 | if r.DataFilled() { | ||
30 | v := reflect.Indirect(reflect.ValueOf(r.Data)) | ||
31 | unmarshalBody(r, v) | ||
32 | } | ||
33 | } | ||
34 | |||
35 | // UnmarshalMeta unmarshals the REST metadata of a response in a REST service | ||
36 | func UnmarshalMeta(r *request.Request) { | ||
37 | r.RequestID = r.HTTPResponse.Header.Get("X-Amzn-Requestid") | ||
38 | if r.RequestID == "" { | ||
39 | // Alternative version of request id in the header | ||
40 | r.RequestID = r.HTTPResponse.Header.Get("X-Amz-Request-Id") | ||
41 | } | ||
42 | if r.DataFilled() { | ||
43 | v := reflect.Indirect(reflect.ValueOf(r.Data)) | ||
44 | unmarshalLocationElements(r, v) | ||
45 | } | ||
46 | } | ||
47 | |||
48 | func unmarshalBody(r *request.Request, v reflect.Value) { | ||
49 | if field, ok := v.Type().FieldByName("_"); ok { | ||
50 | if payloadName := field.Tag.Get("payload"); payloadName != "" { | ||
51 | pfield, _ := v.Type().FieldByName(payloadName) | ||
52 | if ptag := pfield.Tag.Get("type"); ptag != "" && ptag != "structure" { | ||
53 | payload := v.FieldByName(payloadName) | ||
54 | if payload.IsValid() { | ||
55 | switch payload.Interface().(type) { | ||
56 | case []byte: | ||
57 | defer r.HTTPResponse.Body.Close() | ||
58 | b, err := ioutil.ReadAll(r.HTTPResponse.Body) | ||
59 | if err != nil { | ||
60 | r.Error = awserr.New("SerializationError", "failed to decode REST response", err) | ||
61 | } else { | ||
62 | payload.Set(reflect.ValueOf(b)) | ||
63 | } | ||
64 | case *string: | ||
65 | defer r.HTTPResponse.Body.Close() | ||
66 | b, err := ioutil.ReadAll(r.HTTPResponse.Body) | ||
67 | if err != nil { | ||
68 | r.Error = awserr.New("SerializationError", "failed to decode REST response", err) | ||
69 | } else { | ||
70 | str := string(b) | ||
71 | payload.Set(reflect.ValueOf(&str)) | ||
72 | } | ||
73 | default: | ||
74 | switch payload.Type().String() { | ||
75 | case "io.ReadCloser": | ||
76 | payload.Set(reflect.ValueOf(r.HTTPResponse.Body)) | ||
77 | case "io.ReadSeeker": | ||
78 | b, err := ioutil.ReadAll(r.HTTPResponse.Body) | ||
79 | if err != nil { | ||
80 | r.Error = awserr.New("SerializationError", | ||
81 | "failed to read response body", err) | ||
82 | return | ||
83 | } | ||
84 | payload.Set(reflect.ValueOf(ioutil.NopCloser(bytes.NewReader(b)))) | ||
85 | default: | ||
86 | io.Copy(ioutil.Discard, r.HTTPResponse.Body) | ||
87 | defer r.HTTPResponse.Body.Close() | ||
88 | r.Error = awserr.New("SerializationError", | ||
89 | "failed to decode REST response", | ||
90 | fmt.Errorf("unknown payload type %s", payload.Type())) | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | } | ||
95 | } | ||
96 | } | ||
97 | } | ||
98 | |||
99 | func unmarshalLocationElements(r *request.Request, v reflect.Value) { | ||
100 | for i := 0; i < v.NumField(); i++ { | ||
101 | m, field := v.Field(i), v.Type().Field(i) | ||
102 | if n := field.Name; n[0:1] == strings.ToLower(n[0:1]) { | ||
103 | continue | ||
104 | } | ||
105 | |||
106 | if m.IsValid() { | ||
107 | name := field.Tag.Get("locationName") | ||
108 | if name == "" { | ||
109 | name = field.Name | ||
110 | } | ||
111 | |||
112 | switch field.Tag.Get("location") { | ||
113 | case "statusCode": | ||
114 | unmarshalStatusCode(m, r.HTTPResponse.StatusCode) | ||
115 | case "header": | ||
116 | err := unmarshalHeader(m, r.HTTPResponse.Header.Get(name), field.Tag) | ||
117 | if err != nil { | ||
118 | r.Error = awserr.New("SerializationError", "failed to decode REST response", err) | ||
119 | break | ||
120 | } | ||
121 | case "headers": | ||
122 | prefix := field.Tag.Get("locationName") | ||
123 | err := unmarshalHeaderMap(m, r.HTTPResponse.Header, prefix) | ||
124 | if err != nil { | ||
125 | r.Error = awserr.New("SerializationError", "failed to decode REST response", err) | ||
126 | break | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | if r.Error != nil { | ||
131 | return | ||
132 | } | ||
133 | } | ||
134 | } | ||
135 | |||
136 | func unmarshalStatusCode(v reflect.Value, statusCode int) { | ||
137 | if !v.IsValid() { | ||
138 | return | ||
139 | } | ||
140 | |||
141 | switch v.Interface().(type) { | ||
142 | case *int64: | ||
143 | s := int64(statusCode) | ||
144 | v.Set(reflect.ValueOf(&s)) | ||
145 | } | ||
146 | } | ||
147 | |||
148 | func unmarshalHeaderMap(r reflect.Value, headers http.Header, prefix string) error { | ||
149 | switch r.Interface().(type) { | ||
150 | case map[string]*string: // we only support string map value types | ||
151 | out := map[string]*string{} | ||
152 | for k, v := range headers { | ||
153 | k = http.CanonicalHeaderKey(k) | ||
154 | if strings.HasPrefix(strings.ToLower(k), strings.ToLower(prefix)) { | ||
155 | out[k[len(prefix):]] = &v[0] | ||
156 | } | ||
157 | } | ||
158 | r.Set(reflect.ValueOf(out)) | ||
159 | } | ||
160 | return nil | ||
161 | } | ||
162 | |||
163 | func unmarshalHeader(v reflect.Value, header string, tag reflect.StructTag) error { | ||
164 | isJSONValue := tag.Get("type") == "jsonvalue" | ||
165 | if isJSONValue { | ||
166 | if len(header) == 0 { | ||
167 | return nil | ||
168 | } | ||
169 | } else if !v.IsValid() || (header == "" && v.Elem().Kind() != reflect.String) { | ||
170 | return nil | ||
171 | } | ||
172 | |||
173 | switch v.Interface().(type) { | ||
174 | case *string: | ||
175 | v.Set(reflect.ValueOf(&header)) | ||
176 | case []byte: | ||
177 | b, err := base64.StdEncoding.DecodeString(header) | ||
178 | if err != nil { | ||
179 | return err | ||
180 | } | ||
181 | v.Set(reflect.ValueOf(&b)) | ||
182 | case *bool: | ||
183 | b, err := strconv.ParseBool(header) | ||
184 | if err != nil { | ||
185 | return err | ||
186 | } | ||
187 | v.Set(reflect.ValueOf(&b)) | ||
188 | case *int64: | ||
189 | i, err := strconv.ParseInt(header, 10, 64) | ||
190 | if err != nil { | ||
191 | return err | ||
192 | } | ||
193 | v.Set(reflect.ValueOf(&i)) | ||
194 | case *float64: | ||
195 | f, err := strconv.ParseFloat(header, 64) | ||
196 | if err != nil { | ||
197 | return err | ||
198 | } | ||
199 | v.Set(reflect.ValueOf(&f)) | ||
200 | case *time.Time: | ||
201 | t, err := time.Parse(RFC822, header) | ||
202 | if err != nil { | ||
203 | return err | ||
204 | } | ||
205 | v.Set(reflect.ValueOf(&t)) | ||
206 | case aws.JSONValue: | ||
207 | b := []byte(header) | ||
208 | var err error | ||
209 | if tag.Get("location") == "header" { | ||
210 | b, err = base64.StdEncoding.DecodeString(header) | ||
211 | if err != nil { | ||
212 | return err | ||
213 | } | ||
214 | } | ||
215 | |||
216 | m := aws.JSONValue{} | ||
217 | err = json.Unmarshal(b, &m) | ||
218 | if err != nil { | ||
219 | return err | ||
220 | } | ||
221 | v.Set(reflect.ValueOf(m)) | ||
222 | default: | ||
223 | err := fmt.Errorf("Unsupported value for param %v (%s)", v.Interface(), v.Type()) | ||
224 | return err | ||
225 | } | ||
226 | return nil | ||
227 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/restxml/restxml.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/restxml/restxml.go new file mode 100644 index 0000000..7bdf4c8 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/restxml/restxml.go | |||
@@ -0,0 +1,69 @@ | |||
1 | // Package restxml provides RESTful XML serialization of AWS | ||
2 | // requests and responses. | ||
3 | package restxml | ||
4 | |||
5 | //go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/input/rest-xml.json build_test.go | ||
6 | //go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/output/rest-xml.json unmarshal_test.go | ||
7 | |||
8 | import ( | ||
9 | "bytes" | ||
10 | "encoding/xml" | ||
11 | |||
12 | "github.com/aws/aws-sdk-go/aws/awserr" | ||
13 | "github.com/aws/aws-sdk-go/aws/request" | ||
14 | "github.com/aws/aws-sdk-go/private/protocol/query" | ||
15 | "github.com/aws/aws-sdk-go/private/protocol/rest" | ||
16 | "github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil" | ||
17 | ) | ||
18 | |||
19 | // BuildHandler is a named request handler for building restxml protocol requests | ||
20 | var BuildHandler = request.NamedHandler{Name: "awssdk.restxml.Build", Fn: Build} | ||
21 | |||
22 | // UnmarshalHandler is a named request handler for unmarshaling restxml protocol requests | ||
23 | var UnmarshalHandler = request.NamedHandler{Name: "awssdk.restxml.Unmarshal", Fn: Unmarshal} | ||
24 | |||
25 | // UnmarshalMetaHandler is a named request handler for unmarshaling restxml protocol request metadata | ||
26 | var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.restxml.UnmarshalMeta", Fn: UnmarshalMeta} | ||
27 | |||
28 | // UnmarshalErrorHandler is a named request handler for unmarshaling restxml protocol request errors | ||
29 | var UnmarshalErrorHandler = request.NamedHandler{Name: "awssdk.restxml.UnmarshalError", Fn: UnmarshalError} | ||
30 | |||
31 | // Build builds a request payload for the REST XML protocol. | ||
32 | func Build(r *request.Request) { | ||
33 | rest.Build(r) | ||
34 | |||
35 | if t := rest.PayloadType(r.Params); t == "structure" || t == "" { | ||
36 | var buf bytes.Buffer | ||
37 | err := xmlutil.BuildXML(r.Params, xml.NewEncoder(&buf)) | ||
38 | if err != nil { | ||
39 | r.Error = awserr.New("SerializationError", "failed to encode rest XML request", err) | ||
40 | return | ||
41 | } | ||
42 | r.SetBufferBody(buf.Bytes()) | ||
43 | } | ||
44 | } | ||
45 | |||
46 | // Unmarshal unmarshals a payload response for the REST XML protocol. | ||
47 | func Unmarshal(r *request.Request) { | ||
48 | if t := rest.PayloadType(r.Data); t == "structure" || t == "" { | ||
49 | defer r.HTTPResponse.Body.Close() | ||
50 | decoder := xml.NewDecoder(r.HTTPResponse.Body) | ||
51 | err := xmlutil.UnmarshalXML(r.Data, decoder, "") | ||
52 | if err != nil { | ||
53 | r.Error = awserr.New("SerializationError", "failed to decode REST XML response", err) | ||
54 | return | ||
55 | } | ||
56 | } else { | ||
57 | rest.Unmarshal(r) | ||
58 | } | ||
59 | } | ||
60 | |||
61 | // UnmarshalMeta unmarshals response headers for the REST XML protocol. | ||
62 | func UnmarshalMeta(r *request.Request) { | ||
63 | rest.UnmarshalMeta(r) | ||
64 | } | ||
65 | |||
66 | // UnmarshalError unmarshals a response error for the REST XML protocol. | ||
67 | func UnmarshalError(r *request.Request) { | ||
68 | query.UnmarshalError(r) | ||
69 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/unmarshal.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/unmarshal.go new file mode 100644 index 0000000..da1a681 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/unmarshal.go | |||
@@ -0,0 +1,21 @@ | |||
1 | package protocol | ||
2 | |||
3 | import ( | ||
4 | "io" | ||
5 | "io/ioutil" | ||
6 | |||
7 | "github.com/aws/aws-sdk-go/aws/request" | ||
8 | ) | ||
9 | |||
10 | // UnmarshalDiscardBodyHandler is a named request handler to empty and close a response's body | ||
11 | var UnmarshalDiscardBodyHandler = request.NamedHandler{Name: "awssdk.shared.UnmarshalDiscardBody", Fn: UnmarshalDiscardBody} | ||
12 | |||
13 | // UnmarshalDiscardBody is a request handler to empty a response's body and closing it. | ||
14 | func UnmarshalDiscardBody(r *request.Request) { | ||
15 | if r.HTTPResponse == nil || r.HTTPResponse.Body == nil { | ||
16 | return | ||
17 | } | ||
18 | |||
19 | io.Copy(ioutil.Discard, r.HTTPResponse.Body) | ||
20 | r.HTTPResponse.Body.Close() | ||
21 | } | ||
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. | ||
2 | package xmlutil | ||
3 | |||
4 | import ( | ||
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. | ||
18 | func 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. | ||
33 | func 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 | ||
41 | type 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. | ||
51 | func (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. | ||
88 | func (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. | ||
165 | func (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 | ||
207 | func (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. | ||
261 | func (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 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/unmarshal.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/unmarshal.go new file mode 100644 index 0000000..8758462 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/unmarshal.go | |||
@@ -0,0 +1,260 @@ | |||
1 | package xmlutil | ||
2 | |||
3 | import ( | ||
4 | "encoding/base64" | ||
5 | "encoding/xml" | ||
6 | "fmt" | ||
7 | "io" | ||
8 | "reflect" | ||
9 | "strconv" | ||
10 | "strings" | ||
11 | "time" | ||
12 | ) | ||
13 | |||
14 | // UnmarshalXML deserializes an xml.Decoder into the container v. V | ||
15 | // needs to match the shape of the XML expected to be decoded. | ||
16 | // If the shape doesn't match unmarshaling will fail. | ||
17 | func UnmarshalXML(v interface{}, d *xml.Decoder, wrapper string) error { | ||
18 | n, err := XMLToStruct(d, nil) | ||
19 | if err != nil { | ||
20 | return err | ||
21 | } | ||
22 | if n.Children != nil { | ||
23 | for _, root := range n.Children { | ||
24 | for _, c := range root { | ||
25 | if wrappedChild, ok := c.Children[wrapper]; ok { | ||
26 | c = wrappedChild[0] // pull out wrapped element | ||
27 | } | ||
28 | |||
29 | err = parse(reflect.ValueOf(v), c, "") | ||
30 | if err != nil { | ||
31 | if err == io.EOF { | ||
32 | return nil | ||
33 | } | ||
34 | return err | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | return nil | ||
39 | } | ||
40 | return nil | ||
41 | } | ||
42 | |||
43 | // parse deserializes any value from the XMLNode. The type tag is used to infer the type, or reflect | ||
44 | // will be used to determine the type from r. | ||
45 | func parse(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
46 | rtype := r.Type() | ||
47 | if rtype.Kind() == reflect.Ptr { | ||
48 | rtype = rtype.Elem() // check kind of actual element type | ||
49 | } | ||
50 | |||
51 | t := tag.Get("type") | ||
52 | if t == "" { | ||
53 | switch rtype.Kind() { | ||
54 | case reflect.Struct: | ||
55 | t = "structure" | ||
56 | case reflect.Slice: | ||
57 | t = "list" | ||
58 | case reflect.Map: | ||
59 | t = "map" | ||
60 | } | ||
61 | } | ||
62 | |||
63 | switch t { | ||
64 | case "structure": | ||
65 | if field, ok := rtype.FieldByName("_"); ok { | ||
66 | tag = field.Tag | ||
67 | } | ||
68 | return parseStruct(r, node, tag) | ||
69 | case "list": | ||
70 | return parseList(r, node, tag) | ||
71 | case "map": | ||
72 | return parseMap(r, node, tag) | ||
73 | default: | ||
74 | return parseScalar(r, node, tag) | ||
75 | } | ||
76 | } | ||
77 | |||
78 | // parseStruct deserializes a structure and its fields from an XMLNode. Any nested | ||
79 | // types in the structure will also be deserialized. | ||
80 | func parseStruct(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
81 | t := r.Type() | ||
82 | if r.Kind() == reflect.Ptr { | ||
83 | if r.IsNil() { // create the structure if it's nil | ||
84 | s := reflect.New(r.Type().Elem()) | ||
85 | r.Set(s) | ||
86 | r = s | ||
87 | } | ||
88 | |||
89 | r = r.Elem() | ||
90 | t = t.Elem() | ||
91 | } | ||
92 | |||
93 | // unwrap any payloads | ||
94 | if payload := tag.Get("payload"); payload != "" { | ||
95 | field, _ := t.FieldByName(payload) | ||
96 | return parseStruct(r.FieldByName(payload), node, field.Tag) | ||
97 | } | ||
98 | |||
99 | for i := 0; i < t.NumField(); i++ { | ||
100 | field := t.Field(i) | ||
101 | if c := field.Name[0:1]; strings.ToLower(c) == c { | ||
102 | continue // ignore unexported fields | ||
103 | } | ||
104 | |||
105 | // figure out what this field is called | ||
106 | name := field.Name | ||
107 | if field.Tag.Get("flattened") != "" && field.Tag.Get("locationNameList") != "" { | ||
108 | name = field.Tag.Get("locationNameList") | ||
109 | } else if locName := field.Tag.Get("locationName"); locName != "" { | ||
110 | name = locName | ||
111 | } | ||
112 | |||
113 | // try to find the field by name in elements | ||
114 | elems := node.Children[name] | ||
115 | |||
116 | if elems == nil { // try to find the field in attributes | ||
117 | if val, ok := node.findElem(name); ok { | ||
118 | elems = []*XMLNode{{Text: val}} | ||
119 | } | ||
120 | } | ||
121 | |||
122 | member := r.FieldByName(field.Name) | ||
123 | for _, elem := range elems { | ||
124 | err := parse(member, elem, field.Tag) | ||
125 | if err != nil { | ||
126 | return err | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | return nil | ||
131 | } | ||
132 | |||
133 | // parseList deserializes a list of values from an XML node. Each list entry | ||
134 | // will also be deserialized. | ||
135 | func parseList(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
136 | t := r.Type() | ||
137 | |||
138 | if tag.Get("flattened") == "" { // look at all item entries | ||
139 | mname := "member" | ||
140 | if name := tag.Get("locationNameList"); name != "" { | ||
141 | mname = name | ||
142 | } | ||
143 | |||
144 | if Children, ok := node.Children[mname]; ok { | ||
145 | if r.IsNil() { | ||
146 | r.Set(reflect.MakeSlice(t, len(Children), len(Children))) | ||
147 | } | ||
148 | |||
149 | for i, c := range Children { | ||
150 | err := parse(r.Index(i), c, "") | ||
151 | if err != nil { | ||
152 | return err | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | } else { // flattened list means this is a single element | ||
157 | if r.IsNil() { | ||
158 | r.Set(reflect.MakeSlice(t, 0, 0)) | ||
159 | } | ||
160 | |||
161 | childR := reflect.Zero(t.Elem()) | ||
162 | r.Set(reflect.Append(r, childR)) | ||
163 | err := parse(r.Index(r.Len()-1), node, "") | ||
164 | if err != nil { | ||
165 | return err | ||
166 | } | ||
167 | } | ||
168 | |||
169 | return nil | ||
170 | } | ||
171 | |||
172 | // parseMap deserializes a map from an XMLNode. The direct children of the XMLNode | ||
173 | // will also be deserialized as map entries. | ||
174 | func parseMap(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
175 | if r.IsNil() { | ||
176 | r.Set(reflect.MakeMap(r.Type())) | ||
177 | } | ||
178 | |||
179 | if tag.Get("flattened") == "" { // look at all child entries | ||
180 | for _, entry := range node.Children["entry"] { | ||
181 | parseMapEntry(r, entry, tag) | ||
182 | } | ||
183 | } else { // this element is itself an entry | ||
184 | parseMapEntry(r, node, tag) | ||
185 | } | ||
186 | |||
187 | return nil | ||
188 | } | ||
189 | |||
190 | // parseMapEntry deserializes a map entry from a XML node. | ||
191 | func parseMapEntry(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
192 | kname, vname := "key", "value" | ||
193 | if n := tag.Get("locationNameKey"); n != "" { | ||
194 | kname = n | ||
195 | } | ||
196 | if n := tag.Get("locationNameValue"); n != "" { | ||
197 | vname = n | ||
198 | } | ||
199 | |||
200 | keys, ok := node.Children[kname] | ||
201 | values := node.Children[vname] | ||
202 | if ok { | ||
203 | for i, key := range keys { | ||
204 | keyR := reflect.ValueOf(key.Text) | ||
205 | value := values[i] | ||
206 | valueR := reflect.New(r.Type().Elem()).Elem() | ||
207 | |||
208 | parse(valueR, value, "") | ||
209 | r.SetMapIndex(keyR, valueR) | ||
210 | } | ||
211 | } | ||
212 | return nil | ||
213 | } | ||
214 | |||
215 | // parseScaller deserializes an XMLNode value into a concrete type based on the | ||
216 | // interface type of r. | ||
217 | // | ||
218 | // Error is returned if the deserialization fails due to invalid type conversion, | ||
219 | // or unsupported interface type. | ||
220 | func parseScalar(r reflect.Value, node *XMLNode, tag reflect.StructTag) error { | ||
221 | switch r.Interface().(type) { | ||
222 | case *string: | ||
223 | r.Set(reflect.ValueOf(&node.Text)) | ||
224 | return nil | ||
225 | case []byte: | ||
226 | b, err := base64.StdEncoding.DecodeString(node.Text) | ||
227 | if err != nil { | ||
228 | return err | ||
229 | } | ||
230 | r.Set(reflect.ValueOf(b)) | ||
231 | case *bool: | ||
232 | v, err := strconv.ParseBool(node.Text) | ||
233 | if err != nil { | ||
234 | return err | ||
235 | } | ||
236 | r.Set(reflect.ValueOf(&v)) | ||
237 | case *int64: | ||
238 | v, err := strconv.ParseInt(node.Text, 10, 64) | ||
239 | if err != nil { | ||
240 | return err | ||
241 | } | ||
242 | r.Set(reflect.ValueOf(&v)) | ||
243 | case *float64: | ||
244 | v, err := strconv.ParseFloat(node.Text, 64) | ||
245 | if err != nil { | ||
246 | return err | ||
247 | } | ||
248 | r.Set(reflect.ValueOf(&v)) | ||
249 | case *time.Time: | ||
250 | const ISO8601UTC = "2006-01-02T15:04:05Z" | ||
251 | t, err := time.Parse(ISO8601UTC, node.Text) | ||
252 | if err != nil { | ||
253 | return err | ||
254 | } | ||
255 | r.Set(reflect.ValueOf(&t)) | ||
256 | default: | ||
257 | return fmt.Errorf("unsupported value: %v (%s)", r.Interface(), r.Type()) | ||
258 | } | ||
259 | return nil | ||
260 | } | ||
diff --git a/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/xml_to_struct.go b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/xml_to_struct.go new file mode 100644 index 0000000..3e970b6 --- /dev/null +++ b/vendor/github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil/xml_to_struct.go | |||
@@ -0,0 +1,147 @@ | |||
1 | package xmlutil | ||
2 | |||
3 | import ( | ||
4 | "encoding/xml" | ||
5 | "fmt" | ||
6 | "io" | ||
7 | "sort" | ||
8 | ) | ||
9 | |||
10 | // A XMLNode contains the values to be encoded or decoded. | ||
11 | type XMLNode struct { | ||
12 | Name xml.Name `json:",omitempty"` | ||
13 | Children map[string][]*XMLNode `json:",omitempty"` | ||
14 | Text string `json:",omitempty"` | ||
15 | Attr []xml.Attr `json:",omitempty"` | ||
16 | |||
17 | namespaces map[string]string | ||
18 | parent *XMLNode | ||
19 | } | ||
20 | |||
21 | // NewXMLElement returns a pointer to a new XMLNode initialized to default values. | ||
22 | func NewXMLElement(name xml.Name) *XMLNode { | ||
23 | return &XMLNode{ | ||
24 | Name: name, | ||
25 | Children: map[string][]*XMLNode{}, | ||
26 | Attr: []xml.Attr{}, | ||
27 | } | ||
28 | } | ||
29 | |||
30 | // AddChild adds child to the XMLNode. | ||
31 | func (n *XMLNode) AddChild(child *XMLNode) { | ||
32 | if _, ok := n.Children[child.Name.Local]; !ok { | ||
33 | n.Children[child.Name.Local] = []*XMLNode{} | ||
34 | } | ||
35 | n.Children[child.Name.Local] = append(n.Children[child.Name.Local], child) | ||
36 | } | ||
37 | |||
38 | // XMLToStruct converts a xml.Decoder stream to XMLNode with nested values. | ||
39 | func XMLToStruct(d *xml.Decoder, s *xml.StartElement) (*XMLNode, error) { | ||
40 | out := &XMLNode{} | ||
41 | for { | ||
42 | tok, err := d.Token() | ||
43 | if err != nil { | ||
44 | if err == io.EOF { | ||
45 | break | ||
46 | } else { | ||
47 | return out, err | ||
48 | } | ||
49 | } | ||
50 | |||
51 | if tok == nil { | ||
52 | break | ||
53 | } | ||
54 | |||
55 | switch typed := tok.(type) { | ||
56 | case xml.CharData: | ||
57 | out.Text = string(typed.Copy()) | ||
58 | case xml.StartElement: | ||
59 | el := typed.Copy() | ||
60 | out.Attr = el.Attr | ||
61 | if out.Children == nil { | ||
62 | out.Children = map[string][]*XMLNode{} | ||
63 | } | ||
64 | |||
65 | name := typed.Name.Local | ||
66 | slice := out.Children[name] | ||
67 | if slice == nil { | ||
68 | slice = []*XMLNode{} | ||
69 | } | ||
70 | node, e := XMLToStruct(d, &el) | ||
71 | out.findNamespaces() | ||
72 | if e != nil { | ||
73 | return out, e | ||
74 | } | ||
75 | node.Name = typed.Name | ||
76 | node.findNamespaces() | ||
77 | tempOut := *out | ||
78 | // Save into a temp variable, simply because out gets squashed during | ||
79 | // loop iterations | ||
80 | node.parent = &tempOut | ||
81 | slice = append(slice, node) | ||
82 | out.Children[name] = slice | ||
83 | case xml.EndElement: | ||
84 | if s != nil && s.Name.Local == typed.Name.Local { // matching end token | ||
85 | return out, nil | ||
86 | } | ||
87 | out = &XMLNode{} | ||
88 | } | ||
89 | } | ||
90 | return out, nil | ||
91 | } | ||
92 | |||
93 | func (n *XMLNode) findNamespaces() { | ||
94 | ns := map[string]string{} | ||
95 | for _, a := range n.Attr { | ||
96 | if a.Name.Space == "xmlns" { | ||
97 | ns[a.Value] = a.Name.Local | ||
98 | } | ||
99 | } | ||
100 | |||
101 | n.namespaces = ns | ||
102 | } | ||
103 | |||
104 | func (n *XMLNode) findElem(name string) (string, bool) { | ||
105 | for node := n; node != nil; node = node.parent { | ||
106 | for _, a := range node.Attr { | ||
107 | namespace := a.Name.Space | ||
108 | if v, ok := node.namespaces[namespace]; ok { | ||
109 | namespace = v | ||
110 | } | ||
111 | if name == fmt.Sprintf("%s:%s", namespace, a.Name.Local) { | ||
112 | return a.Value, true | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | return "", false | ||
117 | } | ||
118 | |||
119 | // StructToXML writes an XMLNode to a xml.Encoder as tokens. | ||
120 | func StructToXML(e *xml.Encoder, node *XMLNode, sorted bool) error { | ||
121 | e.EncodeToken(xml.StartElement{Name: node.Name, Attr: node.Attr}) | ||
122 | |||
123 | if node.Text != "" { | ||
124 | e.EncodeToken(xml.CharData([]byte(node.Text))) | ||
125 | } else if sorted { | ||
126 | sortedNames := []string{} | ||
127 | for k := range node.Children { | ||
128 | sortedNames = append(sortedNames, k) | ||
129 | } | ||
130 | sort.Strings(sortedNames) | ||
131 | |||
132 | for _, k := range sortedNames { | ||
133 | for _, v := range node.Children[k] { | ||
134 | StructToXML(e, v, sorted) | ||
135 | } | ||
136 | } | ||
137 | } else { | ||
138 | for _, c := range node.Children { | ||
139 | for _, v := range c { | ||
140 | StructToXML(e, v, sorted) | ||
141 | } | ||
142 | } | ||
143 | } | ||
144 | |||
145 | e.EncodeToken(xml.EndElement{Name: node.Name}) | ||
146 | return e.Flush() | ||
147 | } | ||