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