step_deserialize.go 8.6 KB

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