step_initialize.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package middleware
  2. import "context"
  3. // InitializeInput wraps the input parameters for the InitializeMiddlewares to
  4. // consume. InitializeMiddleware may modify the parameter value before
  5. // forwarding it along to the next InitializeHandler.
  6. type InitializeInput struct {
  7. Parameters interface{}
  8. }
  9. // InitializeOutput provides the result returned by the next InitializeHandler.
  10. type InitializeOutput struct {
  11. Result interface{}
  12. }
  13. // InitializeHandler provides the interface for the next handler the
  14. // InitializeMiddleware will call in the middleware chain.
  15. type InitializeHandler interface {
  16. HandleInitialize(ctx context.Context, in InitializeInput) (
  17. out InitializeOutput, metadata Metadata, err error,
  18. )
  19. }
  20. // InitializeMiddleware provides the interface for middleware specific to the
  21. // initialize step. Delegates to the next InitializeHandler for further
  22. // processing.
  23. type InitializeMiddleware interface {
  24. // ID returns a unique ID for the middleware in the InitializeStep. The step does not
  25. // allow duplicate IDs.
  26. ID() string
  27. // HandleInitialize 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. HandleInitialize(ctx context.Context, in InitializeInput, next InitializeHandler) (
  31. out InitializeOutput, metadata Metadata, err error,
  32. )
  33. }
  34. // InitializeMiddlewareFunc returns a InitializeMiddleware with the unique ID provided,
  35. // and the func to be invoked.
  36. func InitializeMiddlewareFunc(id string, fn func(context.Context, InitializeInput, InitializeHandler) (InitializeOutput, Metadata, error)) InitializeMiddleware {
  37. return initializeMiddlewareFunc{
  38. id: id,
  39. fn: fn,
  40. }
  41. }
  42. type initializeMiddlewareFunc struct {
  43. // Unique ID for the middleware.
  44. id string
  45. // Middleware function to be called.
  46. fn func(context.Context, InitializeInput, InitializeHandler) (
  47. InitializeOutput, Metadata, error,
  48. )
  49. }
  50. // ID returns the unique ID for the middleware.
  51. func (s initializeMiddlewareFunc) ID() string { return s.id }
  52. // HandleInitialize invokes the middleware Fn.
  53. func (s initializeMiddlewareFunc) HandleInitialize(ctx context.Context, in InitializeInput, next InitializeHandler) (
  54. out InitializeOutput, metadata Metadata, err error,
  55. ) {
  56. return s.fn(ctx, in, next)
  57. }
  58. var _ InitializeMiddleware = (initializeMiddlewareFunc{})
  59. // InitializeStep provides the ordered grouping of InitializeMiddleware to be
  60. // invoked on a handler.
  61. type InitializeStep struct {
  62. ids *orderedIDs
  63. }
  64. // NewInitializeStep returns an InitializeStep ready to have middleware for
  65. // initialization added to it.
  66. func NewInitializeStep() *InitializeStep {
  67. return &InitializeStep{
  68. ids: newOrderedIDs(),
  69. }
  70. }
  71. var _ Middleware = (*InitializeStep)(nil)
  72. // ID returns the unique ID of the step as a middleware.
  73. func (s *InitializeStep) ID() string {
  74. return "Initialize 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 *InitializeStep) HandleMiddleware(ctx context.Context, in interface{}, next Handler) (
  81. out interface{}, metadata Metadata, err error,
  82. ) {
  83. order := s.ids.GetOrder()
  84. var h InitializeHandler = initializeWrapHandler{Next: next}
  85. for i := len(order) - 1; i >= 0; i-- {
  86. h = decoratedInitializeHandler{
  87. Next: h,
  88. With: order[i].(InitializeMiddleware),
  89. }
  90. }
  91. sIn := InitializeInput{
  92. Parameters: in,
  93. }
  94. res, metadata, err := h.HandleInitialize(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 *InitializeStep) Get(id string) (InitializeMiddleware, bool) {
  99. get, ok := s.ids.Get(id)
  100. if !ok {
  101. return nil, false
  102. }
  103. return get.(InitializeMiddleware), 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 *InitializeStep) Add(m InitializeMiddleware, 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 *InitializeStep) Insert(m InitializeMiddleware, 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 *InitializeStep) Swap(id string, m InitializeMiddleware) (InitializeMiddleware, error) {
  120. removed, err := s.ids.Swap(id, m)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return removed.(InitializeMiddleware), nil
  125. }
  126. // Remove removes the middleware by id. Returns error if the middleware
  127. // doesn't exist.
  128. func (s *InitializeStep) Remove(id string) (InitializeMiddleware, error) {
  129. removed, err := s.ids.Remove(id)
  130. if err != nil {
  131. return nil, err
  132. }
  133. return removed.(InitializeMiddleware), nil
  134. }
  135. // List returns a list of the middleware in the step.
  136. func (s *InitializeStep) List() []string {
  137. return s.ids.List()
  138. }
  139. // Clear removes all middleware in the step.
  140. func (s *InitializeStep) Clear() {
  141. s.ids.Clear()
  142. }
  143. type initializeWrapHandler struct {
  144. Next Handler
  145. }
  146. var _ InitializeHandler = (*initializeWrapHandler)(nil)
  147. // HandleInitialize implements InitializeHandler, converts types and delegates to underlying
  148. // generic handler.
  149. func (w initializeWrapHandler) HandleInitialize(ctx context.Context, in InitializeInput) (
  150. out InitializeOutput, metadata Metadata, err error,
  151. ) {
  152. res, metadata, err := w.Next.Handle(ctx, in.Parameters)
  153. return InitializeOutput{
  154. Result: res,
  155. }, metadata, err
  156. }
  157. type decoratedInitializeHandler struct {
  158. Next InitializeHandler
  159. With InitializeMiddleware
  160. }
  161. var _ InitializeHandler = (*decoratedInitializeHandler)(nil)
  162. func (h decoratedInitializeHandler) HandleInitialize(ctx context.Context, in InitializeInput) (
  163. out InitializeOutput, metadata Metadata, err error,
  164. ) {
  165. return h.With.HandleInitialize(ctx, in, h.Next)
  166. }
  167. // InitializeHandlerFunc provides a wrapper around a function to be used as an initialize middleware handler.
  168. type InitializeHandlerFunc func(context.Context, InitializeInput) (InitializeOutput, Metadata, error)
  169. // HandleInitialize calls the wrapped function with the provided arguments.
  170. func (i InitializeHandlerFunc) HandleInitialize(ctx context.Context, in InitializeInput) (InitializeOutput, Metadata, error) {
  171. return i(ctx, in)
  172. }
  173. var _ InitializeHandler = InitializeHandlerFunc(nil)