Skip to content

Commit

Permalink
fix: field indexes getting overridden when appending
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Aug 3, 2021
1 parent e7dd58b commit 606f5c0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
11 changes: 9 additions & 2 deletions internal/util/column_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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...)
}
44 changes: 44 additions & 0 deletions issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 606f5c0

Please sign in to comment.