forked from aws/aws-sdk-go-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
165 lines (143 loc) · 4.16 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package restjson
import (
"fmt"
"io"
"net/http"
"github.com/aws/aws-sdk-go-v2/private/protocol"
"github.com/aws/aws-sdk-go-v2/private/protocol/json"
"github.com/aws/aws-sdk-go-v2/private/protocol/rest"
)
// An Encoder provides encoding of the AWS RESTJSON protocol. This encoder combindes
// the JSON and REST encoders deligating to them for their associated targets.
//
// It is invalid to set a JSON and stream payload on the same encoder.
type Encoder struct {
method string
reqEncoder *rest.Encoder
bodyEncoder *json.Encoder
t protocol.Target
err error
}
// NewEncoder creates a new encoder for encoding the AWS RESTJSON protocol.
// The request passed in will be the base the path, query, and headers encoded
// will be set on top of.
func NewEncoder(req *http.Request) *Encoder {
e := &Encoder{
method: req.Method,
reqEncoder: rest.NewEncoder(req),
bodyEncoder: json.NewEncoder(),
}
return e
}
// Encode returns the encoded request, and body payload. If no payload body was
// set nil will be returned. If an error occurred while encoding the API an
// error will be returned.
func (e *Encoder) Encode() (*http.Request, io.ReadSeeker, error) {
req, payloadBody, err := e.reqEncoder.Encode()
if err != nil {
return nil, nil, err
}
jsonBody, err := e.bodyEncoder.Encode()
if err != nil {
return nil, nil, err
}
havePayload := payloadBody != nil
haveJSON := jsonBody != nil
if havePayload == haveJSON && haveJSON {
return nil, nil, fmt.Errorf("unexpected JSON body and request payload for AWSMarshaler")
}
body := payloadBody
if body == nil {
body = jsonBody
}
return req, body, err
}
// SetValue will set a value to the header, path, query, or body.
//
// If the request's method is GET all BodyTarget values will be written to
// the query string.
func (e *Encoder) SetValue(t protocol.Target, k string, v protocol.ValueMarshaler, meta protocol.Metadata) {
if e.err != nil {
return
}
switch t {
case protocol.PathTarget:
fallthrough
case protocol.QueryTarget:
fallthrough
case protocol.HeaderTarget:
e.reqEncoder.SetValue(t, k, v, meta)
case protocol.BodyTarget:
fallthrough
case protocol.PayloadTarget:
if e.method == "GET" {
e.reqEncoder.SetValue(t, k, v, meta)
} else {
e.bodyEncoder.SetValue(t, k, v, meta)
}
default:
e.err = fmt.Errorf("unknown SetValue restjson encode target, %s, %s", t, k)
}
}
// SetStream will set the stream to the payload of the request.
func (e *Encoder) SetStream(t protocol.Target, k string, v protocol.StreamMarshaler, meta protocol.Metadata) {
if e.err != nil {
return
}
switch t {
case protocol.PayloadTarget:
e.reqEncoder.SetStream(t, k, v, meta)
default:
e.err = fmt.Errorf("invalid target %s, for SetStream, must be PayloadTarget", t)
}
}
// List will return the proper list encoder based on the given protocol.Target.
func (e *Encoder) List(t protocol.Target, k string, meta protocol.Metadata) protocol.ListEncoder {
if e.err != nil {
return nil
}
e.t = t
switch t {
case protocol.HeaderTarget:
fallthrough
case protocol.QueryTarget:
return e.reqEncoder.List(t, k, meta)
case protocol.BodyTarget:
return e.bodyEncoder.List(t, k, meta)
default:
e.err = fmt.Errorf("unknown SetList restjson encode target, %s, %s", t, k)
return nil
}
}
// Map will return the proper map encoder based on the given protocol Target.
func (e *Encoder) Map(t protocol.Target, k string, meta protocol.Metadata) protocol.MapEncoder {
if e.err != nil {
return nil
}
e.t = t
switch t {
case protocol.QueryTarget:
fallthrough
case protocol.HeadersTarget:
return e.reqEncoder.Map(t, k, meta)
case protocol.BodyTarget:
return e.bodyEncoder.Map(t, k, meta)
default:
e.err = fmt.Errorf("unknown SetMap restjson encode target, %s, %s", t, k)
return nil
}
}
// SetFields will set the nested type's fields to the body.
func (e *Encoder) SetFields(t protocol.Target, k string, m protocol.FieldMarshaler, meta protocol.Metadata) {
if e.err != nil {
return
}
switch t {
case protocol.PayloadTarget:
fallthrough
case protocol.BodyTarget:
e.bodyEncoder.SetFields(t, k, m, meta)
default:
e.err = fmt.Errorf("unknown SetMarshaler restjson encode target, %s, %s", t, k)
}
}