| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xconfmap |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
) |
| 12 |
|
| 13 |
type configChildStruct struct { |
| 14 |
Child errValidateConfig |
| 15 |
ChildPtr *errValidateConfig |
| 16 |
} |
| 17 |
|
| 18 |
type configChildSlice struct { |
| 19 |
Child []errValidateConfig |
| 20 |
ChildPtr []*errValidateConfig |
| 21 |
} |
| 22 |
|
| 23 |
type configChildMapValue struct { |
| 24 |
Child map[string]errValidateConfig |
| 25 |
ChildPtr map[string]*errValidateConfig |
| 26 |
} |
| 27 |
|
| 28 |
type configChildMapKey struct { |
| 29 |
Child map[errType]string |
| 30 |
ChildPtr map[*errType]string |
| 31 |
} |
| 32 |
|
| 33 |
type configChildTypeDef struct { |
| 34 |
Child errType |
| 35 |
ChildPtr *errType |
| 36 |
} |
| 37 |
|
| 38 |
type config any |
| 39 |
|
| 40 |
type configChildInterface struct { |
| 41 |
Child config |
| 42 |
} |
| 43 |
|
| 44 |
type errValidateConfig struct { |
| 45 |
err error |
| 46 |
} |
| 47 |
|
| 48 |
func (e *errValidateConfig) Validate() error { |
| 49 |
return e.err |
| 50 |
} |
| 51 |
|
| 52 |
type errType string |
| 53 |
|
| 54 |
func (e errType) Validate() error { |
| 55 |
if e == "" { |
| 56 |
return nil |
| 57 |
} |
| 58 |
return errors.New(string(e)) |
| 59 |
} |
| 60 |
|
| 61 |
func newErrType(etStr string) *errType { |
| 62 |
et := errType(etStr) |
| 63 |
return &et |
| 64 |
} |
| 65 |
|
| 66 |
type errMapType map[string]string |
| 67 |
|
| 68 |
func (e errMapType) Validate() error { |
| 69 |
return errors.New(e["err"]) |
| 70 |
} |
| 71 |
|
| 72 |
type structKey struct { |
| 73 |
k string |
| 74 |
e error |
| 75 |
} |
| 76 |
|
| 77 |
func (s structKey) String() string { |
| 78 |
return s.k |
| 79 |
} |
| 80 |
|
| 81 |
func (s structKey) Validate() error { |
| 82 |
return s.e |
| 83 |
} |
| 84 |
|
| 85 |
type configChildMapCustomKey struct { |
| 86 |
Child map[structKey]errValidateConfig |
| 87 |
} |
| 88 |
|
| 89 |
func newErrMapType() *errMapType { |
| 90 |
et := errMapType(nil) |
| 91 |
return &et |
| 92 |
} |
| 93 |
|
| 94 |
type configMapstructure struct { |
| 95 |
Valid *errValidateConfig `mapstructure:"validtag,omitempty"` |
| 96 |
NoData *errValidateConfig `mapstructure:""` |
| 97 |
NoName *errValidateConfig `mapstructure:",remain"` |
| 98 |
} |
| 99 |
|
| 100 |
type configDeeplyNested struct { |
| 101 |
MapKeyChild map[configChildStruct]string |
| 102 |
MapValueChild map[string]configChildStruct |
| 103 |
SliceChild []configChildSlice |
| 104 |
MapIntKey map[int]errValidateConfig |
| 105 |
MapFloatKey map[float64]errValidateConfig |
| 106 |
} |
| 107 |
|
| 108 |
type sliceTypeAlias []configChildSlice |
| 109 |
|
| 110 |
func (sliceTypeAlias) Validate() error { |
| 111 |
return errors.New("sliceTypeAlias error") |
| 112 |
} |
| 113 |
|
| 114 |
func TestValidateConfig(t *testing.T) { |
| 115 |
tests := []struct { |
| 116 |
name string |
| 117 |
cfg any |
| 118 |
expected error |
| 119 |
}{ |
| 120 |
{ |
| 121 |
name: "struct", |
| 122 |
cfg: errValidateConfig{err: errors.New("struct")}, |
| 123 |
expected: errors.New("struct"), |
| 124 |
}, |
| 125 |
{ |
| 126 |
name: "pointer struct", |
| 127 |
cfg: &errValidateConfig{err: errors.New("pointer struct")}, |
| 128 |
expected: errors.New("pointer struct"), |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: "type", |
| 132 |
cfg: errType("type"), |
| 133 |
expected: errors.New("type"), |
| 134 |
}, |
| 135 |
{ |
| 136 |
name: "pointer child", |
| 137 |
cfg: newErrType("pointer type"), |
| 138 |
expected: errors.New("pointer type"), |
| 139 |
}, |
| 140 |
{ |
| 141 |
name: "child interface with nil", |
| 142 |
cfg: configChildInterface{}, |
| 143 |
expected: nil, |
| 144 |
}, |
| 145 |
{ |
| 146 |
name: "pointer to child interface with nil", |
| 147 |
cfg: &configChildInterface{}, |
| 148 |
expected: nil, |
| 149 |
}, |
| 150 |
{ |
| 151 |
name: "nil", |
| 152 |
cfg: nil, |
| 153 |
expected: nil, |
| 154 |
}, |
| 155 |
{ |
| 156 |
name: "nil map type", |
| 157 |
cfg: errMapType(nil), |
| 158 |
expected: errors.New(""), |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "nil pointer map type", |
| 162 |
cfg: newErrMapType(), |
| 163 |
expected: errors.New(""), |
| 164 |
}, |
| 165 |
{ |
| 166 |
name: "child struct", |
| 167 |
cfg: configChildStruct{Child: errValidateConfig{err: errors.New("child struct")}}, |
| 168 |
expected: errors.New("child: child struct"), |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "pointer child struct", |
| 172 |
cfg: &configChildStruct{Child: errValidateConfig{err: errors.New("pointer child struct")}}, |
| 173 |
expected: errors.New("child: pointer child struct"), |
| 174 |
}, |
| 175 |
{ |
| 176 |
name: "child struct pointer", |
| 177 |
cfg: &configChildStruct{ChildPtr: &errValidateConfig{err: errors.New("child struct pointer")}}, |
| 178 |
expected: errors.New("childptr: child struct pointer"), |
| 179 |
}, |
| 180 |
{ |
| 181 |
name: "child interface", |
| 182 |
cfg: configChildInterface{Child: errValidateConfig{err: errors.New("child interface")}}, |
| 183 |
expected: errors.New("child: child interface"), |
| 184 |
}, |
| 185 |
{ |
| 186 |
name: "pointer to child interface", |
| 187 |
cfg: &configChildInterface{Child: errValidateConfig{err: errors.New("pointer to child interface")}}, |
| 188 |
expected: errors.New("child: pointer to child interface"), |
| 189 |
}, |
| 190 |
{ |
| 191 |
name: "child interface with pointer", |
| 192 |
cfg: configChildInterface{Child: &errValidateConfig{err: errors.New("child interface with pointer")}}, |
| 193 |
expected: errors.New("child: child interface with pointer"), |
| 194 |
}, |
| 195 |
{ |
| 196 |
name: "pointer to child interface with pointer", |
| 197 |
cfg: &configChildInterface{Child: &errValidateConfig{err: errors.New("pointer to child interface with pointer")}}, |
| 198 |
expected: errors.New("child: pointer to child interface with pointer"), |
| 199 |
}, |
| 200 |
{ |
| 201 |
name: "child slice", |
| 202 |
cfg: configChildSlice{Child: []errValidateConfig{{}, {err: errors.New("child slice")}}}, |
| 203 |
expected: errors.New("child::1: child slice"), |
| 204 |
}, |
| 205 |
{ |
| 206 |
name: "pointer child slice", |
| 207 |
cfg: &configChildSlice{Child: []errValidateConfig{{}, {err: errors.New("pointer child slice")}}}, |
| 208 |
expected: errors.New("child::1: pointer child slice"), |
| 209 |
}, |
| 210 |
{ |
| 211 |
name: "child slice pointer", |
| 212 |
cfg: &configChildSlice{ChildPtr: []*errValidateConfig{{}, {err: errors.New("child slice pointer")}}}, |
| 213 |
expected: errors.New("childptr::1: child slice pointer"), |
| 214 |
}, |
| 215 |
{ |
| 216 |
name: "child map value", |
| 217 |
cfg: configChildMapValue{Child: map[string]errValidateConfig{"test": {err: errors.New("child map")}}}, |
| 218 |
expected: errors.New("child::test: child map"), |
| 219 |
}, |
| 220 |
{ |
| 221 |
name: "pointer child map value", |
| 222 |
cfg: &configChildMapValue{Child: map[string]errValidateConfig{"test": {err: errors.New("pointer child map")}}}, |
| 223 |
expected: errors.New("child::test: pointer child map"), |
| 224 |
}, |
| 225 |
{ |
| 226 |
name: "child map value pointer", |
| 227 |
cfg: &configChildMapValue{ChildPtr: map[string]*errValidateConfig{"test": {err: errors.New("child map pointer")}}}, |
| 228 |
expected: errors.New("childptr::test: child map pointer"), |
| 229 |
}, |
| 230 |
{ |
| 231 |
name: "child map key", |
| 232 |
cfg: configChildMapKey{Child: map[errType]string{"child_map_key": ""}}, |
| 233 |
expected: errors.New("child::child_map_key: child_map_key"), |
| 234 |
}, |
| 235 |
{ |
| 236 |
name: "pointer child map key", |
| 237 |
cfg: &configChildMapKey{Child: map[errType]string{"pointer_child_map_key": ""}}, |
| 238 |
expected: errors.New("child::pointer_child_map_key: pointer_child_map_key"), |
| 239 |
}, |
| 240 |
{ |
| 241 |
name: "child map key pointer", |
| 242 |
cfg: &configChildMapKey{ChildPtr: map[*errType]string{newErrType("child map key pointer"): ""}}, |
| 243 |
expected: errors.New("childptr::[*xconfmap.errType key]: child map key pointer"), |
| 244 |
}, |
| 245 |
{ |
| 246 |
name: "map with stringified non-string key type", |
| 247 |
cfg: &configChildMapCustomKey{Child: map[structKey]errValidateConfig{{k: "struct_key", e: errors.New("custom key error")}: {err: errors.New("value error")}}}, |
| 248 |
expected: errors.New("child::struct_key: custom key error\nchild::struct_key: value error"), |
| 249 |
}, |
| 250 |
{ |
| 251 |
name: "child type", |
| 252 |
cfg: configChildTypeDef{Child: "child type"}, |
| 253 |
expected: errors.New("child: child type"), |
| 254 |
}, |
| 255 |
{ |
| 256 |
name: "pointer child type", |
| 257 |
cfg: &configChildTypeDef{Child: "pointer child type"}, |
| 258 |
expected: errors.New("child: pointer child type"), |
| 259 |
}, |
| 260 |
{ |
| 261 |
name: "child type pointer", |
| 262 |
cfg: &configChildTypeDef{ChildPtr: newErrType("child type pointer")}, |
| 263 |
expected: errors.New("childptr: child type pointer"), |
| 264 |
}, |
| 265 |
{ |
| 266 |
name: "valid mapstructure tag", |
| 267 |
cfg: configMapstructure{Valid: &errValidateConfig{errors.New("test")}}, |
| 268 |
expected: errors.New("validtag: test"), |
| 269 |
}, |
| 270 |
{ |
| 271 |
name: "zero-length mapstructure tag", |
| 272 |
cfg: configMapstructure{NoData: &errValidateConfig{errors.New("test")}}, |
| 273 |
expected: errors.New("nodata: test"), |
| 274 |
}, |
| 275 |
{ |
| 276 |
name: "no field name in mapstructure tag", |
| 277 |
cfg: configMapstructure{NoName: &errValidateConfig{errors.New("test")}}, |
| 278 |
expected: errors.New("noname: test"), |
| 279 |
}, |
| 280 |
{ |
| 281 |
name: "nested map key error", |
| 282 |
cfg: configDeeplyNested{MapKeyChild: map[configChildStruct]string{{Child: errValidateConfig{err: errors.New("child key error")}}: "val"}}, |
| 283 |
expected: errors.New("mapkeychild::[xconfmap.configChildStruct key]::child: child key error"), |
| 284 |
}, |
| 285 |
{ |
| 286 |
name: "nested map value error", |
| 287 |
cfg: configDeeplyNested{MapValueChild: map[string]configChildStruct{"key": {Child: errValidateConfig{err: errors.New("child key error")}}}}, |
| 288 |
expected: errors.New("mapvaluechild::key::child: child key error"), |
| 289 |
}, |
| 290 |
{ |
| 291 |
name: "nested slice value error", |
| 292 |
cfg: configDeeplyNested{SliceChild: []configChildSlice{{Child: []errValidateConfig{{err: errors.New("child key error")}}}}}, |
| 293 |
expected: errors.New("slicechild::0::child::0: child key error"), |
| 294 |
}, |
| 295 |
{ |
| 296 |
name: "nested map with int key", |
| 297 |
cfg: configDeeplyNested{MapIntKey: map[int]errValidateConfig{1: {err: errors.New("int key error")}}}, |
| 298 |
expected: errors.New("mapintkey::1: int key error"), |
| 299 |
}, |
| 300 |
{ |
| 301 |
name: "nested map with float key", |
| 302 |
cfg: configDeeplyNested{MapFloatKey: map[float64]errValidateConfig{1.2: {err: errors.New("float key error")}}}, |
| 303 |
expected: errors.New("mapfloatkey::1.2: float key error"), |
| 304 |
}, |
| 305 |
{ |
| 306 |
name: "slice type alias", |
| 307 |
cfg: sliceTypeAlias{}, |
| 308 |
expected: errors.New("sliceTypeAlias error"), |
| 309 |
}, |
| 310 |
} |
| 311 |
|
| 312 |
for _, tt := range tests { |
| 313 |
t.Run(tt.name, func(t *testing.T) { |
| 314 |
err := Validate(tt.cfg) |
| 315 |
|
| 316 |
if tt.expected != nil { |
| 317 |
assert.EqualError(t, err, tt.expected.Error()) |
| 318 |
} else { |
| 319 |
assert.NoError(t, err) |
| 320 |
} |
| 321 |
}) |
| 322 |
} |
| 323 |
} |