step_build.go 7.9 KB

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