diff options
author | Jake Champlin <jake.champlin.27@gmail.com> | 2017-06-06 12:40:07 -0400 |
---|---|---|
committer | Jake Champlin <jake.champlin.27@gmail.com> | 2017-06-06 12:40:07 -0400 |
commit | bae9f6d2fd5eb5bc80929bd393932b23f14d7c93 (patch) | |
tree | ca9ab12a7d78b1fc27a8f734729081357ce6d252 /vendor/github.com/aws/aws-sdk-go/private/protocol/rest/unmarshal.go | |
parent | 254c495b6bebab3fb72a243c4bce858d79e6ee99 (diff) | |
download | terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.tar.gz terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.tar.zst terraform-provider-statuscake-bae9f6d2fd5eb5bc80929bd393932b23f14d7c93.zip |
Initial transfer of provider code
Diffstat (limited to 'vendor/github.com/aws/aws-sdk-go/private/protocol/rest/unmarshal.go')
-rw-r--r-- | vendor/github.com/aws/aws-sdk-go/private/protocol/rest/unmarshal.go | 227 |
1 files changed, 227 insertions, 0 deletions
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 | } | ||