| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xconfmap // import "go.opentelemetry.io/collector/confmap/xconfmap" |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"reflect" |
| 10 |
"strconv" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/confmap" |
| 14 |
) |
| 15 |
|
| 16 |
// As interface types are only used for static typing, a common idiom to find the reflection Type |
| 17 |
// for an interface type Foo is to use a *Foo value. |
| 18 |
var configValidatorType = reflect.TypeFor[Validator]() |
| 19 |
|
| 20 |
// Validator defines an optional interface for configurations to implement to do validation. |
| 21 |
type Validator interface { |
| 22 |
// Validate the configuration and returns an error if invalid. |
| 23 |
Validate() error |
| 24 |
} |
| 25 |
|
| 26 |
// Validate validates a config, by doing this: |
| 27 |
// - Call Validate on the config itself if the config implements ConfigValidator. |
| 28 |
func Validate(cfg any) error { |
| 29 |
var err error |
| 30 |
|
| 31 |
for _, validationErr := range validate(reflect.ValueOf(cfg)) { |
| 32 |
err = errors.Join(err, validationErr) |
| 33 |
} |
| 34 |
|
| 35 |
return err |
| 36 |
} |
| 37 |
|
| 38 |
type pathError struct { |
| 39 |
err error |
| 40 |
path []string |
| 41 |
} |
| 42 |
|
| 43 |
func (pe pathError) Error() string { |
| 44 |
if len(pe.path) > 0 { |
| 45 |
var path string |
| 46 |
sb := strings.Builder{} |
| 47 |
|
| 48 |
_, _ = sb.WriteString(pe.path[len(pe.path)-1]) |
| 49 |
for i := len(pe.path) - 2; i >= 0; i-- { |
| 50 |
_, _ = sb.WriteString(confmap.KeyDelimiter) |
| 51 |
_, _ = sb.WriteString(pe.path[i]) |
| 52 |
} |
| 53 |
path = sb.String() |
| 54 |
|
| 55 |
return fmt.Sprintf("%s: %s", path, pe.err) |
| 56 |
} |
| 57 |
|
| 58 |
return pe.err.Error() |
| 59 |
} |
| 60 |
|
| 61 |
func (pe pathError) Unwrap() error { |
| 62 |
return pe.err |
| 63 |
} |
| 64 |
|
| 65 |
func validate(v reflect.Value) []pathError { |
| 66 |
errs := []pathError{} |
| 67 |
// Validate the value itself. |
| 68 |
switch v.Kind() { |
| 69 |
case reflect.Invalid: |
| 70 |
return nil |
| 71 |
case reflect.Ptr, reflect.Interface: |
| 72 |
return validate(v.Elem()) |
| 73 |
case reflect.Struct: |
| 74 |
err := callValidateIfPossible(v) |
| 75 |
if err != nil { |
| 76 |
errs = append(errs, pathError{err: err}) |
| 77 |
} |
| 78 |
|
| 79 |
// Reflect on the pointed data and check each of its fields. |
| 80 |
for i := 0; i < v.NumField(); i++ { |
| 81 |
if !v.Type().Field(i).IsExported() { |
| 82 |
continue |
| 83 |
} |
| 84 |
field := v.Type().Field(i) |
| 85 |
path := fieldName(field) |
| 86 |
|
| 87 |
subpathErrs := validate(v.Field(i)) |
| 88 |
for _, err := range subpathErrs { |
| 89 |
errs = append(errs, pathError{ |
| 90 |
err: err.err, |
| 91 |
path: append(err.path, path), |
| 92 |
}) |
| 93 |
} |
| 94 |
} |
| 95 |
return errs |
| 96 |
case reflect.Slice, reflect.Array: |
| 97 |
err := callValidateIfPossible(v) |
| 98 |
if err != nil { |
| 99 |
errs = append(errs, pathError{err: err}) |
| 100 |
} |
| 101 |
|
| 102 |
// Reflect on the pointed data and check each of its fields. |
| 103 |
for i := 0; i < v.Len(); i++ { |
| 104 |
subPathErrs := validate(v.Index(i)) |
| 105 |
|
| 106 |
for _, err := range subPathErrs { |
| 107 |
errs = append(errs, pathError{ |
| 108 |
err: err.err, |
| 109 |
path: append(err.path, strconv.Itoa(i)), |
| 110 |
}) |
| 111 |
} |
| 112 |
} |
| 113 |
return errs |
| 114 |
case reflect.Map: |
| 115 |
err := callValidateIfPossible(v) |
| 116 |
if err != nil { |
| 117 |
errs = append(errs, pathError{err: err}) |
| 118 |
} |
| 119 |
|
| 120 |
iter := v.MapRange() |
| 121 |
for iter.Next() { |
| 122 |
keyErrs := validate(iter.Key()) |
| 123 |
valueErrs := validate(iter.Value()) |
| 124 |
key := stringifyMapKey(iter.Key()) |
| 125 |
|
| 126 |
for _, err := range keyErrs { |
| 127 |
errs = append(errs, pathError{err: err.err, path: append(err.path, key)}) |
| 128 |
} |
| 129 |
|
| 130 |
for _, err := range valueErrs { |
| 131 |
errs = append(errs, pathError{err: err.err, path: append(err.path, key)}) |
| 132 |
} |
| 133 |
} |
| 134 |
return errs |
| 135 |
default: |
| 136 |
err := callValidateIfPossible(v) |
| 137 |
if err != nil { |
| 138 |
return []pathError{{err: err}} |
| 139 |
} |
| 140 |
|
| 141 |
return nil |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func callValidateIfPossible(v reflect.Value) error { |
| 146 |
// If the value type implements ConfigValidator just call Validate |
| 147 |
if v.Type().Implements(configValidatorType) { |
| 148 |
return v.Interface().(Validator).Validate() |
| 149 |
} |
| 150 |
|
| 151 |
// If the pointer type implements ConfigValidator call Validate on the pointer to the current value. |
| 152 |
if reflect.PointerTo(v.Type()).Implements(configValidatorType) { |
| 153 |
// If not addressable, then create a new *V pointer and set the value to current v. |
| 154 |
if !v.CanAddr() { |
| 155 |
pv := reflect.New(reflect.PointerTo(v.Type()).Elem()) |
| 156 |
pv.Elem().Set(v) |
| 157 |
v = pv.Elem() |
| 158 |
} |
| 159 |
return v.Addr().Interface().(Validator).Validate() |
| 160 |
} |
| 161 |
|
| 162 |
return nil |
| 163 |
} |
| 164 |
|
| 165 |
func fieldName(field reflect.StructField) string { |
| 166 |
var fieldName string |
| 167 |
if tag, ok := field.Tag.Lookup(confmap.MapstructureTag); ok { |
| 168 |
tags := strings.Split(tag, ",") |
| 169 |
if len(tags) > 0 { |
| 170 |
fieldName = tags[0] |
| 171 |
} |
| 172 |
} |
| 173 |
// Even if the mapstructure tag exists, the field name may not |
| 174 |
// be available, so set it if it is still blank. |
| 175 |
if fieldName == "" { |
| 176 |
fieldName = strings.ToLower(field.Name) |
| 177 |
} |
| 178 |
|
| 179 |
return fieldName |
| 180 |
} |
| 181 |
|
| 182 |
func stringifyMapKey(val reflect.Value) string { |
| 183 |
switch v := val.Interface().(type) { |
| 184 |
case string: |
| 185 |
return v |
| 186 |
case fmt.Stringer: |
| 187 |
return v.String() |
| 188 |
default: |
| 189 |
switch val.Kind() { |
| 190 |
case reflect.Ptr, reflect.Interface, reflect.Struct, reflect.Slice, reflect.Array, reflect.Map: |
| 191 |
return fmt.Sprintf("[%T key]", val.Interface()) |
| 192 |
default: |
| 193 |
return fmt.Sprintf("%v", val.Interface()) |
| 194 |
} |
| 195 |
} |
| 196 |
} |