step_initialize.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  // InitializeInput wraps the input parameters for the InitializeMiddlewares to
  10  // consume. InitializeMiddleware may modify the parameter value before
  11  // forwarding it along to the next InitializeHandler.
  12  
  13  type InitializeInput struct {
  14  	Parameters interface{}
  15  }
  16  
  17  // InitializeOutput provides the result returned by the next InitializeHandler.
  18  type InitializeOutput struct {
  19  	Result interface{}
  20  }
  21  
  22  // InitializeHandler provides the interface for the next handler the
  23  // InitializeMiddleware will call in the middleware chain.
  24  type InitializeHandler interface {
  25  	HandleInitialize(ctx context.Context, in InitializeInput) (
  26  		out InitializeOutput, metadata Metadata, err error,
  27  	)
  28  }
  29  
  30  // InitializeMiddleware provides the interface for middleware specific to the
  31  // initialize step. Delegates to the next InitializeHandler for further
  32  // processing.
  33  type InitializeMiddleware interface {
  34  	// ID returns a unique ID for the middleware in the InitializeStep. The step does not
  35  	// allow duplicate IDs.
  36  	ID() string
  37  
  38  	// HandleInitialize 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  	HandleInitialize(ctx context.Context, in InitializeInput, next InitializeHandler) (
  42  		out InitializeOutput, metadata Metadata, err error,
  43  	)
  44  }
  45  
  46  // InitializeMiddlewareFunc returns a InitializeMiddleware with the unique ID provided,
  47  // and the func to be invoked.
  48  func InitializeMiddlewareFunc(id string, fn func(context.Context, InitializeInput, InitializeHandler) (InitializeOutput, Metadata, error)) InitializeMiddleware {
  49  	return initializeMiddlewareFunc{
  50  		id: id,
  51  		fn: fn,
  52  	}
  53  }
  54  
  55  type initializeMiddlewareFunc struct {
  56  	// Unique ID for the middleware.
  57  	id string
  58  
  59  	// Middleware function to be called.
  60  	fn func(context.Context, InitializeInput, InitializeHandler) (
  61  		InitializeOutput, Metadata, error,
  62  	)
  63  }
  64  
  65  // ID returns the unique ID for the middleware.
  66  func (s initializeMiddlewareFunc) ID() string { return s.id }
  67  
  68  // HandleInitialize invokes the middleware Fn.
  69  func (s initializeMiddlewareFunc) HandleInitialize(ctx context.Context, in InitializeInput, next InitializeHandler) (
  70  	out InitializeOutput, metadata Metadata, err error,
  71  ) {
  72  	return s.fn(ctx, in, next)
  73  }
  74  
  75  var _ InitializeMiddleware = (initializeMiddlewareFunc{})
  76  
  77  // InitializeStep provides the ordered grouping of InitializeMiddleware to be
  78  // invoked on a handler.
  79  type InitializeStep struct {
  80  	head *decoratedInitializeHandler
  81  	tail *decoratedInitializeHandler
  82  }
  83  
  84  // NewInitializeStep returns an InitializeStep ready to have middleware for
  85  // initialize added to it.
  86  func NewInitializeStep() *InitializeStep {
  87  	return &InitializeStep{}
  88  }
  89  
  90  var _ Middleware = (*InitializeStep)(nil)
  91  
  92  // ID returns the unique ID of the step as a middleware.
  93  func (s *InitializeStep) ID() string {
  94  	return "Initialize 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 *InitializeStep) HandleMiddleware(ctx context.Context, in interface{}, next Handler) (
 102  	out interface{}, metadata Metadata, err error,
 103  ) {
 104  	sIn := InitializeInput{
 105  		Parameters: in,
 106  	}
 107  
 108  	wh := &initializeWrapHandler{next}
 109  	if s.head == nil {
 110  		res, metadata, err := wh.HandleInitialize(ctx, sIn)
 111  		return res.Result, metadata, err
 112  	}
 113  
 114  	s.tail.Next = wh
 115  	res, metadata, err := s.head.HandleInitialize(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 *InitializeStep) Get(id string) (InitializeMiddleware, 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 *InitializeStep) Add(m InitializeMiddleware, pos RelativePosition) error {
 135  	if s.head == nil {
 136  		s.head = &decoratedInitializeHandler{nil, m}
 137  		s.tail = s.head
 138  		return nil
 139  	}
 140  
 141  	if pos == Before {
 142  		s.head = &decoratedInitializeHandler{s.head, m}
 143  	} else {
 144  		tail := &decoratedInitializeHandler{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 *InitializeStep) Insert(m InitializeMiddleware, 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 = &decoratedInitializeHandler{s.head, m}
 164  		} else { // somewhere in the middle
 165  			prev.Next = &decoratedInitializeHandler{found, m}
 166  		}
 167  	} else {
 168  		if found.Next == nil { // at the end
 169  			tail := &decoratedInitializeHandler{nil, m}
 170  			s.tail.Next = tail
 171  			s.tail = tail
 172  		} else { // somewhere in the middle
 173  			found.Next = &decoratedInitializeHandler{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 *InitializeStep) Swap(id string, m InitializeMiddleware) (InitializeMiddleware, 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 *InitializeStep) Remove(id string) (InitializeMiddleware, 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.(*decoratedInitializeHandler)
 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 *InitializeStep) 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  		// *initializeWrapHandler, make sure to check for that
 228  		if hnext, ok := h.Next.(*decoratedInitializeHandler); 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 *InitializeStep) Clear() {
 239  	s.head = nil
 240  	s.tail = nil
 241  }
 242  
 243  func (s *InitializeStep) get(id string) (found, prev *decoratedInitializeHandler) {
 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  		// *initializeWrapHandler
 256  		h, _ = h.Next.(*decoratedInitializeHandler)
 257  	}
 258  	return
 259  }
 260  
 261  type initializeWrapHandler struct {
 262  	Next Handler
 263  }
 264  
 265  var _ InitializeHandler = (*initializeWrapHandler)(nil)
 266  
 267  // HandleInitialize implements InitializeHandler, converts types and delegates to underlying
 268  // generic handler.
 269  func (w initializeWrapHandler) HandleInitialize(ctx context.Context, in InitializeInput) (
 270  	out InitializeOutput, metadata Metadata, err error,
 271  ) {
 272  	res, metadata, err := w.Next.Handle(ctx, in.Parameters)
 273  	return InitializeOutput{
 274  		Result: res,
 275  	}, metadata, err
 276  }
 277  
 278  type decoratedInitializeHandler struct {
 279  	Next InitializeHandler
 280  	With InitializeMiddleware
 281  }
 282  
 283  var _ InitializeHandler = (*decoratedInitializeHandler)(nil)
 284  
 285  func (h decoratedInitializeHandler) HandleInitialize(ctx context.Context, in InitializeInput) (
 286  	out InitializeOutput, metadata Metadata, err error,
 287  ) {
 288  	return h.With.HandleInitialize(ctx, in, h.Next)
 289  }
 290  
 291  // InitializeHandlerFunc provides a wrapper around a function to be used as initializeMiddleware.
 292  type InitializeHandlerFunc func(context.Context, InitializeInput) (InitializeOutput, Metadata, error)
 293  
 294  // HandleInitialize calls the wrapped function with the provided arguments.
 295  func (f InitializeHandlerFunc) HandleInitialize(ctx context.Context, in InitializeInput) (InitializeOutput, Metadata, error) {
 296  	return f(ctx, in)
 297  }
 298  
 299  var _ InitializeHandler = InitializeHandlerFunc(nil)
 300