Skip to content

Commit

Permalink
Merge branch 'master' into bowen/#178
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl authored Jul 3, 2023
2 parents 34fce48 + e8b7d03 commit 0e73682
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ This project exists thanks to all the people who contribute, to participate in t
<a href="https://github.com/dragoonchang" target="_blank"><img src="https://avatars.githubusercontent.com/u/1432336?v=4" width="48" height="48"></a>
<a href="https://github.com/dhanusaputra" target="_blank"><img src="https://avatars.githubusercontent.com/u/35093673?v=4" width="48" height="48"></a>
<a href="https://github.com/mauri870" target="_blank"><img src="https://avatars.githubusercontent.com/u/10168637?v=4" width="48" height="48"></a>
<a href="https://github.com/Marian0" target="_blank"><img src="https://avatars.githubusercontent.com/u/624592?v=4" width="48" height="48"></a>
<a href="https://github.com/ahmed3mar" target="_blank"><img src="https://avatars.githubusercontent.com/u/12982325?v=4" width="48" height="48"></a>

## Group

Expand Down
2 changes: 2 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Laravel!
<a href="https://github.com/dragoonchang" target="_blank"><img src="https://avatars.githubusercontent.com/u/1432336?v=4" width="48" height="48"></a>
<a href="https://github.com/dhanusaputra" target="_blank"><img src="https://avatars.githubusercontent.com/u/35093673?v=4" width="48" height="48"></a>
<a href="https://github.com/mauri870" target="_blank"><img src="https://avatars.githubusercontent.com/u/10168637?v=4" width="48" height="48"></a>
<a href="https://github.com/Marian0" target="_blank"><img src="https://avatars.githubusercontent.com/u/624592?v=4" width="48" height="48"></a>
<a href="https://github.com/ahmed3mar" target="_blank"><img src="https://avatars.githubusercontent.com/u/12982325?v=4" width="48" height="48"></a>

## 群组

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion support/constant.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package support

const Version string = "v1.12.4"
const Version string = "v1.12.5"

const (
EnvRuntime = "runtime"
Expand Down
2 changes: 1 addition & 1 deletion support/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
73 changes: 68 additions & 5 deletions support/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"testing"

"github.com/google/uuid"
"github.com/goravel/framework/database/orm"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 0e73682

Please sign in to comment.