src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

step_serialize.go (8884B)


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