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