From ea7a23f32f89e857b918e3616208c91725f2f11c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Garcia?= Date: Tue, 12 Jan 2021 22:34:46 -0300 Subject: [PATCH] Fix bug on FillStructWith so it ignores extra columns correctly This bug was not being detected before because depending on the order of the attributes on a map instance this test would succeed by accident. I was lucky enough to reproduce this error enough times to understand it so it was possible to fix it. --- structs.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/structs.go b/structs.go index d1d270e..efadb15 100644 --- a/structs.go +++ b/structs.go @@ -81,10 +81,15 @@ func FillStructWith(record interface{}, dbRow map[string]interface{}) error { } info := getCachedTagInfo(tagInfoCache, t) - for colName, rawSrc := range dbRow { + fieldIdx, found := info.Index[colName] + if !found { + // Ignore columns not tagged with `kissorm:""` + continue + } + src := NewPtrConverter(rawSrc) - dest := v.Field(info.Index[colName]) + dest := v.Field(fieldIdx) destType := t.Field(info.Index[colName]).Type destValue, err := src.Convert(destType)