Create supports Array / ArrayPtr (#3076)

* add Array / ArrayPtr create tests

* support create using array
This commit is contained in:
Hinagiku Soranoba 2020-06-23 15:38:36 +09:00 committed by GitHub
parent 1df757113a
commit b733d16f56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 1 deletions

View File

@ -73,7 +73,7 @@ type Tabler interface {
// get data type from dialector // get data type from dialector
func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) { func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) {
modelType := reflect.ValueOf(dest).Type() modelType := reflect.ValueOf(dest).Type()
for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Ptr { for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Array || modelType.Kind() == reflect.Ptr {
modelType = modelType.Elem() modelType = modelType.Elem()
} }

View File

@ -189,6 +189,48 @@ func TestPolymorphicHasOne(t *testing.T) {
CheckPet(t, *pet, *pet) CheckPet(t, *pet, *pet)
} }
}) })
t.Run("Array", func(t *testing.T) {
var pets = [...]Pet{{
Name: "PolymorphicHasOne-Array-1",
Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-1"},
}, {
Name: "PolymorphicHasOne-Array-2",
Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-2"},
}, {
Name: "PolymorphicHasOne-Array-3",
Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-3"},
}}
if err := DB.Create(&pets).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
for _, pet := range pets {
CheckPet(t, pet, pet)
}
})
t.Run("ArrayPtr", func(t *testing.T) {
var pets = [...]*Pet{{
Name: "PolymorphicHasOne-Array-1",
Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-1"},
}, {
Name: "PolymorphicHasOne-Array-2",
Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-2"},
}, {
Name: "PolymorphicHasOne-Array-3",
Toy: Toy{Name: "Toy-PolymorphicHasOne-Array-3"},
}}
if err := DB.Create(&pets).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
for _, pet := range pets {
CheckPet(t, *pet, *pet)
}
})
} }
func TestCreateEmptyStruct(t *testing.T) { func TestCreateEmptyStruct(t *testing.T) {