mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
common/helpers: make subnetmap work with struct as values
The way it was converted from a mapstruct made it not possible to have struct as values. Fix that by checking if keys look like IP or not.
This commit is contained in:
@@ -97,6 +97,10 @@ func TestSubnetMapUnmarshalHook(t *testing.T) {
|
||||
Description: "Invalid IP",
|
||||
Input: gin.H{"200.33.300.1": "customer"},
|
||||
Error: true,
|
||||
}, {
|
||||
Description: "Random key",
|
||||
Input: gin.H{"kfgdjgkfj": "customer"},
|
||||
Error: true,
|
||||
}, {
|
||||
Description: "Single value",
|
||||
Input: "customer",
|
||||
@@ -158,3 +162,72 @@ func TestSubnetMapUnmarshalHook(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubnetMapUnmarshalHookWithMapValue(t *testing.T) {
|
||||
type SomeStruct struct {
|
||||
Blip string
|
||||
Blop string
|
||||
}
|
||||
cases := []struct {
|
||||
Description string
|
||||
Input gin.H
|
||||
Expected gin.H
|
||||
}{
|
||||
{
|
||||
Description: "single value",
|
||||
Input: gin.H{
|
||||
"blip": "some",
|
||||
"blop": "thing",
|
||||
},
|
||||
Expected: gin.H{
|
||||
"::/0": gin.H{
|
||||
"Blip": "some",
|
||||
"Blop": "thing",
|
||||
},
|
||||
},
|
||||
}, {
|
||||
Description: "proper map",
|
||||
Input: gin.H{
|
||||
"::/0": gin.H{
|
||||
"blip": "some",
|
||||
"blop": "thing",
|
||||
},
|
||||
"203.0.113.14": gin.H{
|
||||
"blip": "other",
|
||||
"blop": "stuff",
|
||||
},
|
||||
},
|
||||
Expected: gin.H{
|
||||
"::/0": gin.H{
|
||||
"Blip": "some",
|
||||
"Blop": "thing",
|
||||
},
|
||||
"203.0.113.14/32": gin.H{
|
||||
"Blip": "other",
|
||||
"Blop": "stuff",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.Description, func(t *testing.T) {
|
||||
var tree helpers.SubnetMap[SomeStruct]
|
||||
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||
Result: &tree,
|
||||
ErrorUnused: true,
|
||||
Metadata: nil,
|
||||
DecodeHook: helpers.SubnetMapUnmarshallerHook[SomeStruct](),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewDecoder() error:\n%+v", err)
|
||||
}
|
||||
err = decoder.Decode(tc.Input)
|
||||
if err != nil {
|
||||
t.Fatalf("Decode() error:\n%+v", err)
|
||||
}
|
||||
if diff := helpers.Diff(tree.ToMap(), tc.Expected); diff != "" {
|
||||
t.Fatalf("Decode() (-got, +want):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user