src

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

step_build.go (8065B)


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