mirror of
https://github.com/hanzoai/otel-collector.git
synced 2026-10-30 18:26:25 +00:00
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]>
71 lines
1.5 KiB
Go
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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|