aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/aws/smithy-go/middleware/step_serialize.go
blob: 114bafcedea8f5db7a236334b0f0cac419d363c8 (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package middleware

import "context"

// SerializeInput provides the input parameters for the SerializeMiddleware to
// consume. SerializeMiddleware may modify the Request value before forwarding
// SerializeInput along to the next SerializeHandler. The Parameters member
// should not be modified by SerializeMiddleware, InitializeMiddleware should
// be responsible for modifying the provided Parameter value.
type SerializeInput struct {
	Parameters interface{}
	Request    interface{}
}

// SerializeOutput provides the result returned by the next SerializeHandler.
type SerializeOutput struct {
	Result interface{}
}

// SerializeHandler provides the interface for the next handler the
// SerializeMiddleware will call in the middleware chain.
type SerializeHandler interface {
	HandleSerialize(ctx context.Context, in SerializeInput) (
		out SerializeOutput, metadata Metadata, err error,
	)
}

// SerializeMiddleware provides the interface for middleware specific to the
// serialize step. Delegates to the next SerializeHandler for further
// processing.
type SerializeMiddleware interface {
	// ID returns a unique ID for the middleware in the SerializeStep. The step does not
	// allow duplicate IDs.
	ID() string

	// HandleSerialize invokes the middleware behavior which must delegate to the next handler
	// for the middleware chain to continue. The method must return a result or
	// error to its caller.
	HandleSerialize(ctx context.Context, in SerializeInput, next SerializeHandler) (
		out SerializeOutput, metadata Metadata, err error,
	)
}

// SerializeMiddlewareFunc returns a SerializeMiddleware with the unique ID
// provided, and the func to be invoked.
func SerializeMiddlewareFunc(id string, fn func(context.Context, SerializeInput, SerializeHandler) (SerializeOutput, Metadata, error)) SerializeMiddleware {
	return serializeMiddlewareFunc{
		id: id,
		fn: fn,
	}
}

type serializeMiddlewareFunc struct {
	// Unique ID for the middleware.
	id string

	// Middleware function to be called.
	fn func(context.Context, SerializeInput, SerializeHandler) (
		SerializeOutput, Metadata, error,
	)
}

// ID returns the unique ID for the middleware.
func (s serializeMiddlewareFunc) ID() string { return s.id }

// HandleSerialize invokes the middleware Fn.
func (s serializeMiddlewareFunc) HandleSerialize(ctx context.Context, in SerializeInput, next SerializeHandler) (
	out SerializeOutput, metadata Metadata, err error,
) {
	return s.fn(ctx, in, next)
}

var _ SerializeMiddleware = (serializeMiddlewareFunc{})

// SerializeStep provides the ordered grouping of SerializeMiddleware to be
// invoked on a handler.
type SerializeStep struct {
	newRequest func() interface{}
	ids        *orderedIDs
}

// NewSerializeStep returns a SerializeStep ready to have middleware for
// initialization added to it. The newRequest func parameter is used to
// initialize the transport specific request for the stack SerializeStep to
// serialize the input parameters into.
func NewSerializeStep(newRequest func() interface{}) *SerializeStep {
	return &SerializeStep{
		ids:        newOrderedIDs(),
		newRequest: newRequest,
	}
}

var _ Middleware = (*SerializeStep)(nil)

// ID returns the unique ID of the step as a middleware.
func (s *SerializeStep) ID() string {
	return "Serialize stack step"
}

// HandleMiddleware invokes the middleware by decorating the next handler
// provided. Returns the result of the middleware and handler being invoked.
//
// Implements Middleware interface.
func (s *SerializeStep) HandleMiddleware(ctx context.Context, in interface{}, next Handler) (
	out interface{}, metadata Metadata, err error,
) {
	order := s.ids.GetOrder()

	var h SerializeHandler = serializeWrapHandler{Next: next}
	for i := len(order) - 1; i >= 0; i-- {
		h = decoratedSerializeHandler{
			Next: h,
			With: order[i].(SerializeMiddleware),
		}
	}

	sIn := SerializeInput{
		Parameters: in,
		Request:    s.newRequest(),
	}

	res, metadata, err := h.HandleSerialize(ctx, sIn)
	return res.Result, metadata, err
}

// Get retrieves the middleware identified by id. If the middleware is not present, returns false.
func (s *SerializeStep) Get(id string) (SerializeMiddleware, bool) {
	get, ok := s.ids.Get(id)
	if !ok {
		return nil, false
	}
	return get.(SerializeMiddleware), ok
}

// Add injects the middleware to the relative position of the middleware group.
// Returns an error if the middleware already exists.
func (s *SerializeStep) Add(m SerializeMiddleware, pos RelativePosition) error {
	return s.ids.Add(m, pos)
}

// Insert injects the middleware relative to an existing middleware ID.
// Returns error if the original middleware does not exist, or the middleware
// being added already exists.
func (s *SerializeStep) Insert(m SerializeMiddleware, relativeTo string, pos RelativePosition) error {
	return s.ids.Insert(m, relativeTo, pos)
}

// Swap removes the middleware by id, replacing it with the new middleware.
// Returns the middleware removed, or error if the middleware to be removed
// doesn't exist.
func (s *SerializeStep) Swap(id string, m SerializeMiddleware) (SerializeMiddleware, error) {
	removed, err := s.ids.Swap(id, m)
	if err != nil {
		return nil, err
	}

	return removed.(SerializeMiddleware), nil
}

// Remove removes the middleware by id. Returns error if the middleware
// doesn't exist.
func (s *SerializeStep) Remove(id string) (SerializeMiddleware, error) {
	removed, err := s.ids.Remove(id)
	if err != nil {
		return nil, err
	}

	return removed.(SerializeMiddleware), nil
}

// List returns a list of the middleware in the step.
func (s *SerializeStep) List() []string {
	return s.ids.List()
}

// Clear removes all middleware in the step.
func (s *SerializeStep) Clear() {
	s.ids.Clear()
}

type serializeWrapHandler struct {
	Next Handler
}

var _ SerializeHandler = (*serializeWrapHandler)(nil)

// Implements SerializeHandler, converts types and delegates to underlying
// generic handler.
func (w serializeWrapHandler) HandleSerialize(ctx context.Context, in SerializeInput) (
	out SerializeOutput, metadata Metadata, err error,
) {
	res, metadata, err := w.Next.Handle(ctx, in.Request)
	return SerializeOutput{
		Result: res,
	}, metadata, err
}

type decoratedSerializeHandler struct {
	Next SerializeHandler
	With SerializeMiddleware
}

var _ SerializeHandler = (*decoratedSerializeHandler)(nil)

func (h decoratedSerializeHandler) HandleSerialize(ctx context.Context, in SerializeInput) (
	out SerializeOutput, metadata Metadata, err error,
) {
	return h.With.HandleSerialize(ctx, in, h.Next)
}

// SerializeHandlerFunc provides a wrapper around a function to be used as a serialize middleware handler.
type SerializeHandlerFunc func(context.Context, SerializeInput) (SerializeOutput, Metadata, error)

// HandleSerialize calls the wrapped function with the provided arguments.
func (s SerializeHandlerFunc) HandleSerialize(ctx context.Context, in SerializeInput) (SerializeOutput, Metadata, error) {
	return s(ctx, in)
}

var _ SerializeHandler = SerializeHandlerFunc(nil)