From b733d16f56bcc79ab68903bd6f028c521da2b6e7 Mon Sep 17 00:00:00 2001 From: Hinagiku Soranoba Date: Tue, 23 Jun 2020 15:38:36 +0900 Subject: [PATCH] Create supports Array / ArrayPtr (#3076) * add Array / ArrayPtr create tests * support create using array --- schema/schema.go | 2 +- tests/create_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/schema/schema.go b/schema/schema.go index e5894443..72bc6544 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -73,7 +73,7 @@ type Tabler interface { // get data type from dialector func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) { 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() } diff --git a/tests/create_test.go b/tests/create_test.go index 4bf623b3..75059f18 100644 --- a/tests/create_test.go +++ b/tests/create_test.go @@ -189,6 +189,48 @@ func TestPolymorphicHasOne(t *testing.T) { 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) {