Files
zeekayandHanzo Dev c16ed9fe26 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: Hanzo Dev <[email protected]>
2026-09-17 02:34:35 -07:00

71 lines
1.5 KiB
Go

package flatten
import "testing"
func TestFlattenJSON(t *testing.T) {
type args struct {
data map[string]any
prefix string
}
tests := []struct {
name string
args args
result map[string]any
}{
{
name: "FlattenJSON",
args: args{
data: map[string]any{
"hello": "world",
"user": map[string]any{
"name": "Alice",
"age": 30,
"address": map[string]any{
"city": "Wonderland",
"zip": "12345",
},
},
"items": []any{
map[string]any{
"name": "item1",
"price": 10,
},
map[string]any{
"name": "item2",
"price": 20,
},
},
},
prefix: "",
},
result: map[string]any{
"hello": "world",
"user.name": "Alice",
"user.age": 30.0,
"user.address.city": "Wonderland",
"user.address.zip": "12345",
"items.0.name": "item1",
"items.0.price": 10.0,
"items.1.name": "item2",
"items.1.price": 20.0,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FlattenJSON(tt.args.data, tt.args.prefix)
if len(got) != len(tt.result) {
t.Errorf("FlattenJSON() got %v entries, want %v entries", len(got), len(tt.result))
return
}
for k, v := range tt.result {
if gotV, ok := got[k]; !ok {
t.Errorf("FlattenJSON() missing key %v", k)
} else if gotV != v {
t.Errorf("FlattenJSON() got %v = %v, want %v", k, gotV, v)
}
}
})
}
}