forked from benthosdev/benthos
/
config_bloblang.go
38 lines (32 loc) · 1.21 KB
/
config_bloblang.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package service
import (
"fmt"
"strings"
"github.com/dafanshu/benthos/v4/internal/docs"
"github.com/dafanshu/benthos/v4/public/bloblang"
)
// NewBloblangField defines a new config field that describes a Bloblang mapping
// string. It is then possible to extract a *bloblang.Executor from the
// resulting parsed config with the method FieldBloblang.
func NewBloblangField(name string) *ConfigField {
tf := docs.FieldBloblang(name, "")
return &ConfigField{field: tf}
}
// FieldBloblang accesses a field from a parsed config that was defined with
// NewBloblangField and returns either a *bloblang.Executor or an error if the
// mapping was invalid.
func (p *ParsedConfig) FieldBloblang(path ...string) (*bloblang.Executor, error) {
v, exists := p.field(path...)
if !exists {
return nil, fmt.Errorf("field '%v' was not found in the config", strings.Join(path, "."))
}
str, ok := v.(string)
if !ok {
return nil, fmt.Errorf("expected field '%v' to be a string, got %T", strings.Join(path, "."), v)
}
exec, err := bloblang.XWrapEnvironment(p.mgr.BloblEnvironment()).Parse(str)
if err != nil {
return nil, fmt.Errorf("failed to parse bloblang mapping '%v': %v", strings.Join(path, "."), err)
}
return exec, nil
}