|
9 | 9 | "testing"
|
10 | 10 |
|
11 | 11 | "github.com/stretchr/testify/assert"
|
| 12 | + "github.com/stretchr/testify/require" |
12 | 13 | "go.opentelemetry.io/collector/pdata/pcommon"
|
13 | 14 |
|
14 | 15 | "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
|
@@ -39,7 +40,7 @@ func Test_GetMapValue_Invalid(t *testing.T) {
|
39 | 40 | G: getSetter,
|
40 | 41 | },
|
41 | 42 | },
|
42 |
| - err: errors.New("unable to resolve a string index in map: could not resolve key for map/slice, expecting 'string' but got '<nil>'"), |
| 43 | + err: errors.New("cannot get map value: unable to resolve a string index in map: could not resolve key for map/slice, expecting 'string' but got '<nil>'"), |
43 | 44 | },
|
44 | 45 | {
|
45 | 46 | name: "index map with int",
|
@@ -171,7 +172,7 @@ func Test_SetMapValue_Invalid(t *testing.T) {
|
171 | 172 | G: getSetter,
|
172 | 173 | },
|
173 | 174 | },
|
174 |
| - err: errors.New("unable to resolve a string index in map: could not resolve key for map/slice, expecting 'string' but got '<nil>'"), |
| 175 | + err: errors.New("cannot set map value: unable to resolve a string index in map: could not resolve key for map/slice, expecting 'string' but got '<nil>'"), |
175 | 176 | },
|
176 | 177 | {
|
177 | 178 | name: "index map with int",
|
@@ -311,3 +312,136 @@ func Test_SetMapValue_NilKey(t *testing.T) {
|
311 | 312 | err := ctxutil.SetMapValue[any](context.Background(), nil, pcommon.NewMap(), nil, "bar")
|
312 | 313 | assert.Error(t, err)
|
313 | 314 | }
|
| 315 | + |
| 316 | +func Test_SetMap(t *testing.T) { |
| 317 | + createMap := func() pcommon.Map { |
| 318 | + m := pcommon.NewMap() |
| 319 | + require.NoError(t, m.FromRaw(map[string]any{"foo": "bar"})) |
| 320 | + return m |
| 321 | + } |
| 322 | + tests := []struct { |
| 323 | + name string |
| 324 | + val any |
| 325 | + err error |
| 326 | + expected any |
| 327 | + }{ |
| 328 | + { |
| 329 | + name: "invalid type", |
| 330 | + val: "invalid", |
| 331 | + err: nil, // This is an issue in SetMap(), not returning an error here. |
| 332 | + expected: pcommon.NewMap(), |
| 333 | + }, |
| 334 | + { |
| 335 | + name: "raw map", |
| 336 | + val: map[string]any{"foo": "bar"}, |
| 337 | + expected: createMap(), |
| 338 | + }, |
| 339 | + { |
| 340 | + name: "pcommon.Map", |
| 341 | + val: createMap(), |
| 342 | + expected: createMap(), |
| 343 | + }, |
| 344 | + } |
| 345 | + |
| 346 | + for _, tt := range tests { |
| 347 | + t.Run(tt.name, func(t *testing.T) { |
| 348 | + m := pcommon.NewMap() |
| 349 | + err := ctxutil.SetMap(m, tt.val) |
| 350 | + if tt.err != nil { |
| 351 | + require.Equal(t, tt.err, err) |
| 352 | + return |
| 353 | + } |
| 354 | + assert.Equal(t, tt.expected, m) |
| 355 | + }) |
| 356 | + } |
| 357 | +} |
| 358 | + |
| 359 | +func Test_GetMap(t *testing.T) { |
| 360 | + createMap := func() pcommon.Map { |
| 361 | + m := pcommon.NewMap() |
| 362 | + require.NoError(t, m.FromRaw(map[string]any{"foo": "bar"})) |
| 363 | + return m |
| 364 | + } |
| 365 | + tests := []struct { |
| 366 | + name string |
| 367 | + val any |
| 368 | + err error |
| 369 | + }{ |
| 370 | + { |
| 371 | + name: "invalid type", |
| 372 | + val: "invalid", |
| 373 | + err: errors.New("failed to convert type string into pcommon.Map"), |
| 374 | + }, |
| 375 | + { |
| 376 | + name: "raw map", |
| 377 | + val: map[string]any{"foo": "bar"}, |
| 378 | + }, |
| 379 | + { |
| 380 | + name: "pcommon.Map", |
| 381 | + val: createMap(), |
| 382 | + }, |
| 383 | + } |
| 384 | + |
| 385 | + for _, tt := range tests { |
| 386 | + t.Run(tt.name, func(t *testing.T) { |
| 387 | + m, err := ctxutil.GetMap(tt.val) |
| 388 | + if tt.err != nil { |
| 389 | + require.Equal(t, tt.err, err) |
| 390 | + return |
| 391 | + } |
| 392 | + assert.Equal(t, m, createMap()) |
| 393 | + }) |
| 394 | + } |
| 395 | +} |
| 396 | + |
| 397 | +func Test_GetMapKeyName(t *testing.T) { |
| 398 | + getSetter := &ottl.StandardGetSetter[any]{ |
| 399 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 400 | + return nil, nil |
| 401 | + }, |
| 402 | + } |
| 403 | + tests := []struct { |
| 404 | + name string |
| 405 | + keys []ottl.Key[any] |
| 406 | + err error |
| 407 | + key string |
| 408 | + }{ |
| 409 | + { |
| 410 | + name: "first key not a string", |
| 411 | + keys: []ottl.Key[any]{ |
| 412 | + &pathtest.Key[any]{ |
| 413 | + I: ottltest.Intp(0), |
| 414 | + G: getSetter, |
| 415 | + }, |
| 416 | + }, |
| 417 | + err: errors.New("unable to resolve a string index in map: could not resolve key for map/slice, expecting 'string' but got '<nil>'"), |
| 418 | + }, |
| 419 | + { |
| 420 | + name: "first key not initialized", |
| 421 | + keys: []ottl.Key[any]{ |
| 422 | + &pathtest.Key[any]{}, |
| 423 | + }, |
| 424 | + err: errors.New("unable to resolve a string index in map: invalid key type"), |
| 425 | + }, |
| 426 | + { |
| 427 | + name: "valid", |
| 428 | + keys: []ottl.Key[any]{ |
| 429 | + &pathtest.Key[any]{ |
| 430 | + S: ottltest.Strp("string"), |
| 431 | + }, |
| 432 | + }, |
| 433 | + key: "string", |
| 434 | + }, |
| 435 | + } |
| 436 | + |
| 437 | + for _, tt := range tests { |
| 438 | + t.Run(tt.name, func(t *testing.T) { |
| 439 | + resolvedKey, err := ctxutil.GetMapKeyName[any](context.Background(), nil, tt.keys[0]) |
| 440 | + if tt.err != nil { |
| 441 | + assert.Equal(t, tt.err.Error(), err.Error()) |
| 442 | + return |
| 443 | + } |
| 444 | + assert.Equal(t, tt.key, *resolvedKey) |
| 445 | + }) |
| 446 | + } |
| 447 | +} |
0 commit comments