step_deserialize.go raw

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