Skip to content

Commit

Permalink
refactor: clean up NewRecrodFromStruct implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed May 4, 2021
1 parent 8436cb1 commit a874395
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions exp/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,30 @@ func NewRecordFromStruct(i interface{}, forInsert, forUpdate bool) (r Record, er
r = make(map[string]interface{}, len(cols))
for _, col := range cols {
f := cm[col]
switch {
case forInsert:
if f.ShouldInsert {
addFieldToRecord(r, value, f)
if !shouldSkipField(f, forInsert, forUpdate) {
if ok, fieldVal := getFieldValue(value, f); ok {
r[f.ColumnName] = fieldVal
}
case forUpdate:
if f.ShouldUpdate {
addFieldToRecord(r, value, f)
}
default:
addFieldToRecord(r, value, f)
}
}
}
return
}

func addFieldToRecord(r Record, val reflect.Value, f util.ColumnData) {
v, isAvailable := util.SafeGetFieldByIndex(val, f.FieldIndex)
if !isAvailable {
return
}
switch {
case f.DefaultIfEmpty && util.IsEmptyValue(v):
r[f.ColumnName] = Default()
case v.IsValid():
r[f.ColumnName] = v.Interface()
default:
r[f.ColumnName] = reflect.Zero(f.GoType).Interface()
func shouldSkipField(f util.ColumnData, forInsert, forUpdate bool) bool {
shouldSkipInsert := forInsert && !f.ShouldInsert
shouldSkipUpdate := forUpdate && !f.ShouldUpdate
return shouldSkipInsert || shouldSkipUpdate
}

func getFieldValue(val reflect.Value, f util.ColumnData) (ok bool, fieldVal interface{}) {
if v, isAvailable := util.SafeGetFieldByIndex(val, f.FieldIndex); !isAvailable {
return false, nil
} else if f.DefaultIfEmpty && util.IsEmptyValue(v) {
return true, Default()
} else if v.IsValid() {
return true, v.Interface()
} else {
return true, reflect.Zero(f.GoType).Interface()
}
}

0 comments on commit a874395

Please sign in to comment.