Debian Codegraph @

component/componenttest/configtest.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package componenttest // import "go.opentelemetry.io/collector/component/componenttest"
5
6 import (
7 "fmt"
8 "reflect"
9 "regexp"
10 "strings"
11
12 "go.uber.org/multierr"
13 )
14
15 // The regular expression for valid config field tag.
16 var configFieldTagRegExp = regexp.MustCompile("^[a-z0-9][a-z0-9_]*$")
17
18 // CheckConfigStruct enforces that given configuration object is following the patterns
19 // used by the collector. This ensures consistency between different implementations
20 // of components and extensions. It is recommended for implementers of components
21 // to call this function on their tests passing the default configuration of the
22 // component factory.
23 func CheckConfigStruct(config any) error {
24 t := reflect.TypeOf(config)
25 if t.Kind() == reflect.Ptr {
26 t = t.Elem()
27 }
28
29 if t.Kind() != reflect.Struct {
30 return fmt.Errorf("config must be a struct or a pointer to one, the passed object is a %s", t.Kind())
31 }
32
33 return validateConfigDataType(t)
34 }
35
36 // validateConfigDataType performs a descending validation of the given type.
37 // If the type is a struct it goes to each of its fields to check for the proper
38 // tags.
39 func validateConfigDataType(t reflect.Type) error {
40 var errs error
41
42 switch t.Kind() {
43 case reflect.Ptr:
44 errs = multierr.Append(errs, validateConfigDataType(t.Elem()))
45 case reflect.Struct:
46 // Reflect on the pointed data and check each of its fields.
47 nf := t.NumField()
48 for i := range nf {
49 f := t.Field(i)
50 errs = multierr.Append(errs, checkStructFieldTags(f))
51 }
52 default:
53 // The config object can carry other types but they are not used when
54 // reading the configuration via koanf so ignore them. Basically ignore:
55 // reflect.Uintptr, reflect.Chan, reflect.Func, reflect.Interface, and
56 // reflect.UnsafePointer.
57 }
58
59 if errs != nil {
60 return fmt.Errorf("type %q from package %q has invalid config settings: %w", t.Name(), t.PkgPath(), errs)
61 }
62
63 return nil
64 }
65
66 // checkStructFieldTags inspects the tags of a struct field.
67 func checkStructFieldTags(f reflect.StructField) error {
68 tagValue, ok := f.Tag.Lookup("mapstructure")
69 if !ok {
70 // Ignore special types.
71 switch f.Type.Kind() {
72 case reflect.Interface, reflect.Chan, reflect.Func, reflect.Uintptr, reflect.UnsafePointer:
73 // Allow the config to carry the types above, but since they are not read
74 // when loading configuration, just ignore them.
75 return nil
76 }
77
78 // Public fields of other types should be tagged.
79 chars := []byte(f.Name)
80 if len(chars) > 0 && chars[0] >= 'A' && chars[0] <= 'Z' {
81 return fmt.Errorf("mapstructure tag not present on field %q", f.Name)
82 }
83
84 // Not public field, no need to have a tag.
85 return nil
86 }
87
88 if tagValue == "" {
89 return fmt.Errorf("mapstructure tag on field %q is empty", f.Name)
90 }
91
92 tagParts := strings.Split(tagValue, ",")
93 if tagParts[0] != "" {
94 if tagParts[0] == "-" {
95 // Nothing to do, as mapstructure decode skips this field.
96 return nil
97 }
98 }
99
100 for _, tag := range tagParts[1:] {
101 switch tag {
102 case "squash":
103 if (f.Type.Kind() != reflect.Struct) && (f.Type.Kind() != reflect.Ptr || f.Type.Elem().Kind() != reflect.Struct) {
104 return fmt.Errorf(
105 "attempt to squash non-struct type on field %q", f.Name)
106 }
107 case "remain":
108 if f.Type.Kind() != reflect.Map && f.Type.Kind() != reflect.Interface {
109 return fmt.Errorf(`attempt to use "remain" on non-map or interface type field %q`, f.Name)
110 }
111 }
112 }
113
114 switch f.Type.Kind() {
115 case reflect.Struct:
116 // It is another struct, continue down-level.
117 return validateConfigDataType(f.Type)
118
119 case reflect.Map, reflect.Slice, reflect.Array:
120 // The element of map, array, or slice can be itself a configuration object.
121 return validateConfigDataType(f.Type.Elem())
122
123 default:
124 fieldTag := tagParts[0]
125 if fieldTag != "" && !configFieldTagRegExp.MatchString(fieldTag) {
126 return fmt.Errorf(
127 "field %q has config tag %q which doesn't satisfy %q",
128 f.Name,
129 fieldTag,
130 configFieldTagRegExp.String())
131 }
132 }
133
134 return nil
135 }