Files
zeekayandClaude Opus 5 7b23b7e68d Go 1.27.1, and the code it can say more simply
Modules declare go 1.27.1 and the build images follow. go fix applied the
modernizers; omitzero is not applied, since it changes what the JSON says.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-09-16 19:25:40 -07:00

391 lines
8.3 KiB
Go

package parser
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Tomato struct {
Name string
Meta map[string]any
}
type Potato struct {
Name string
Meta map[string]map[string]any
}
type Ignored struct {
String string `label:"-"`
StringP *string `label:"-"`
Struct Tomato `label:"-"`
StructP *Tomato `label:"-"`
Slice []string `label:"-"`
Map map[string]any `label:"-"`
}
func TestDecode_RawValue(t *testing.T) {
testCases := []struct {
desc string
labels map[string]string
elt any
expected any
}{
{
desc: "level 1",
elt: &Tomato{},
labels: map[string]string{
"ingress.name": "test",
"ingress.meta.aaa": "test",
},
expected: &Tomato{
Name: "test",
Meta: map[string]any{
"aaa": "test",
},
},
},
{
desc: "level 2",
labels: map[string]string{
"ingress.name": "test",
"ingress.meta.aaa": "test",
"ingress.meta.bbb.ccc": "test",
},
elt: &Tomato{},
expected: &Tomato{
Name: "test",
Meta: map[string]any{
"aaa": "test",
"bbb": map[string]any{
"ccc": "test",
},
},
},
},
{
desc: "level 3",
labels: map[string]string{
"ingress.name": "test",
"ingress.meta.aaa": "test",
"ingress.meta.bbb.ccc": "test",
"ingress.meta.bbb.ddd.eee": "test",
},
elt: &Tomato{},
expected: &Tomato{
Name: "test",
Meta: map[string]any{
"aaa": "test",
"bbb": map[string]any{
"ccc": "test",
"ddd": map[string]any{
"eee": "test",
},
},
},
},
},
{
desc: "struct slice, one entry",
elt: &Tomato{},
labels: map[string]string{
"ingress.name": "test1",
"ingress.meta.aaa[0].bbb": "test2",
"ingress.meta.aaa[0].ccc": "test3",
},
expected: &Tomato{
Name: "test1",
Meta: map[string]any{
"aaa": []any{
map[string]any{
"bbb": "test2",
"ccc": "test3",
},
},
},
},
},
{
desc: "struct slice, multiple entries",
elt: &Tomato{},
labels: map[string]string{
"ingress.name": "test1",
"ingress.meta.aaa[0].bbb": "test2",
"ingress.meta.aaa[0].ccc": "test3",
"ingress.meta.aaa[1].bbb": "test4",
"ingress.meta.aaa[1].ccc": "test5",
"ingress.meta.aaa[2].bbb": "test6",
"ingress.meta.aaa[2].ccc": "test7",
},
expected: &Tomato{
Name: "test1",
Meta: map[string]any{
"aaa": []any{
map[string]any{
"bbb": "test2",
"ccc": "test3",
},
map[string]any{
"bbb": "test4",
"ccc": "test5",
},
map[string]any{
"bbb": "test6",
"ccc": "test7",
},
},
},
},
},
{
desc: "explicit map of map, level 1",
elt: &Potato{},
labels: map[string]string{
"ingress.name": "test",
"ingress.meta.aaa.bbb": "test1",
},
expected: &Potato{
Name: "test",
Meta: map[string]map[string]any{
"aaa": {
"bbb": "test1",
},
},
},
},
{
desc: "explicit map of map, level 2",
elt: &Potato{},
labels: map[string]string{
"ingress.name": "test",
"ingress.meta.aaa.bbb": "test1",
"ingress.meta.aaa.ccc": "test2",
},
expected: &Potato{
Name: "test",
Meta: map[string]map[string]any{
"aaa": {
"bbb": "test1",
"ccc": "test2",
},
},
},
},
{
desc: "explicit map of map, level 3",
elt: &Potato{},
labels: map[string]string{
"ingress.name": "test",
"ingress.meta.aaa.bbb.ccc": "test1",
"ingress.meta.aaa.bbb.ddd": "test2",
"ingress.meta.aaa.eee": "test3",
},
expected: &Potato{
Name: "test",
Meta: map[string]map[string]any{
"aaa": {
"bbb": map[string]any{
"ccc": "test1",
"ddd": "test2",
},
"eee": "test3",
},
},
},
},
{
desc: "explicit map of map, level 4",
elt: &Potato{},
labels: map[string]string{
"ingress.name": "test",
"ingress.meta.aaa.bbb.ccc.ddd": "test1",
"ingress.meta.aaa.bbb.ccc.eee": "test2",
"ingress.meta.aaa.bbb.fff": "test3",
"ingress.meta.aaa.ggg": "test4",
},
expected: &Potato{
Name: "test",
Meta: map[string]map[string]any{
"aaa": {
"bbb": map[string]any{
"ccc": map[string]any{
"ddd": "test1",
"eee": "test2",
},
"fff": "test3",
},
"ggg": "test4",
},
},
},
},
{
desc: "explicit map of map, struct slice, level 1, one entry",
elt: &Potato{},
labels: map[string]string{
"ingress.name": "test1",
"ingress.meta.aaa.bbb[0].ccc": "test2",
"ingress.meta.aaa.bbb[0].ddd": "test3",
},
expected: &Potato{
Name: "test1",
Meta: map[string]map[string]any{
"aaa": {
"bbb": []any{
map[string]any{
"ccc": "test2",
"ddd": "test3",
},
},
},
},
},
},
{
desc: "explicit map of map, struct slice, level 1, multiple entries",
elt: &Potato{},
labels: map[string]string{
"ingress.name": "test1",
"ingress.meta.aaa.bbb[0].ccc": "test2",
"ingress.meta.aaa.bbb[0].ddd": "test3",
"ingress.meta.aaa.bbb[1].ccc": "test4",
"ingress.meta.aaa.bbb[1].ddd": "test5",
"ingress.meta.aaa.bbb[2].ccc": "test6",
"ingress.meta.aaa.bbb[2].ddd": "test7",
},
expected: &Potato{
Name: "test1",
Meta: map[string]map[string]any{
"aaa": {
"bbb": []any{
map[string]any{
"ccc": "test2",
"ddd": "test3",
},
map[string]any{
"ccc": "test4",
"ddd": "test5",
},
map[string]any{
"ccc": "test6",
"ddd": "test7",
},
},
},
},
},
},
{
desc: "explicit map of map, struct slice, level 2, one entry",
elt: &Potato{},
labels: map[string]string{
"ingress.name": "test1",
"ingress.meta.aaa.bbb.ccc[0].ddd": "test2",
"ingress.meta.aaa.bbb.ccc[0].eee": "test3",
},
expected: &Potato{
Name: "test1",
Meta: map[string]map[string]any{
"aaa": {
"bbb": map[string]any{
"ccc": []any{
map[string]any{
"ddd": "test2",
"eee": "test3",
},
},
},
},
},
},
},
{
desc: "explicit map of map, struct slice, level 2, multiple entries",
elt: &Potato{},
labels: map[string]string{
"ingress.name": "test1",
"ingress.meta.aaa.bbb.ccc[0].ddd": "test2",
"ingress.meta.aaa.bbb.ccc[0].eee": "test3",
"ingress.meta.aaa.bbb.ccc[1].ddd": "test4",
"ingress.meta.aaa.bbb.ccc[1].eee": "test5",
"ingress.meta.aaa.bbb.ccc[2].ddd": "test6",
"ingress.meta.aaa.bbb.ccc[2].eee": "test7",
},
expected: &Potato{
Name: "test1",
Meta: map[string]map[string]any{
"aaa": {
"bbb": map[string]any{
"ccc": []any{
map[string]any{
"ddd": "test2",
"eee": "test3",
},
map[string]any{
"ddd": "test4",
"eee": "test5",
},
map[string]any{
"ddd": "test6",
"eee": "test7",
},
},
},
},
},
},
},
{
desc: "Ignore label tag, one element label for each type",
elt: &Ignored{},
labels: map[string]string{
"ingress.string": "test1",
"ingress.stringp": "test1",
"ingress.struct.name": "test1",
"ingress.structp.name": "test1",
"ingress.slice": "test1",
"ingress.map.aaa": "test1",
},
expected: &Ignored{
String: "",
StringP: nil,
Struct: Tomato{},
StructP: nil,
Slice: nil,
Map: nil,
},
},
{
desc: "Ignore label tag, one empty label for each type",
elt: &Ignored{},
labels: map[string]string{
"ingress.string": "",
"ingress.stringp": "",
"ingress.struct": "",
"ingress.structp": "",
"ingress.slice": "",
"ingress.map": "",
},
expected: &Ignored{
String: "",
StringP: nil,
Struct: Tomato{},
StructP: nil,
Slice: nil,
Map: nil,
},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
err := Decode(test.labels, test.elt, "ingress")
require.NoError(t, err)
assert.Equal(t, test.expected, test.elt)
})
}
}