mirror of https://github.com/VinGarcia/ksql.git
Fix bug on StrucToMap that was including fields not tagged with ksql
parent
e5e786cf4b
commit
e970a3546a
|
@ -107,6 +107,10 @@ func StructToMap(obj interface{}) (map[string]interface{}, error) {
|
||||||
|
|
||||||
m := map[string]interface{}{}
|
m := map[string]interface{}{}
|
||||||
for i := 0; i < v.NumField(); i++ {
|
for i := 0; i < v.NumField(); i++ {
|
||||||
|
if !info.ByIndex(i).Valid {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
field := v.Field(i)
|
field := v.Field(i)
|
||||||
ft := field.Type()
|
ft := field.Type()
|
||||||
if ft.Kind() == reflect.Ptr {
|
if ft.Kind() == reflect.Ptr {
|
||||||
|
|
|
@ -67,6 +67,24 @@ func TestStructToMap(t *testing.T) {
|
||||||
assert.Equal(t, nil, err)
|
assert.Equal(t, nil, err)
|
||||||
assert.Equal(t, map[string]interface{}{}, m)
|
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) {
|
func TestFillStructWith(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue