From e8b7d03f2514ff051ee423202e7bd3caf8ff8307 Mon Sep 17 00:00:00 2001 From: Wenbo Han Date: Mon, 3 Jul 2023 16:51:10 +0800 Subject: [PATCH] release: Upgrade v1.12.5 (#228) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: path error in Windows (#193) * Update README * fix: path error in Windows (#192) --------- Co-authored-by: 耗子 * Upgrade v1.12.1 (#197) * Update README * fix: path error in Windows (#192) * feat: remove promptui (#194) * feat: remove promptui * fix: lint * fix: lint * Update README (#195) --------- Co-authored-by: 耗子 * Fix: MakeGate error (#199) * Fix: MakeGate error * Update print * Upgrade v1.12.3 (#202) * Fix the problem of package facades path * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Upgrade v1.12.3 * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize action * Optimize README (#204) * Feat: Fix throttle middleware panic (#214) * Feat: Fix throttle middleware error * Upgrade v1.12.4 * fix: Tentative fix for Lazy Eager Loading with uuids (#226) * tentative fix for Lazy Eager Loading with uuids * added some unit tests for TestGetIDByReflect * Add new contributors * Upgrade v1.12.5 * Upgrade v1.12.5 --------- Co-authored-by: 耗子 Co-authored-by: Mariano Peyregne --- README.md | 2 + README_zh.md | 2 + go.mod | 2 +- support/constant.go | 2 +- support/database/database.go | 2 +- support/database/database_test.go | 73 ++++++++++++++++++++++++++++--- 6 files changed, 75 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 210eaf58a..79df3b3ba 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,8 @@ This project exists thanks to all the people who contribute, to participate in t + + ## Group diff --git a/README_zh.md b/README_zh.md index 62af0f4bd..a00d346bf 100644 --- a/README_zh.md +++ b/README_zh.md @@ -63,6 +63,8 @@ Laravel! + + ## 群组 diff --git a/go.mod b/go.mod index af987429d..a816e9dba 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/golang-migrate/migrate/v4 v4.16.2 github.com/golang-module/carbon/v2 v2.2.3 github.com/golang/protobuf v1.5.3 + github.com/google/uuid v1.3.0 github.com/google/wire v0.5.0 github.com/gookit/color v1.5.3 github.com/gookit/validate v1.4.6 @@ -90,7 +91,6 @@ require ( github.com/google/go-cmp v0.5.9 // indirect github.com/google/s2a-go v0.1.3 // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect - github.com/google/uuid v1.3.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect github.com/googleapis/gax-go/v2 v2.8.0 // indirect github.com/gookit/filter v1.1.4 // indirect diff --git a/support/constant.go b/support/constant.go index 09a3487cb..20523941b 100644 --- a/support/constant.go +++ b/support/constant.go @@ -1,6 +1,6 @@ package support -const Version string = "v1.12.4" +const Version string = "v1.12.5" const ( EnvRuntime = "runtime" diff --git a/support/database/database.go b/support/database/database.go index 12dcbaf8a..c66530cf1 100644 --- a/support/database/database.go +++ b/support/database/database.go @@ -39,7 +39,7 @@ func GetIDByReflect(t reflect.Type, v reflect.Value) any { } if strings.Contains(t.Field(i).Tag.Get("gorm"), "primaryKey") { id := v.Field(i).Interface() - if cast.ToString(id) == "" || cast.ToInt(id) == 0 { + if cast.ToString(id) == "" && cast.ToInt(id) == 0 { return nil } diff --git a/support/database/database_test.go b/support/database/database_test.go index 77a00e730..4cee5db82 100644 --- a/support/database/database_test.go +++ b/support/database/database_test.go @@ -4,6 +4,7 @@ import ( "reflect" "testing" + "github.com/google/uuid" "github.com/goravel/framework/database/orm" "github.com/stretchr/testify/assert" @@ -109,12 +110,74 @@ type TestStruct struct { ID int `gorm:"primaryKey"` } +type TestStructString struct { + ID string `gorm:"primaryKey"` +} + +type TestStructUUID struct { + ID uuid.UUID `gorm:"primaryKey"` +} + +type TestStructNoPK struct { + ID int +} + func TestGetIDByReflect(t *testing.T) { - ts := TestStruct{ID: 1} - v := reflect.ValueOf(ts) - tpe := reflect.TypeOf(ts) + tests := []struct { + description string + setup func(description string) + }{ + { + description: "TestStruct.ID type int", + setup: func(description string) { + ts := TestStruct{ID: 1} + v := reflect.ValueOf(ts) + tpe := reflect.TypeOf(ts) + + result := GetIDByReflect(tpe, v) + + assert.Equal(t, 1, result) + }, + }, + { + description: "TestStruct.ID type string", + setup: func(description string) { + ts := TestStructString{ID: "goravel"} + v := reflect.ValueOf(ts) + tpe := reflect.TypeOf(ts) - result := GetIDByReflect(tpe, v) + result := GetIDByReflect(tpe, v) - assert.Equal(t, 1, result) + assert.Equal(t, "goravel", result) + }, + }, + { + description: "TestStruct.ID type UUID", + setup: func(description string) { + id := uuid.New() + ts := TestStructUUID{ID: id} + v := reflect.ValueOf(ts) + tpe := reflect.TypeOf(ts) + + result := GetIDByReflect(tpe, v) + + assert.Equal(t, id, result) + }, + }, + { + description: "TestStruct without primaryKey", + setup: func(description string) { + ts := TestStructNoPK{ID: 1} + v := reflect.ValueOf(ts) + tpe := reflect.TypeOf(ts) + + result := GetIDByReflect(tpe, v) + + assert.Nil(t, result) + }, + }, + } + for _, test := range tests { + test.setup(test.description) + } }