github.com/Jeffail/benthos/v3@v3.65.0/public/service/config_input.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/Jeffail/benthos/v3/internal/docs"
     8  	"github.com/Jeffail/benthos/v3/lib/input"
     9  	"gopkg.in/yaml.v3"
    10  )
    11  
    12  // NewInputField defines a new input field, it is then possible to extract an
    13  // OwnedInput from the resulting parsed config with the method FieldInput.
    14  func NewInputField(name string) *ConfigField {
    15  	return &ConfigField{
    16  		field: docs.FieldCommon(name, "").HasType(docs.FieldTypeInput),
    17  	}
    18  }
    19  
    20  // FieldInput accesses a field from a parsed config that was defined with
    21  // NewInputField and returns an OwnedInput, or an error if the configuration was
    22  // invalid.
    23  func (p *ParsedConfig) FieldInput(path ...string) (*OwnedInput, error) {
    24  	field, exists := p.field(path...)
    25  	if !exists {
    26  		return nil, fmt.Errorf("field '%v' was not found in the config", strings.Join(path, "."))
    27  	}
    28  
    29  	pNode, ok := field.(*yaml.Node)
    30  	if !ok {
    31  		return nil, fmt.Errorf("unexpected value, expected object, got %T", field)
    32  	}
    33  
    34  	var conf input.Config
    35  	if err := pNode.Decode(&conf); err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	iproc, err := p.mgr.NewInput(conf, false)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return &OwnedInput{iproc}, nil
    44  }
    45  
    46  // NewInputListField defines a new input list field, it is then possible
    47  // to extract a list of OwnedInput from the resulting parsed config with the
    48  // method FieldInputList.
    49  func NewInputListField(name string) *ConfigField {
    50  	return &ConfigField{
    51  		field: docs.FieldCommon(name, "").Array().HasType(docs.FieldTypeInput),
    52  	}
    53  }
    54  
    55  // FieldInputList accesses a field from a parsed config that was defined
    56  // with NewInputListField and returns a slice of OwnedInput, or an error
    57  // if the configuration was invalid.
    58  func (p *ParsedConfig) FieldInputList(path ...string) ([]*OwnedInput, error) {
    59  	field, exists := p.field(path...)
    60  	if !exists {
    61  		return nil, fmt.Errorf("field '%v' was not found in the config", strings.Join(path, "."))
    62  	}
    63  
    64  	fieldArray, ok := field.([]interface{})
    65  	if !ok {
    66  		return nil, fmt.Errorf("unexpected value, expected array, got %T", field)
    67  	}
    68  
    69  	var configs []input.Config
    70  	for i, iConf := range fieldArray {
    71  		node, ok := iConf.(*yaml.Node)
    72  		if !ok {
    73  			return nil, fmt.Errorf("value %v returned unexpected value, expected object, got %T", i, iConf)
    74  		}
    75  
    76  		var conf input.Config
    77  		if err := node.Decode(&conf); err != nil {
    78  			return nil, fmt.Errorf("value %v: %w", i, err)
    79  		}
    80  		configs = append(configs, conf)
    81  	}
    82  
    83  	ins := make([]*OwnedInput, len(configs))
    84  	for i, c := range configs {
    85  		iproc, err := p.mgr.NewInput(c, false)
    86  		if err != nil {
    87  			return nil, fmt.Errorf("input %v: %w", i, err)
    88  		}
    89  		ins[i] = &OwnedInput{iproc}
    90  	}
    91  
    92  	return ins, nil
    93  }