github.com/Jeffail/benthos/v3@v3.65.0/internal/bundle/processors.go (about) 1 package bundle 2 3 import ( 4 "sort" 5 6 "github.com/Jeffail/benthos/v3/internal/docs" 7 "github.com/Jeffail/benthos/v3/lib/processor" 8 "github.com/Jeffail/benthos/v3/lib/types" 9 ) 10 11 // AllProcessors is a set containing every single processor that has been 12 // imported. 13 var AllProcessors = &ProcessorSet{ 14 specs: map[string]processorSpec{}, 15 } 16 17 //------------------------------------------------------------------------------ 18 19 // ProcessorAdd adds a new processor to this environment by providing a 20 // constructor and documentation. 21 func (e *Environment) ProcessorAdd(constructor ProcessorConstructor, spec docs.ComponentSpec) error { 22 return e.processors.Add(constructor, spec) 23 } 24 25 // ProcessorInit attempts to initialise a processor from a config. 26 func (e *Environment) ProcessorInit(conf processor.Config, mgr NewManagement) (types.Processor, error) { 27 return e.processors.Init(conf, mgr) 28 } 29 30 // ProcessorDocs returns a slice of processor specs, which document each method. 31 func (e *Environment) ProcessorDocs() []docs.ComponentSpec { 32 return e.processors.Docs() 33 } 34 35 //------------------------------------------------------------------------------ 36 37 // ProcessorConstructor constructs an processor component. 38 type ProcessorConstructor func(processor.Config, NewManagement) (processor.Type, error) 39 40 type processorSpec struct { 41 constructor ProcessorConstructor 42 spec docs.ComponentSpec 43 } 44 45 // ProcessorSet contains an explicit set of processors available to a Benthos 46 // service. 47 type ProcessorSet struct { 48 specs map[string]processorSpec 49 } 50 51 // Add a new processor to this set by providing a spec (name, documentation, and 52 // constructor). 53 func (s *ProcessorSet) Add(constructor ProcessorConstructor, spec docs.ComponentSpec) error { 54 if s.specs == nil { 55 s.specs = map[string]processorSpec{} 56 } 57 s.specs[spec.Name] = processorSpec{ 58 constructor: constructor, 59 spec: spec, 60 } 61 docs.RegisterDocs(spec) 62 return nil 63 } 64 65 // Init attempts to initialise an processor from a config. 66 func (s *ProcessorSet) Init(conf processor.Config, mgr NewManagement) (types.Processor, error) { 67 spec, exists := s.specs[conf.Type] 68 if !exists { 69 // TODO: V4 Remove this 70 if ctor, exists := processor.GetDeprecatedPlugin(conf.Type); exists { 71 return ctor(conf, mgr, mgr.Logger(), mgr.Metrics()) 72 } 73 return nil, types.ErrInvalidProcessorType 74 } 75 return spec.constructor(conf, mgr) 76 } 77 78 // Docs returns a slice of processor specs, which document each method. 79 func (s *ProcessorSet) Docs() []docs.ComponentSpec { 80 var docs []docs.ComponentSpec 81 for _, v := range s.specs { 82 docs = append(docs, v.spec) 83 } 84 sort.Slice(docs, func(i, j int) bool { 85 return docs[i].Name < docs[j].Name 86 }) 87 return docs 88 } 89 90 // DocsFor returns the documentation for a given component name, returns a 91 // boolean indicating whether the component name exists. 92 func (s *ProcessorSet) DocsFor(name string) (docs.ComponentSpec, bool) { 93 c, ok := s.specs[name] 94 if !ok { 95 return docs.ComponentSpec{}, false 96 } 97 return c.spec, true 98 }