step_deserialize.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package middleware
  2. import (
  3. "context"
  4. )
  5. // DeserializeInput provides the input parameters for the DeserializeInput to
  6. // consume. DeserializeMiddleware should not modify the Request, and instead
  7. // forward it along to the next DeserializeHandler.
  8. type DeserializeInput struct {
  9. Request interface{}
  10. }
  11. // DeserializeOutput provides the result returned by the next
  12. // DeserializeHandler. The DeserializeMiddleware should deserialize the
  13. // RawResponse into a Result that can be consumed by middleware higher up in
  14. // the stack.
  15. type DeserializeOutput struct {
  16. RawResponse interface{}
  17. Result interface{}
  18. }
  19. // DeserializeHandler provides the interface for the next handler the
  20. // DeserializeMiddleware will call in the middleware chain.
  21. type DeserializeHandler interface {
  22. HandleDeserialize(ctx context.Context, in DeserializeInput) (
  23. out DeserializeOutput, metadata Metadata, err error,
  24. )
  25. }
  26. // DeserializeMiddleware provides the interface for middleware specific to the
  27. // serialize step. Delegates to the next DeserializeHandler for further
  28. // processing.
  29. type DeserializeMiddleware interface {
  30. // ID returns a unique ID for the middleware in the DeserializeStep. The step does not
  31. // allow duplicate IDs.
  32. ID() string
  33. // HandleDeserialize invokes the middleware behavior which must delegate to the next handler
  34. // for the middleware chain to continue. The method must return a result or
  35. // error to its caller.
  36. HandleDeserialize(ctx context.Context, in DeserializeInput, next DeserializeHandler) (
  37. out DeserializeOutput, metadata Metadata, err error,
  38. )
  39. }
  40. // DeserializeMiddlewareFunc returns a DeserializeMiddleware with the unique ID
  41. // provided, and the func to be invoked.
  42. func DeserializeMiddlewareFunc(id string, fn func(context.Context, DeserializeInput, DeserializeHandler) (DeserializeOutput, Metadata, error)) DeserializeMiddleware {
  43. return deserializeMiddlewareFunc{
  44. id: id,
  45. fn: fn,
  46. }
  47. }
  48. type deserializeMiddlewareFunc struct {
  49. // Unique ID for the middleware.
  50. id string
  51. // Middleware function to be called.
  52. fn func(context.Context, DeserializeInput, DeserializeHandler) (
  53. DeserializeOutput, Metadata, error,
  54. )
  55. }
  56. // ID returns the unique ID for the middleware.
  57. func (s deserializeMiddlewareFunc) ID() string { return s.id }
  58. // HandleDeserialize invokes the middleware Fn.
  59. func (s deserializeMiddlewareFunc) HandleDeserialize(ctx context.Context, in DeserializeInput, next DeserializeHandler) (
  60. out DeserializeOutput, metadata Metadata, err error,
  61. ) {
  62. return s.fn(ctx, in, next)
  63. }
  64. var _ DeserializeMiddleware = (deserializeMiddlewareFunc{})
  65. // DeserializeStep provides the ordered grouping of DeserializeMiddleware to be
  66. // invoked on a handler.
  67. type DeserializeStep struct {
  68. ids *orderedIDs
  69. }
  70. // NewDeserializeStep returns a DeserializeStep ready to have middleware for
  71. // initialization added to it.
  72. func NewDeserializeStep() *DeserializeStep {
  73. return &DeserializeStep{
  74. ids: newOrderedIDs(),
  75. }
  76. }
  77. var _ Middleware = (*DeserializeStep)(nil)
  78. // ID returns the unique ID of the step as a middleware.
  79. func (s *DeserializeStep) ID() string {
  80. return "Deserialize stack step"
  81. }
  82. // HandleMiddleware invokes the middleware by decorating the next handler
  83. // provided. Returns the result of the middleware and handler being invoked.
  84. //
  85. // Implements Middleware interface.
  86. func (s *DeserializeStep) HandleMiddleware(ctx context.Context, in interface{}, next Handler) (
  87. out interface{}, metadata Metadata, err error,
  88. ) {
  89. order := s.ids.GetOrder()
  90. var h DeserializeHandler = deserializeWrapHandler{Next: next}
  91. for i := len(order) - 1; i >= 0; i-- {
  92. h = decoratedDeserializeHandler{
  93. Next: h,
  94. With: order[i].(DeserializeMiddleware),
  95. }
  96. }
  97. sIn := DeserializeInput{
  98. Request: in,
  99. }
  100. res, metadata, err := h.HandleDeserialize(ctx, sIn)
  101. return res.Result, metadata, err
  102. }
  103. // Get retrieves the middleware identified by id. If the middleware is not present, returns false.
  104. func (s *DeserializeStep) Get(id string) (DeserializeMiddleware, bool) {
  105. get, ok := s.ids.Get(id)
  106. if !ok {
  107. return nil, false
  108. }
  109. return get.(DeserializeMiddleware), ok
  110. }
  111. // Add injects the middleware to the relative position of the middleware group.
  112. // Returns an error if the middleware already exists.
  113. func (s *DeserializeStep) Add(m DeserializeMiddleware, pos RelativePosition) error {
  114. return s.ids.Add(m, pos)
  115. }
  116. // Insert injects the middleware relative to an existing middleware ID.
  117. // Returns error if the original middleware does not exist, or the middleware
  118. // being added already exists.
  119. func (s *DeserializeStep) Insert(m DeserializeMiddleware, relativeTo string, pos RelativePosition) error {
  120. return s.ids.Insert(m, relativeTo, pos)
  121. }
  122. // Swap removes the middleware by id, replacing it with the new middleware.
  123. // Returns the middleware removed, or error if the middleware to be removed
  124. // doesn't exist.
  125. func (s *DeserializeStep) Swap(id string, m DeserializeMiddleware) (DeserializeMiddleware, error) {
  126. removed, err := s.ids.Swap(id, m)
  127. if err != nil {
  128. return nil, err
  129. }
  130. return removed.(DeserializeMiddleware), nil
  131. }
  132. // Remove removes the middleware by id. Returns error if the middleware
  133. // doesn't exist.
  134. func (s *DeserializeStep) Remove(id string) (DeserializeMiddleware, error) {
  135. removed, err := s.ids.Remove(id)
  136. if err != nil {
  137. return nil, err
  138. }
  139. return removed.(DeserializeMiddleware), nil
  140. }
  141. // List returns a list of the middleware in the step.
  142. func (s *DeserializeStep) List() []string {
  143. return s.ids.List()
  144. }
  145. // Clear removes all middleware in the step.
  146. func (s *DeserializeStep) Clear() {
  147. s.ids.Clear()
  148. }
  149. type deserializeWrapHandler struct {
  150. Next Handler
  151. }
  152. var _ DeserializeHandler = (*deserializeWrapHandler)(nil)
  153. // HandleDeserialize implements DeserializeHandler, converts types and delegates to underlying
  154. // generic handler.
  155. func (w deserializeWrapHandler) HandleDeserialize(ctx context.Context, in DeserializeInput) (
  156. out DeserializeOutput, metadata Metadata, err error,
  157. ) {
  158. resp, metadata, err := w.Next.Handle(ctx, in.Request)
  159. return DeserializeOutput{
  160. RawResponse: resp,
  161. }, metadata, err
  162. }
  163. type decoratedDeserializeHandler struct {
  164. Next DeserializeHandler
  165. With DeserializeMiddleware
  166. }
  167. var _ DeserializeHandler = (*decoratedDeserializeHandler)(nil)
  168. func (h decoratedDeserializeHandler) HandleDeserialize(ctx context.Context, in DeserializeInput) (
  169. out DeserializeOutput, metadata Metadata, err error,
  170. ) {
  171. return h.With.HandleDeserialize(ctx, in, h.Next)
  172. }
  173. // DeserializeHandlerFunc provides a wrapper around a function to be used as a deserialize middleware handler.
  174. type DeserializeHandlerFunc func(context.Context, DeserializeInput) (DeserializeOutput, Metadata, error)
  175. // HandleDeserialize invokes the wrapped function with the given arguments.
  176. func (d DeserializeHandlerFunc) HandleDeserialize(ctx context.Context, in DeserializeInput) (DeserializeOutput, Metadata, error) {
  177. return d(ctx, in)
  178. }
  179. var _ DeserializeHandler = DeserializeHandlerFunc(nil)