Skip to content

Commit

Permalink
refactor: renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Eyo Chen committed Aug 3, 2024
1 parent 80197e5 commit fdb1351
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ func (f *Factory[T]) setNonZeroValues(v interface{}) {

// handle slice
if curField.Type.Kind() == reflect.Slice {
f.setNonZeroValuesForSlice(curVal.Addr().Interface())
f.setNonZeroSlice(curVal.Addr().Interface())
continue
}

// handle pointer to slice
if curField.Type.Kind() == reflect.Ptr && curField.Type.Elem().Kind() == reflect.Slice {
newInstance := reflect.New(curField.Type.Elem()).Elem()
f.setNonZeroValuesForSlice(newInstance.Addr().Interface())
f.setNonZeroSlice(newInstance.Addr().Interface())
curVal.Set(newInstance.Addr())
continue
}
Expand All @@ -93,23 +93,23 @@ func (f *Factory[T]) setNonZeroValues(v interface{}) {
}
}

// setNonZeroValuesForSlice sets non-zero values to the given slice.
// setNonZeroSlice sets non-zero values to the given slice.
// Parameter v must be a pointer to a slice
func (f *Factory[T]) setNonZeroValuesForSlice(v interface{}) {
func (f *Factory[T]) setNonZeroSlice(v interface{}) {
val := reflect.ValueOf(v).Elem()

// handle slice
if val.Type().Elem().Kind() == reflect.Slice {
e := reflect.New(val.Type().Elem()).Elem()
f.setNonZeroValuesForSlice(e.Addr().Interface())
f.setNonZeroSlice(e.Addr().Interface())
val.Set(reflect.Append(val, e))
return
}

// handle slice of pointers
if val.Type().Elem().Kind() == reflect.Ptr && val.Type().Elem().Elem().Kind() == reflect.Slice {
e := reflect.New(val.Type().Elem().Elem()).Elem()
f.setNonZeroValuesForSlice(e.Addr().Interface())
f.setNonZeroSlice(e.Addr().Interface())
val.Set(reflect.Append(val, e.Addr()))
return
}
Expand Down Expand Up @@ -168,10 +168,6 @@ func (f *Factory[T]) insertAss(ctx context.Context) error {
return errors.New("tagToInfo is not set")
}

if len(f.associations) == 0 {
return errors.New("inserting associations without any associations")
}

for name, vals := range f.associations {
tableName := f.tagToInfo[name].tableName
if _, err := f.db.InsertList(ctx, db.InserListParams{StorageName: tableName, Values: vals}); err != nil {
Expand Down

0 comments on commit fdb1351

Please sign in to comment.