Fix bug on StrucToMap that was including fields not tagged with ksql

pull/10/head
Vinícius Garcia 2021-12-22 19:19:53 -03:00
parent e5e786cf4b
commit e970a3546a
2 changed files with 22 additions and 0 deletions

View File

@ -107,6 +107,10 @@ func StructToMap(obj interface{}) (map[string]interface{}, error) {
m := map[string]interface{}{}
for i := 0; i < v.NumField(); i++ {
if !info.ByIndex(i).Valid {
continue
}
field := v.Field(i)
ft := field.Type()
if ft.Kind() == reflect.Ptr {

View File

@ -67,6 +67,24 @@ func TestStructToMap(t *testing.T) {
assert.Equal(t, nil, err)
assert.Equal(t, map[string]interface{}{}, m)
})
t.Run("should ignore fields not tagged with ksql", func(t *testing.T) {
m, err := StructToMap(struct {
Name string `ksql:"name_attr"`
Age int `ksql:"age_attr"`
NotPartOfTheQuery int
}{
Name: "fake-name",
Age: 42,
NotPartOfTheQuery: 42,
})
assert.Equal(t, nil, err)
assert.Equal(t, map[string]interface{}{
"name_attr": "fake-name",
"age_attr": 42,
}, m)
})
}
func TestFillStructWith(t *testing.T) {