github.com/Jeffail/benthos/v3@v3.65.0/public/service/config_processor.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/processor" 9 "gopkg.in/yaml.v3" 10 ) 11 12 // NewProcessorField defines a new processor field, it is then possible to 13 // extract an OwnedProcessor from the resulting parsed config with the method 14 // FieldProcessor. 15 func NewProcessorField(name string) *ConfigField { 16 return &ConfigField{ 17 field: docs.FieldCommon(name, "").HasType(docs.FieldTypeProcessor), 18 } 19 } 20 21 // FieldProcessor accesses a field from a parsed config that was defined with 22 // NewProcessorField and returns an OwnedProcessor, or an error if the 23 // configuration was invalid. 24 func (p *ParsedConfig) FieldProcessor(path ...string) (*OwnedProcessor, error) { 25 proc, exists := p.field(path...) 26 if !exists { 27 return nil, fmt.Errorf("field '%v' was not found in the config", strings.Join(path, ".")) 28 } 29 30 pNode, ok := proc.(*yaml.Node) 31 if !ok { 32 return nil, fmt.Errorf("unexpected value, expected object, got %T", proc) 33 } 34 35 var procConf processor.Config 36 if err := pNode.Decode(&procConf); err != nil { 37 return nil, err 38 } 39 40 iproc, err := p.mgr.NewProcessor(procConf) 41 if err != nil { 42 return nil, err 43 } 44 return &OwnedProcessor{iproc}, nil 45 } 46 47 // NewProcessorListField defines a new processor list field, it is then possible 48 // to extract a list of OwnedProcessor from the resulting parsed config with the 49 // method FieldProcessorList. 50 func NewProcessorListField(name string) *ConfigField { 51 return &ConfigField{ 52 field: docs.FieldCommon(name, "").Array().HasType(docs.FieldTypeProcessor), 53 } 54 } 55 56 // FieldProcessorList accesses a field from a parsed config that was defined 57 // with NewProcessorListField and returns a slice of OwnedProcessor, or an error 58 // if the configuration was invalid. 59 func (p *ParsedConfig) FieldProcessorList(path ...string) ([]*OwnedProcessor, error) { 60 proc, exists := p.field(path...) 61 if !exists { 62 return nil, fmt.Errorf("field '%v' was not found in the config", strings.Join(path, ".")) 63 } 64 65 procsArray, ok := proc.([]interface{}) 66 if !ok { 67 return nil, fmt.Errorf("unexpected value, expected array, got %T", proc) 68 } 69 70 var procConfigs []processor.Config 71 for i, iConf := range procsArray { 72 node, ok := iConf.(*yaml.Node) 73 if !ok { 74 return nil, fmt.Errorf("value %v returned unexpected value, expected object, got %T", i, iConf) 75 } 76 77 var pconf processor.Config 78 if err := node.Decode(&pconf); err != nil { 79 return nil, fmt.Errorf("value %v: %w", i, err) 80 } 81 procConfigs = append(procConfigs, pconf) 82 } 83 84 procs := make([]*OwnedProcessor, len(procConfigs)) 85 for i, c := range procConfigs { 86 iproc, err := p.mgr.NewProcessor(c) 87 if err != nil { 88 return nil, fmt.Errorf("processor %v: %w", i, err) 89 } 90 procs[i] = &OwnedProcessor{iproc} 91 } 92 93 return procs, nil 94 }