forked from mirror/gorm
Create supports Array / ArrayPtr (#3076)
* add Array / ArrayPtr create tests * support create using array
This commit is contained in:
parent
1df757113a
commit
b733d16f56
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue