diff --git a/callbacks.go b/callbacks.go index 50b5b0e9..e2c297d6 100644 --- a/callbacks.go +++ b/callbacks.go @@ -121,6 +121,28 @@ func (p *processor) Execute(db *DB) *DB { stmt.ReflectValue = stmt.ReflectValue.Elem() } + if (stmt.ReflectValue.Kind() == reflect.Slice || stmt.ReflectValue.Kind() == reflect.Array) && + (stmt.ReflectValue.Len() > 0 || stmt.ReflectValue.Index(0).Kind() == reflect.Interface) { + len := stmt.ReflectValue.Len() + firstElem := stmt.ReflectValue.Index(0) + for firstElem.Kind() == reflect.Interface || firstElem.Kind() == reflect.Ptr { + firstElem = firstElem.Elem() + } + elemType := firstElem.Type() + sliceType := reflect.SliceOf(elemType) + structArrayReflectValue := reflect.MakeSlice(sliceType, 0, len) + + for i := 0; i < len; i++ { + elem := stmt.ReflectValue.Index(i) + for elem.Kind() == reflect.Interface || elem.Kind() == reflect.Ptr { + elem = elem.Elem() + } + structArrayReflectValue = reflect.Append(structArrayReflectValue, elem) + } + stmt.ReflectValue = structArrayReflectValue + fmt.Println(stmt.ReflectValue.Type()) + } + if !stmt.ReflectValue.IsValid() { db.AddError(ErrInvalidValue) } diff --git a/schema/schema.go b/schema/schema.go index db236797..01bc4c63 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -138,6 +138,15 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam modelType = modelType.Elem() } + if modelType.Kind() == reflect.Interface { + if value.Len() > 0 { + modelType = reflect.Indirect(value.Index(0)).Elem().Type() + } + if modelType.Kind() == reflect.Ptr { + modelType = modelType.Elem() + } + } + if modelType.Kind() != reflect.Struct { if modelType.PkgPath() == "" { return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, dest)