Skip to content

Commit 606f5c0

Browse files
committed
fix: field indexes getting overridden when appending
1 parent e7dd58b commit 606f5c0

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

internal/util/column_map.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ func newColumnData(f *reflect.StructField, columnName string, fieldIndex []int,
9191
ShouldInsert: !goquTag.Contains(skipInsertTagName),
9292
ShouldUpdate: !goquTag.Contains(skipUpdateTagName),
9393
DefaultIfEmpty: goquTag.Contains(defaultIfEmptyTagName),
94-
FieldIndex: append(fieldIndex, f.Index...),
94+
FieldIndex: concatFieldIndexes(fieldIndex, f.Index),
9595
GoType: f.Type,
9696
}
9797
}
9898

9999
func getStructColumnMap(f *reflect.StructField, fieldIndex []int, fieldNames, prefixes []string) ColumnMap {
100-
subFieldIndexes := append(fieldIndex, f.Index...)
100+
subFieldIndexes := concatFieldIndexes(fieldIndex, f.Index)
101101
subPrefixes := append(prefixes, fieldNames...)
102102
if f.Type.Kind() == reflect.Ptr {
103103
return newColumnMap(f.Type.Elem(), subFieldIndexes, subPrefixes)
@@ -121,3 +121,10 @@ func shouldIgnoreField(dbTag tag.Options) bool {
121121

122122
return false
123123
}
124+
125+
// safely concat two fieldIndex slices into one.
126+
func concatFieldIndexes(fieldIndexPath, fieldIndex []int) []int {
127+
fieldIndexes := make([]int, 0, len(fieldIndexPath)+len(fieldIndex))
128+
fieldIndexes = append(fieldIndexes, fieldIndexPath...)
129+
return append(fieldIndexes, fieldIndex...)
130+
}

issues_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,50 @@ func (gis *githubIssuesSuite) TestIssue203() {
450450
gis.Empty(args, []interface{}{})
451451
}
452452

453+
func (gis *githubIssuesSuite) TestIssue290() {
454+
type OcomModel struct {
455+
ID uint `json:"id" db:"id" goqu:"skipinsert"`
456+
CreatedDate time.Time `json:"created_date" db:"created_date" goqu:"skipupdate"`
457+
ModifiedDate time.Time `json:"modified_date" db:"modified_date"`
458+
}
459+
460+
type ActiveModel struct {
461+
OcomModel
462+
ActiveStartDate time.Time `json:"active_start_date" db:"active_start_date"`
463+
ActiveEndDate *time.Time `json:"active_end_date" db:"active_end_date"`
464+
}
465+
466+
type CodeModel struct {
467+
ActiveModel
468+
469+
Code string `json:"code" db:"code"`
470+
Description string `json:"description" binding:"required" db:"description"`
471+
}
472+
473+
type CodeExample struct {
474+
CodeModel
475+
}
476+
477+
var item CodeExample
478+
item.Code = "Code"
479+
item.Description = "Description"
480+
item.ID = 1 // Value set HERE!
481+
item.CreatedDate = time.Date(
482+
2021, 1, 1, 1, 1, 1, 1, time.UTC)
483+
item.ModifiedDate = time.Date(
484+
2021, 2, 2, 2, 2, 2, 2, time.UTC) // The Value we Get!
485+
item.ActiveStartDate = time.Date(
486+
2021, 3, 3, 3, 3, 3, 3, time.UTC)
487+
488+
updateQuery := goqu.From("example").Update().Set(item).Where(goqu.C("id").Eq(1))
489+
490+
sql, params, err := updateQuery.ToSQL()
491+
492+
gis.NoError(err)
493+
gis.Empty(params)
494+
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
495+
}
496+
453497
func TestGithubIssuesSuite(t *testing.T) {
454498
suite.Run(t, new(githubIssuesSuite))
455499
}

0 commit comments

Comments
 (0)