Skip to content

Commit

Permalink
Merge pull request #90 from suyuan32/dev
Browse files Browse the repository at this point in the history
fix: optimize ent id converter
  • Loading branch information
suyuan32 authored Apr 6, 2024
2 parents e797ac1 + 240e3ba commit 33b5e54
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 2 deletions.
25 changes: 23 additions & 2 deletions util/entx/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package entx

import (
"regexp"
"strings"

"github.com/suyuan32/goctls/rpc/parser"
Expand Down Expand Up @@ -88,15 +89,35 @@ func ConvertSpecificNounToUpper(str string) string {
target = strings.Replace(target, v.Origin, v.Target, -1)
}

if !strings.Contains(target, "Ids") {
target = ConvertIdFieldToUpper(target)

return target
}

// ConvertIdFieldToUpper is used to convert snack format Id to Ent format
func ConvertIdFieldToUpper(target string) string {
if IsNotIDField(target) {
if strings.Contains(target, "Id") {
target = strings.Replace(target, "Id", "ID", -1)
}
} else {
if strings.HasSuffix(target, "Id") {
target = target[:len(target)-1] + "D"
}
}

return target
}

// IsNotIDField Judge whether the field is not an ID field
func IsNotIDField(field string) bool {
compile, err := regexp.Compile("Id[a-z]+/g")
if err != nil {
return false
}

return compile.MatchString(field)
}

// ConvertEntTypeToGotype returns go type from ent type
func ConvertEntTypeToGotype(prop string) string {
switch prop {
Expand Down
165 changes: 165 additions & 0 deletions util/entx/converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package entx

import "testing"

func TestIsIDField(t *testing.T) {
type args struct {
field string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "test 1",
args: args{
field: "Id",
},
want: false,
},
{
name: "test 2",
args: args{
field: "Ids",
},
want: true,
},
{
name: "test 3",
args: args{
field: "Idx",
},
want: true,
},
{
name: "test 4",
args: args{
field: "UserId",
},
want: false,
},
{
name: "test 5",
args: args{
field: "IdxId",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsNotIDField(tt.args.field); got != tt.want {
t.Errorf("IsIDField() = %v, want %v", got, tt.want)
}
})
}
}

func TestConvertIdFieldToUpper(t *testing.T) {
type args struct {
target string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test 1",
args: args{
target: "Id",
},
want: "ID",
},
{
name: "test 2",
args: args{
target: "UserId",
},
want: "UserID",
},
{
name: "test 3",
args: args{
target: "Identity",
},
want: "Identity",
},
{
name: "test 4",
args: args{
target: "Idx",
},
want: "Idx",
},
{
name: "test 5",
args: args{
target: "IdxId",
},
want: "IdxID",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ConvertIdFieldToUpper(tt.args.target); got != tt.want {
t.Errorf("ConvertIdFieldToUpper() = %v, want %v", got, tt.want)
}
})
}
}

func TestConvertSpecificNounToUpper(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test 1",
args: args{
str: "IdentityUuid",
},
want: "IdentityUUID",
},
{
name: "test 2",
args: args{
str: "IdxApi",
},
want: "IdxAPI",
},
{
name: "test 3",
args: args{
str: "IdxApi",
},
want: "IdxAPI",
},
{
name: "test 4",
args: args{
str: "TestUrl",
},
want: "TestURL",
},
{
name: "test 5",
args: args{
str: "IdxId",
},
want: "IdxID",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ConvertSpecificNounToUpper(tt.args.str); got != tt.want {
t.Errorf("ConvertSpecificNounToUpper() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 33b5e54

Please sign in to comment.