Files
Pablo BaeyensandGitHub bcf43b434b [confmap] Return nil map if original map was nil (#13161)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

Amends `ToStringMap` so that it returns `map[string]any(nil)` if the map
used to create this `Conf` was nil. Currently it returns an empty map.

I consider this a bug since, while not explicitly undocumented, I would
expect the following two properties to be true, and they were not before
this change:
1. For any map `m` without `expandedValue`s,
`NewFromStringMap(m).ToStringMap() == m`
2. For any map `m` without `expandedValue`s and any `path` referencing
an existing key in `m` with a `map[string]any` value,
`NewFromStringMap(m).Sub(path) == m[path[0]][path[1]][...][path[N]]`

<!-- Issue number if applicable -->
#### Link to tracking issue

I need this to be able to distinguish between 

```
foo:
   bar:
```

and

```
foo:
  bar: {}
```

which currently have different behaviors when mapping to pointers.

The goal is to be able to do #13168
2025-06-09 10:43:33 +00:00

124 lines
2.4 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package e2etest
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestNilToStringMap(t *testing.T) {
tests := []struct {
name string
expectedMap map[string]any
expectedSub map[string]any
}{
{
name: "subsection_null.yaml",
expectedMap: map[string]any{
"field": map[string]any{
"key": nil,
},
},
expectedSub: nil,
},
{
name: "subsection_empty_map.yaml",
expectedMap: map[string]any{
"field": map[string]any{
"key": map[string]any{},
},
},
expectedSub: map[string]any{},
},
{
name: "subsection_set_but_empty.yaml",
expectedMap: map[string]any{
"field": map[string]any{
"key": nil,
},
},
expectedSub: nil,
},
{
name: "subsection_unset.yaml",
expectedMap: map[string]any{
"field": nil,
},
expectedSub: nil,
},
{
name: "subsection_unset_empty_map.yaml",
expectedMap: map[string]any{
"field": map[string]any{},
},
expectedSub: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resolver := NewResolver(t, tt.name)
conf, err := resolver.Resolve(context.Background())
require.NoError(t, err)
require.Equal(t, tt.expectedMap, conf.ToStringMap())
sub, err := conf.Sub("field::key")
require.NoError(t, err)
require.Equal(t, tt.expectedSub, sub.ToStringMap())
})
}
}
func TestNilToStringMapEnv(t *testing.T) {
tests := []struct {
envValue string
expectedMap map[string]any
expectedSub map[string]any
}{
{
envValue: "null",
expectedMap: map[string]any{
"field": map[string]any{
"key": nil,
},
},
expectedSub: nil,
},
{
envValue: "{}",
expectedMap: map[string]any{
"field": map[string]any{
"key": map[string]any{},
},
},
expectedSub: map[string]any{},
},
{
envValue: "",
expectedMap: map[string]any{
"field": map[string]any{
"key": nil,
},
},
expectedSub: nil,
},
}
for _, tt := range tests {
t.Run(tt.envValue, func(t *testing.T) {
t.Setenv("VALUE", tt.envValue)
resolver := NewResolver(t, "types_map.yaml")
conf, err := resolver.Resolve(context.Background())
require.NoError(t, err)
require.Equal(t, tt.expectedMap, conf.ToStringMap())
sub, err := conf.Sub("field::key")
require.NoError(t, err)
require.Equal(t, tt.expectedSub, sub.ToStringMap())
})
}
}