step_serialize.go 8.7 KB

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