step_build.go 6.0 KB

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