/
config.go
116 lines (92 loc) · 2.72 KB
/
config.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package operator // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
import (
"encoding/json"
"fmt"
"go.opentelemetry.io/collector/confmap"
"go.uber.org/zap"
)
// Config is the configuration of an operator
type Config struct {
Builder
}
// NewConfig wraps the builder interface in a concrete struct
func NewConfig(b Builder) Config {
return Config{Builder: b}
}
// Builder is an entity that can build a single operator
type Builder interface {
ID() string
Type() string
Build(*zap.SugaredLogger) (Operator, error)
SetID(string)
}
// UnmarshalJSON will unmarshal a config from JSON.
func (c *Config) UnmarshalJSON(bytes []byte) error {
var typeUnmarshaller struct {
Type string
}
if err := json.Unmarshal(bytes, &typeUnmarshaller); err != nil {
return err
}
if typeUnmarshaller.Type == "" {
return fmt.Errorf("missing required field 'type'")
}
builderFunc, ok := DefaultRegistry.Lookup(typeUnmarshaller.Type)
if !ok {
return fmt.Errorf("unsupported type '%s'", typeUnmarshaller.Type)
}
builder := builderFunc()
if err := json.Unmarshal(bytes, builder); err != nil {
return fmt.Errorf("unmarshal to %s: %w", typeUnmarshaller.Type, err)
}
c.Builder = builder
return nil
}
// UnmarshalYAML will unmarshal a config from YAML.
func (c *Config) UnmarshalYAML(unmarshal func(any) error) error {
rawConfig := map[string]any{}
err := unmarshal(&rawConfig)
if err != nil {
return fmt.Errorf("failed to unmarshal yaml to base config: %w", err)
}
typeInterface, ok := rawConfig["type"]
if !ok {
return fmt.Errorf("missing required field 'type'")
}
typeString, ok := typeInterface.(string)
if !ok {
return fmt.Errorf("non-string type %T for field 'type'", typeInterface)
}
builderFunc, ok := DefaultRegistry.Lookup(typeString)
if !ok {
return fmt.Errorf("unsupported type '%s'", typeString)
}
builder := builderFunc()
if err = unmarshal(builder); err != nil {
return fmt.Errorf("unmarshal to %s: %w", typeString, err)
}
c.Builder = builder
return nil
}
func (c *Config) Unmarshal(component *confmap.Conf) error {
if !component.IsSet("type") {
return fmt.Errorf("missing required field 'type'")
}
typeInterface := component.Get("type")
typeString, ok := typeInterface.(string)
if !ok {
return fmt.Errorf("non-string type %T for field 'type'", typeInterface)
}
builderFunc, ok := DefaultRegistry.Lookup(typeString)
if !ok {
return fmt.Errorf("unsupported type '%s'", typeString)
}
builder := builderFunc()
if err := component.Unmarshal(builder, confmap.WithIgnoreUnused()); err != nil {
return fmt.Errorf("unmarshal to %s: %w", typeString, err)
}
c.Builder = builder
return nil
}