step_finalize.go 6.3 KB

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