1 // Code generated by smithy-go/middleware/generate.go DO NOT EDIT.
2 package middleware
3 4 import (
5 "context"
6 "fmt"
7 )
8 9 // FinalizeInput provides the input parameters for the FinalizeMiddleware to
10 // consume. FinalizeMiddleware may modify the Request value before forwarding
11 // the FinalizeInput along to the next next FinalizeHandler.
12 type FinalizeInput struct {
13 Request interface{}
14 }
15 16 // FinalizeOutput provides the result returned by the next FinalizeHandler.
17 type FinalizeOutput struct {
18 Result interface{}
19 }
20 21 // FinalizeHandler provides the interface for the next handler the
22 // FinalizeMiddleware will call in the middleware chain.
23 type FinalizeHandler interface {
24 HandleFinalize(ctx context.Context, in FinalizeInput) (
25 out FinalizeOutput, metadata Metadata, err error,
26 )
27 }
28 29 // FinalizeMiddleware provides the interface for middleware specific to the
30 // finalize step. Delegates to the next FinalizeHandler for further
31 // processing.
32 type FinalizeMiddleware interface {
33 // ID returns a unique ID for the middleware in the FinalizeStep. The step does not
34 // allow duplicate IDs.
35 ID() string
36 37 // HandleFinalize invokes the middleware behavior which must delegate to the next handler
38 // for the middleware chain to continue. The method must return a result or
39 // error to its caller.
40 HandleFinalize(ctx context.Context, in FinalizeInput, next FinalizeHandler) (
41 out FinalizeOutput, metadata Metadata, err error,
42 )
43 }
44 45 // FinalizeMiddlewareFunc returns a FinalizeMiddleware with the unique ID provided,
46 // and the func to be invoked.
47 func FinalizeMiddlewareFunc(id string, fn func(context.Context, FinalizeInput, FinalizeHandler) (FinalizeOutput, Metadata, error)) FinalizeMiddleware {
48 return finalizeMiddlewareFunc{
49 id: id,
50 fn: fn,
51 }
52 }
53 54 type finalizeMiddlewareFunc struct {
55 // Unique ID for the middleware.
56 id string
57 58 // Middleware function to be called.
59 fn func(context.Context, FinalizeInput, FinalizeHandler) (
60 FinalizeOutput, Metadata, error,
61 )
62 }
63 64 // ID returns the unique ID for the middleware.
65 func (s finalizeMiddlewareFunc) ID() string { return s.id }
66 67 // HandleFinalize invokes the middleware Fn.
68 func (s finalizeMiddlewareFunc) HandleFinalize(ctx context.Context, in FinalizeInput, next FinalizeHandler) (
69 out FinalizeOutput, metadata Metadata, err error,
70 ) {
71 return s.fn(ctx, in, next)
72 }
73 74 var _ FinalizeMiddleware = (finalizeMiddlewareFunc{})
75 76 // FinalizeStep provides the ordered grouping of FinalizeMiddleware to be
77 // invoked on a handler.
78 type FinalizeStep struct {
79 head *decoratedFinalizeHandler
80 tail *decoratedFinalizeHandler
81 }
82 83 // NewFinalizeStep returns an FinalizeStep ready to have middleware for
84 // finalize added to it.
85 func NewFinalizeStep() *FinalizeStep {
86 return &FinalizeStep{}
87 }
88 89 var _ Middleware = (*FinalizeStep)(nil)
90 91 // ID returns the unique ID of the step as a middleware.
92 func (s *FinalizeStep) ID() string {
93 return "Finalize stack step"
94 }
95 96 // HandleMiddleware invokes the middleware by decorating the next handler
97 // provided. Returns the result of the middleware and handler being invoked.
98 //
99 // Implements Middleware interface.
100 func (s *FinalizeStep) HandleMiddleware(ctx context.Context, in interface{}, next Handler) (
101 out interface{}, metadata Metadata, err error,
102 ) {
103 sIn := FinalizeInput{
104 Request: in,
105 }
106 107 wh := &finalizeWrapHandler{next}
108 if s.head == nil {
109 res, metadata, err := wh.HandleFinalize(ctx, sIn)
110 return res.Result, metadata, err
111 }
112 113 s.tail.Next = wh
114 res, metadata, err := s.head.HandleFinalize(ctx, sIn)
115 return res.Result, metadata, err
116 }
117 118 // Get retrieves the middleware identified by id. If the middleware is not present, returns false.
119 func (s *FinalizeStep) Get(id string) (FinalizeMiddleware, bool) {
120 found, _ := s.get(id)
121 if found == nil {
122 return nil, false
123 }
124 125 return found.With, true
126 }
127 128 // Add injects the middleware to the relative position of the middleware group.
129 //
130 // Add never returns an error. It used to for duplicate phases but this
131 // behavior has since been removed as part of a performance optimization. The
132 // return value from Add can be ignored.
133 func (s *FinalizeStep) Add(m FinalizeMiddleware, pos RelativePosition) error {
134 if s.head == nil {
135 s.head = &decoratedFinalizeHandler{nil, m}
136 s.tail = s.head
137 return nil
138 }
139 140 if pos == Before {
141 s.head = &decoratedFinalizeHandler{s.head, m}
142 } else {
143 tail := &decoratedFinalizeHandler{nil, m}
144 s.tail.Next = tail
145 s.tail = tail
146 }
147 148 return nil
149 }
150 151 // Insert injects the middleware relative to an existing middleware ID.
152 // Returns error if the original middleware does not exist, or the middleware
153 // being added already exists.
154 func (s *FinalizeStep) Insert(m FinalizeMiddleware, relativeTo string, pos RelativePosition) error {
155 found, prev := s.get(relativeTo)
156 if found == nil {
157 return fmt.Errorf("not found: %s", m.ID())
158 }
159 160 if pos == Before {
161 if prev == nil { // at the front
162 s.head = &decoratedFinalizeHandler{s.head, m}
163 } else { // somewhere in the middle
164 prev.Next = &decoratedFinalizeHandler{found, m}
165 }
166 } else {
167 if found.Next == nil { // at the end
168 tail := &decoratedFinalizeHandler{nil, m}
169 s.tail.Next = tail
170 s.tail = tail
171 } else { // somewhere in the middle
172 found.Next = &decoratedFinalizeHandler{found.Next, m}
173 }
174 }
175 176 return nil
177 }
178 179 // Swap removes the middleware by id, replacing it with the new middleware.
180 // Returns the middleware removed, or error if the middleware to be removed
181 // doesn't exist.
182 func (s *FinalizeStep) Swap(id string, m FinalizeMiddleware) (FinalizeMiddleware, error) {
183 found, _ := s.get(id)
184 if found == nil {
185 return nil, fmt.Errorf("not found: %s", m.ID())
186 }
187 188 swapped := found.With
189 found.With = m
190 return swapped, nil
191 }
192 193 // Remove removes the middleware by id. Returns error if the middleware
194 // doesn't exist.
195 func (s *FinalizeStep) Remove(id string) (FinalizeMiddleware, error) {
196 found, prev := s.get(id)
197 if found == nil {
198 return nil, fmt.Errorf("not found: %s", id)
199 }
200 201 if s.head == s.tail { // it's the only one
202 s.head = nil
203 s.tail = nil
204 } else if found == s.head { // at the front
205 s.head = s.head.Next.(*decoratedFinalizeHandler)
206 } else if found == s.tail { // at the end
207 prev.Next = nil
208 s.tail = prev
209 } else {
210 prev.Next = found.Next // somewhere in the middle
211 }
212 213 return found.With, nil
214 }
215 216 // List returns a list of the middleware in the step.
217 func (s *FinalizeStep) List() []string {
218 var ids []string
219 for h := s.head; h != nil; {
220 ids = append(ids, h.With.ID())
221 if h.Next == nil {
222 break
223 }
224 225 // once executed, tail.Next of the list will be set to an
226 // *finalizeWrapHandler, make sure to check for that
227 if hnext, ok := h.Next.(*decoratedFinalizeHandler); ok {
228 h = hnext
229 } else {
230 break
231 }
232 }
233 return ids
234 }
235 236 // Clear removes all middleware in the step.
237 func (s *FinalizeStep) Clear() {
238 s.head = nil
239 s.tail = nil
240 }
241 242 func (s *FinalizeStep) get(id string) (found, prev *decoratedFinalizeHandler) {
243 for h := s.head; h != nil; {
244 if h.With.ID() == id {
245 found = h
246 return
247 }
248 prev = h
249 if h.Next == nil {
250 return
251 }
252 253 // once executed, tail.Next of the list will be set to an
254 // *finalizeWrapHandler
255 h, _ = h.Next.(*decoratedFinalizeHandler)
256 }
257 return
258 }
259 260 type finalizeWrapHandler struct {
261 Next Handler
262 }
263 264 var _ FinalizeHandler = (*finalizeWrapHandler)(nil)
265 266 // HandleFinalize implements FinalizeHandler, converts types and delegates to underlying
267 // generic handler.
268 func (w finalizeWrapHandler) HandleFinalize(ctx context.Context, in FinalizeInput) (
269 out FinalizeOutput, metadata Metadata, err error,
270 ) {
271 res, metadata, err := w.Next.Handle(ctx, in.Request)
272 return FinalizeOutput{
273 Result: res,
274 }, metadata, err
275 }
276 277 type decoratedFinalizeHandler struct {
278 Next FinalizeHandler
279 With FinalizeMiddleware
280 }
281 282 var _ FinalizeHandler = (*decoratedFinalizeHandler)(nil)
283 284 func (h decoratedFinalizeHandler) HandleFinalize(ctx context.Context, in FinalizeInput) (
285 out FinalizeOutput, metadata Metadata, err error,
286 ) {
287 return h.With.HandleFinalize(ctx, in, h.Next)
288 }
289 290 // FinalizeHandlerFunc provides a wrapper around a function to be used as finalizeMiddleware.
291 type FinalizeHandlerFunc func(context.Context, FinalizeInput) (FinalizeOutput, Metadata, error)
292 293 // HandleFinalize calls the wrapped function with the provided arguments.
294 func (f FinalizeHandlerFunc) HandleFinalize(ctx context.Context, in FinalizeInput) (FinalizeOutput, Metadata, error) {
295 return f(ctx, in)
296 }
297 298 var _ FinalizeHandler = FinalizeHandlerFunc(nil)
299