Merge 8492af206f1e7a4eee7c38b28fd56999f6ea68ac into 4e34a6d21b63e9a9b701a70be9759e5539bf26e9

This commit is contained in:
KEHyeon 2025-08-24 18:51:15 +08:00 committed by GitHub
commit 111d5aaf64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 0 deletions

View File

@ -121,6 +121,26 @@ 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
}
if !stmt.ReflectValue.IsValid() {
db.AddError(ErrInvalidValue)
}

View File

@ -135,6 +135,14 @@ 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.Kind() == reflect.Interface {
modelType = reflect.Indirect(reflect.ValueOf(dest)).Elem().Type()

View File

@ -808,3 +808,15 @@ func TestCreateFromMapWithTable(t *testing.T) {
t.Errorf("failed to create data from map with table, @id != id")
}
}
func TestCreateWithInterfaceArrayType(t *testing.T) {
user := *GetUser("create", Config{})
type UserInterface interface{}
var userInterface UserInterface = &user
if results := DB.Create([]UserInterface{userInterface}); results.Error != nil {
t.Fatalf("errors happened when create: %v", results.Error)
} else if results.RowsAffected != 1 {
t.Fatalf("rows affected expects: %v, got %v", 1, results.RowsAffected)
}
}