Skip to content

Commit 717019a

Browse files
committed
fixed structFieldValues not returning readonly fields
1 parent a32d5e1 commit 717019a

File tree

4 files changed

+10
-16
lines changed

4 files changed

+10
-16
lines changed

impl/insert.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,7 @@ func insertStructValues(table string, rowStruct interface{}, namer sqldb.StructF
134134
return nil, nil, fmt.Errorf("InsertStruct into table %s: expected struct but got %T", table, rowStruct)
135135
}
136136

137-
columns, flags, vals := structFields(v, namer, ignoreColumns, restrictToColumns, false)
138-
for i := 0; i < len(columns); i++ {
139-
// Remove readonly column data
140-
if flags[i].IsReadOnly() {
141-
columns = append(columns[:i], columns[i+1:]...)
142-
flags = append(flags[:i], flags[i+1:]...)
143-
vals = append(vals[:i], vals[i+1:]...)
144-
i--
145-
}
146-
}
137+
columns, _, vals = structFieldValues(v, namer, ignoreColumns, restrictToColumns, false)
147138
if len(columns) == 0 {
148139
return nil, nil, fmt.Errorf("InsertStruct into table %s: %T has no exported struct fields with `db` tag", table, rowStruct)
149140
}

impl/scanstruct.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,25 @@ func getStructFieldPointers(v reflect.Value, namer sqldb.StructFieldNamer, ignor
9696
return nil
9797
}
9898

99-
// structFields returns the struct field names using the passed namer ignoring names in ignoreNames
99+
// structFieldValues returns the struct field names using the passed namer ignoring names in ignoreNames
100100
// and if restrictToNames is not empty, then filtering out names not in it.
101+
// struct fields with ,readonly suffix in their struct field naming tag will not be returned
102+
// because this function is intended for getting struct values for writing.
101103
// If true is passed for keepPK, then ignoreNames and restrictToNames are not applied to names with
102104
// the ,pk suffix in their struct field naming tag.
103105
// The same number of pkCol bools will be returend as names, every corresponding bool marking
104106
// if the name had the ,pk suffix in their struct field naming tag.
105-
func structFields(v reflect.Value, namer sqldb.StructFieldNamer, ignoreNames, restrictToNames []string, keepPK bool) (names []string, flags []sqldb.FieldFlag, vals []interface{}) {
107+
// If false is passed for keepReadOnly then
108+
func structFieldValues(v reflect.Value, namer sqldb.StructFieldNamer, ignoreNames, restrictToNames []string, keepPK bool) (names []string, flags []sqldb.FieldFlag, vals []interface{}) {
106109
for i := 0; i < v.NumField(); i++ {
107110
field := v.Type().Field(i)
108111
name, flag, ok := namer.StructFieldName(field)
109-
if !ok {
112+
if !ok || flag.IsReadOnly() {
110113
continue
111114
}
112115

113116
if field.Anonymous {
114-
embedNames, embedFlags, embedValues := structFields(v.Field(i), namer, ignoreNames, restrictToNames, keepPK)
117+
embedNames, embedFlags, embedValues := structFieldValues(v.Field(i), namer, ignoreNames, restrictToNames, keepPK)
115118
names = append(names, embedNames...)
116119
flags = append(flags, embedFlags...)
117120
vals = append(vals, embedValues...)

impl/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func UpdateStruct(conn sqldb.Connection, table string, rowStruct interface{}, na
7676
return fmt.Errorf("UpdateStruct of table %s: expected struct but got %T", table, rowStruct)
7777
}
7878

79-
columns, flags, vals := structFields(v, namer, ignoreColumns, restrictToColumns, true)
79+
columns, flags, vals := structFieldValues(v, namer, ignoreColumns, restrictToColumns, true)
8080
if len(columns) == 0 {
8181
return fmt.Errorf("UpdateStruct of table %s: %T has no exported struct fields with `db` tag", table, rowStruct)
8282
}

impl/upsert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func UpsertStruct(conn sqldb.Connection, table string, rowStruct interface{}, na
2626
return fmt.Errorf("UpsertStruct to table %s: expected struct but got %T", table, rowStruct)
2727
}
2828

29-
columns, flags, vals := structFields(v, namer, ignoreColumns, restrictToColumns, true)
29+
columns, flags, vals := structFieldValues(v, namer, ignoreColumns, restrictToColumns, true)
3030
if len(columns) == 0 {
3131
return fmt.Errorf("UpsertStruct to table %s: %T has no exported struct fields with `db` tag", table, rowStruct)
3232
}

0 commit comments

Comments
 (0)