step_initialize.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Code generated by smithy-go/middleware/generate.go DO NOT EDIT.
  2. package middleware
  3. import (
  4. "context"
  5. "fmt"
  6. )
  7. // InitializeInput wraps the input parameters for the InitializeMiddlewares to
  8. // consume. InitializeMiddleware may modify the parameter value before
  9. // forwarding it along to the next InitializeHandler.
  10. type InitializeInput struct {
  11. Parameters interface{}
  12. }
  13. // InitializeOutput provides the result returned by the next InitializeHandler.
  14. type InitializeOutput struct {
  15. Result interface{}
  16. }
  17. // InitializeHandler provides the interface for the next handler the
  18. // InitializeMiddleware will call in the middleware chain.
  19. type InitializeHandler interface {
  20. HandleInitialize(ctx context.Context, in InitializeInput) (
  21. out InitializeOutput, metadata Metadata, err error,
  22. )
  23. }
  24. // InitializeMiddleware provides the interface for middleware specific to the
  25. // initialize step. Delegates to the next InitializeHandler for further
  26. // processing.
  27. type InitializeMiddleware interface {
  28. // ID returns a unique ID for the middleware in the InitializeStep. The step does not
  29. // allow duplicate IDs.
  30. ID() string
  31. // HandleInitialize 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. HandleInitialize(ctx context.Context, in InitializeInput, next InitializeHandler) (
  35. out InitializeOutput, metadata Metadata, err error,
  36. )
  37. }
  38. // InitializeMiddlewareFunc returns a InitializeMiddleware with the unique ID provided,
  39. // and the func to be invoked.
  40. func InitializeMiddlewareFunc(id string, fn func(context.Context, InitializeInput, InitializeHandler) (InitializeOutput, Metadata, error)) InitializeMiddleware {
  41. return initializeMiddlewareFunc{
  42. id: id,
  43. fn: fn,
  44. }
  45. }
  46. type initializeMiddlewareFunc struct {
  47. // Unique ID for the middleware.
  48. id string
  49. // Middleware function to be called.
  50. fn func(context.Context, InitializeInput, InitializeHandler) (
  51. InitializeOutput, Metadata, error,
  52. )
  53. }
  54. // ID returns the unique ID for the middleware.
  55. func (s initializeMiddlewareFunc) ID() string { return s.id }
  56. // HandleInitialize invokes the middleware Fn.
  57. func (s initializeMiddlewareFunc) HandleInitialize(ctx context.Context, in InitializeInput, next InitializeHandler) (
  58. out InitializeOutput, metadata Metadata, err error,
  59. ) {
  60. return s.fn(ctx, in, next)
  61. }
  62. var _ InitializeMiddleware = (initializeMiddlewareFunc{})
  63. // InitializeStep provides the ordered grouping of InitializeMiddleware to be
  64. // invoked on a handler.
  65. type InitializeStep struct {
  66. head *decoratedInitializeHandler
  67. tail *decoratedInitializeHandler
  68. }
  69. // NewInitializeStep returns an InitializeStep ready to have middleware for
  70. // initialize added to it.
  71. func NewInitializeStep() *InitializeStep {
  72. return &InitializeStep{}
  73. }
  74. var _ Middleware = (*InitializeStep)(nil)
  75. // ID returns the unique ID of the step as a middleware.
  76. func (s *InitializeStep) ID() string {
  77. return "Initialize 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 *InitializeStep) HandleMiddleware(ctx context.Context, in interface{}, next Handler) (
  84. out interface{}, metadata Metadata, err error,
  85. ) {
  86. sIn := InitializeInput{
  87. Parameters: in,
  88. }
  89. wh := &initializeWrapHandler{next}
  90. if s.head == nil {
  91. res, metadata, err := wh.HandleInitialize(ctx, sIn)
  92. return res.Result, metadata, err
  93. }
  94. s.tail.Next = wh
  95. res, metadata, err := s.head.HandleInitialize(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 *InitializeStep) Get(id string) (InitializeMiddleware, 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 *InitializeStep) Add(m InitializeMiddleware, pos RelativePosition) error {
  112. if s.head == nil {
  113. s.head = &decoratedInitializeHandler{nil, m}
  114. s.tail = s.head
  115. return nil
  116. }
  117. if pos == Before {
  118. s.head = &decoratedInitializeHandler{s.head, m}
  119. } else {
  120. tail := &decoratedInitializeHandler{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 *InitializeStep) Insert(m InitializeMiddleware, 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 = &decoratedInitializeHandler{s.head, m}
  137. } else { // somewhere in the middle
  138. prev.Next = &decoratedInitializeHandler{found, m}
  139. }
  140. } else {
  141. if found.Next == nil { // at the end
  142. tail := &decoratedInitializeHandler{nil, m}
  143. s.tail.Next = tail
  144. s.tail = tail
  145. } else { // somewhere in the middle
  146. found.Next = &decoratedInitializeHandler{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 *InitializeStep) Swap(id string, m InitializeMiddleware) (InitializeMiddleware, 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 *InitializeStep) Remove(id string) (InitializeMiddleware, 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.(*decoratedInitializeHandler)
  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 *InitializeStep) 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. // *initializeWrapHandler, make sure to check for that
  193. if hnext, ok := h.Next.(*decoratedInitializeHandler); 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 *InitializeStep) Clear() {
  203. s.head = nil
  204. s.tail = nil
  205. }
  206. func (s *InitializeStep) get(id string) (found, prev *decoratedInitializeHandler) {
  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. // *initializeWrapHandler
  218. h, _ = h.Next.(*decoratedInitializeHandler)
  219. }
  220. return
  221. }
  222. type initializeWrapHandler struct {
  223. Next Handler
  224. }
  225. var _ InitializeHandler = (*initializeWrapHandler)(nil)
  226. // HandleInitialize implements InitializeHandler, converts types and delegates to underlying
  227. // generic handler.
  228. func (w initializeWrapHandler) HandleInitialize(ctx context.Context, in InitializeInput) (
  229. out InitializeOutput, metadata Metadata, err error,
  230. ) {
  231. res, metadata, err := w.Next.Handle(ctx, in.Parameters)
  232. return InitializeOutput{
  233. Result: res,
  234. }, metadata, err
  235. }
  236. type decoratedInitializeHandler struct {
  237. Next InitializeHandler
  238. With InitializeMiddleware
  239. }
  240. var _ InitializeHandler = (*decoratedInitializeHandler)(nil)
  241. func (h decoratedInitializeHandler) HandleInitialize(ctx context.Context, in InitializeInput) (
  242. out InitializeOutput, metadata Metadata, err error,
  243. ) {
  244. return h.With.HandleInitialize(ctx, in, h.Next)
  245. }
  246. // InitializeHandlerFunc provides a wrapper around a function to be used as initializeMiddleware.
  247. type InitializeHandlerFunc func(context.Context, InitializeInput) (InitializeOutput, Metadata, error)
  248. // HandleInitialize calls the wrapped function with the provided arguments.
  249. func (f InitializeHandlerFunc) HandleInitialize(ctx context.Context, in InitializeInput) (InitializeOutput, Metadata, error) {
  250. return f(ctx, in)
  251. }
  252. var _ InitializeHandler = InitializeHandlerFunc(nil)