diff --git a/internal/util/column_map.go b/internal/util/column_map.go index d41d4d42..b8269341 100644 --- a/internal/util/column_map.go +++ b/internal/util/column_map.go @@ -91,13 +91,13 @@ func newColumnData(f *reflect.StructField, columnName string, fieldIndex []int, ShouldInsert: !goquTag.Contains(skipInsertTagName), ShouldUpdate: !goquTag.Contains(skipUpdateTagName), DefaultIfEmpty: goquTag.Contains(defaultIfEmptyTagName), - FieldIndex: append(fieldIndex, f.Index...), + FieldIndex: concatFieldIndexes(fieldIndex, f.Index), GoType: f.Type, } } func getStructColumnMap(f *reflect.StructField, fieldIndex []int, fieldNames, prefixes []string) ColumnMap { - subFieldIndexes := append(fieldIndex, f.Index...) + subFieldIndexes := concatFieldIndexes(fieldIndex, f.Index) subPrefixes := append(prefixes, fieldNames...) if f.Type.Kind() == reflect.Ptr { return newColumnMap(f.Type.Elem(), subFieldIndexes, subPrefixes) @@ -121,3 +121,10 @@ func shouldIgnoreField(dbTag tag.Options) bool { return false } + +// safely concat two fieldIndex slices into one. +func concatFieldIndexes(fieldIndexPath, fieldIndex []int) []int { + fieldIndexes := make([]int, 0, len(fieldIndexPath)+len(fieldIndex)) + fieldIndexes = append(fieldIndexes, fieldIndexPath...) + return append(fieldIndexes, fieldIndex...) +} diff --git a/issues_test.go b/issues_test.go index 9a283725..60848c7f 100644 --- a/issues_test.go +++ b/issues_test.go @@ -450,6 +450,50 @@ func (gis *githubIssuesSuite) TestIssue203() { gis.Empty(args, []interface{}{}) } +func (gis *githubIssuesSuite) TestIssue290() { + type OcomModel struct { + ID uint `json:"id" db:"id" goqu:"skipinsert"` + CreatedDate time.Time `json:"created_date" db:"created_date" goqu:"skipupdate"` + ModifiedDate time.Time `json:"modified_date" db:"modified_date"` + } + + type ActiveModel struct { + OcomModel + ActiveStartDate time.Time `json:"active_start_date" db:"active_start_date"` + ActiveEndDate *time.Time `json:"active_end_date" db:"active_end_date"` + } + + type CodeModel struct { + ActiveModel + + Code string `json:"code" db:"code"` + Description string `json:"description" binding:"required" db:"description"` + } + + type CodeExample struct { + CodeModel + } + + var item CodeExample + item.Code = "Code" + item.Description = "Description" + item.ID = 1 // Value set HERE! + item.CreatedDate = time.Date( + 2021, 1, 1, 1, 1, 1, 1, time.UTC) + item.ModifiedDate = time.Date( + 2021, 2, 2, 2, 2, 2, 2, time.UTC) // The Value we Get! + item.ActiveStartDate = time.Date( + 2021, 3, 3, 3, 3, 3, 3, time.UTC) + + updateQuery := goqu.From("example").Update().Set(item).Where(goqu.C("id").Eq(1)) + + sql, params, err := updateQuery.ToSQL() + + gis.NoError(err) + gis.Empty(params) + gis.Equal(`UPDATE "example" SET "active_end_date"=NULL,"active_start_date"='2021-03-03T03:03:03.000000003Z',"code"='Code',"description"='Description',"id"=1,"modified_date"='2021-02-02T02:02:02.000000002Z' WHERE ("id" = 1)`, sql) //nolint:lll +} + func TestGithubIssuesSuite(t *testing.T) { suite.Run(t, new(githubIssuesSuite)) }