github.com/Jeffail/benthos/v3@v3.65.0/internal/bundle/environment.go (about)

     1  package bundle
     2  
     3  import (
     4  	"github.com/Jeffail/benthos/v3/internal/docs"
     5  )
     6  
     7  // Environment is a collection of Benthos component plugins that can be used in
     8  // order to build and run streaming pipelines with access to different sets of
     9  // plugins. This is useful for sandboxing, testing, etc.
    10  type Environment struct {
    11  	buffers    *BufferSet
    12  	caches     *CacheSet
    13  	inputs     *InputSet
    14  	outputs    *OutputSet
    15  	processors *ProcessorSet
    16  	rateLimits *RateLimitSet
    17  }
    18  
    19  // NewEnvironment creates an empty environment.
    20  func NewEnvironment() *Environment {
    21  	return &Environment{
    22  		buffers:    &BufferSet{},
    23  		caches:     &CacheSet{},
    24  		inputs:     &InputSet{},
    25  		outputs:    &OutputSet{},
    26  		processors: &ProcessorSet{},
    27  		rateLimits: &RateLimitSet{},
    28  	}
    29  }
    30  
    31  // Clone an existing environment to a new one that can be modified
    32  // independently.
    33  func (e *Environment) Clone() *Environment {
    34  	newEnv := NewEnvironment()
    35  	for _, v := range e.buffers.specs {
    36  		_ = newEnv.buffers.Add(v.constructor, v.spec)
    37  	}
    38  	for _, v := range e.caches.specs {
    39  		_ = newEnv.caches.Add(v.constructor, v.spec)
    40  	}
    41  	for _, v := range e.inputs.specs {
    42  		_ = newEnv.inputs.Add(v.constructor, v.spec)
    43  	}
    44  	for _, v := range e.outputs.specs {
    45  		_ = newEnv.outputs.Add(v.constructor, v.spec)
    46  	}
    47  	for _, v := range e.processors.specs {
    48  		_ = newEnv.processors.Add(v.constructor, v.spec)
    49  	}
    50  	for _, v := range e.rateLimits.specs {
    51  		_ = newEnv.rateLimits.Add(v.constructor, v.spec)
    52  	}
    53  	return newEnv
    54  }
    55  
    56  // GetDocs returns a documentation spec for an implementation of a component.
    57  func (e *Environment) GetDocs(name string, ctype docs.Type) (docs.ComponentSpec, bool) {
    58  	var spec docs.ComponentSpec
    59  	var ok bool
    60  
    61  	switch ctype {
    62  	case docs.TypeBuffer:
    63  		spec, ok = e.buffers.DocsFor(name)
    64  	case docs.TypeCache:
    65  		spec, ok = e.caches.DocsFor(name)
    66  	case docs.TypeInput:
    67  		spec, ok = e.inputs.DocsFor(name)
    68  	case docs.TypeOutput:
    69  		spec, ok = e.outputs.DocsFor(name)
    70  	case docs.TypeProcessor:
    71  		spec, ok = e.processors.DocsFor(name)
    72  	case docs.TypeRateLimit:
    73  		spec, ok = e.rateLimits.DocsFor(name)
    74  	default:
    75  		return docs.GetDocs(nil, name, ctype)
    76  	}
    77  
    78  	return spec, ok
    79  }
    80  
    81  // GlobalEnvironment contains service-wide singleton bundles.
    82  var GlobalEnvironment = &Environment{
    83  	buffers:    AllBuffers,
    84  	caches:     AllCaches,
    85  	inputs:     AllInputs,
    86  	outputs:    AllOutputs,
    87  	processors: AllProcessors,
    88  	rateLimits: AllRateLimits,
    89  }