registry.go 642 B

123456789101112131415161718192021222324252627282930
  1. package interceptor
  2. // Registry is a collector for interceptors.
  3. type Registry struct {
  4. factories []Factory
  5. }
  6. // Add adds a new Interceptor to the registry.
  7. func (r *Registry) Add(f Factory) {
  8. r.factories = append(r.factories, f)
  9. }
  10. // Build constructs a single Interceptor from a InterceptorRegistry
  11. func (r *Registry) Build(id string) (Interceptor, error) {
  12. if len(r.factories) == 0 {
  13. return &NoOp{}, nil
  14. }
  15. interceptors := []Interceptor{}
  16. for _, f := range r.factories {
  17. i, err := f.NewInterceptor(id)
  18. if err != nil {
  19. return nil, err
  20. }
  21. interceptors = append(interceptors, i)
  22. }
  23. return NewChain(interceptors), nil
  24. }