From 959352f945c63122dec670af3d60dd14fbe255e5 Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Tue, 2 Sep 2025 13:06:10 +0800 Subject: [PATCH 1/6] improve gen dao's type map check. --- .../internal/cmd/gendao/gendao_structure.go | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/cmd/gf/internal/cmd/gendao/gendao_structure.go b/cmd/gf/internal/cmd/gendao/gendao_structure.go index e56e72cbcb1..fc11bc3b437 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_structure.go +++ b/cmd/gf/internal/cmd/gendao/gendao_structure.go @@ -59,6 +59,37 @@ func generateStructDefinition(ctx context.Context, in generateStructDefinitionIn return buffer.String(), appendImports } +func getTypeMappingInfo( + ctx context.Context, fieldType string, inTypeMapping map[DBFieldTypeName]CustomAttributeType, +) (typeNameStr, importStr string) { + if typeMapping, ok := inTypeMapping[strings.ToLower(fieldType)]; ok { + typeNameStr = typeMapping.Type + importStr = typeMapping.Import + return + } + tryTypeMatch, _ := gregex.MatchString(`(.+?)\(([^\(\)]+)\)(.*)`, fieldType) + var ( + tryTypeName string + moreTry bool + ) + if len(tryTypeMatch) == 4 { + tryTypeMatch3 := gstr.Trim(tryTypeMatch[3]) + tryTypeName = gstr.Trim(tryTypeMatch[1]) + tryTypeMatch3 + moreTry = tryTypeMatch3 != "" + } else { + tryTypeName = gstr.Split(fieldType, " ")[0] + } + if tryTypeName != "" { + if typeMapping, ok := inTypeMapping[strings.ToLower(tryTypeName)]; ok { + typeNameStr = typeMapping.Type + importStr = typeMapping.Import + } else if moreTry { + typeNameStr, importStr = getTypeMappingInfo(ctx, tryTypeName, inTypeMapping) + } + } + return +} + // generateStructFieldDefinition generates and returns the attribute definition for specified field. func generateStructFieldDefinition( ctx context.Context, field *gdb.TableField, in generateStructDefinitionInput, @@ -71,21 +102,7 @@ func generateStructFieldDefinition( ) if in.TypeMapping != nil && len(in.TypeMapping) > 0 { - var ( - tryTypeName string - ) - tryTypeMatch, _ := gregex.MatchString(`(.+?)\((.+)\)`, field.Type) - if len(tryTypeMatch) == 3 { - tryTypeName = gstr.Trim(tryTypeMatch[1]) - } else { - tryTypeName = gstr.Split(field.Type, " ")[0] - } - if tryTypeName != "" { - if typeMapping, ok := in.TypeMapping[strings.ToLower(tryTypeName)]; ok { - localTypeNameStr = typeMapping.Type - appendImport = typeMapping.Import - } - } + localTypeNameStr, appendImport = getTypeMappingInfo(ctx, field.Type, in.TypeMapping) } if localTypeNameStr == "" { From 6274a416ad08dafa104fed10a658627759a77f9d Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Tue, 2 Sep 2025 13:12:21 +0800 Subject: [PATCH 2/6] improve code --- cmd/gf/internal/cmd/gendao/gendao_structure.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/cmd/gf/internal/cmd/gendao/gendao_structure.go b/cmd/gf/internal/cmd/gendao/gendao_structure.go index fc11bc3b437..e5bea750bd1 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_structure.go +++ b/cmd/gf/internal/cmd/gendao/gendao_structure.go @@ -70,22 +70,14 @@ func getTypeMappingInfo( tryTypeMatch, _ := gregex.MatchString(`(.+?)\(([^\(\)]+)\)(.*)`, fieldType) var ( tryTypeName string - moreTry bool ) if len(tryTypeMatch) == 4 { - tryTypeMatch3 := gstr.Trim(tryTypeMatch[3]) - tryTypeName = gstr.Trim(tryTypeMatch[1]) + tryTypeMatch3 - moreTry = tryTypeMatch3 != "" + tryTypeName = gstr.Trim(tryTypeMatch[1]) + gstr.Trim(tryTypeMatch[3]) } else { tryTypeName = gstr.Split(fieldType, " ")[0] } if tryTypeName != "" { - if typeMapping, ok := inTypeMapping[strings.ToLower(tryTypeName)]; ok { - typeNameStr = typeMapping.Type - importStr = typeMapping.Import - } else if moreTry { - typeNameStr, importStr = getTypeMappingInfo(ctx, tryTypeName, inTypeMapping) - } + typeNameStr, importStr = getTypeMappingInfo(ctx, tryTypeName, inTypeMapping) } return } From 9944334953e6c8fc785f2a07b1f65bc2a5ea4bcd Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Tue, 2 Sep 2025 13:21:54 +0800 Subject: [PATCH 3/6] Revert "improve code" This reverts commit 6274a416ad08dafa104fed10a658627759a77f9d. --- cmd/gf/internal/cmd/gendao/gendao_structure.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/gf/internal/cmd/gendao/gendao_structure.go b/cmd/gf/internal/cmd/gendao/gendao_structure.go index e5bea750bd1..fc11bc3b437 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_structure.go +++ b/cmd/gf/internal/cmd/gendao/gendao_structure.go @@ -70,14 +70,22 @@ func getTypeMappingInfo( tryTypeMatch, _ := gregex.MatchString(`(.+?)\(([^\(\)]+)\)(.*)`, fieldType) var ( tryTypeName string + moreTry bool ) if len(tryTypeMatch) == 4 { - tryTypeName = gstr.Trim(tryTypeMatch[1]) + gstr.Trim(tryTypeMatch[3]) + tryTypeMatch3 := gstr.Trim(tryTypeMatch[3]) + tryTypeName = gstr.Trim(tryTypeMatch[1]) + tryTypeMatch3 + moreTry = tryTypeMatch3 != "" } else { tryTypeName = gstr.Split(fieldType, " ")[0] } if tryTypeName != "" { - typeNameStr, importStr = getTypeMappingInfo(ctx, tryTypeName, inTypeMapping) + if typeMapping, ok := inTypeMapping[strings.ToLower(tryTypeName)]; ok { + typeNameStr = typeMapping.Type + importStr = typeMapping.Import + } else if moreTry { + typeNameStr, importStr = getTypeMappingInfo(ctx, tryTypeName, inTypeMapping) + } } return } From f9d269ce65bf1677ffb9b940e3f7db94304c7ae9 Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Tue, 2 Sep 2025 13:48:29 +0800 Subject: [PATCH 4/6] fixed test unit bug --- cmd/gf/internal/cmd/cmd_z_unit_gen_pb_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gf/internal/cmd/cmd_z_unit_gen_pb_test.go b/cmd/gf/internal/cmd/cmd_z_unit_gen_pb_test.go index 4ba3c179d6d..80cf1e8142e 100644 --- a/cmd/gf/internal/cmd/cmd_z_unit_gen_pb_test.go +++ b/cmd/gf/internal/cmd/cmd_z_unit_gen_pb_test.go @@ -55,7 +55,7 @@ func TestGenPbIssue3882(t *testing.T) { func TestGenPbIssue3953(t *testing.T) { gtest.C(t, func(t *gtest.T) { var ( - outputPath = gfile.Temp(guid.S()) + outputPath = gfile.Temp("f" + guid.S()) outputApiPath = filepath.Join(outputPath, "api") outputCtrlPath = filepath.Join(outputPath, "controller") From e38499e6b779ab1af4b1ee69d0b83270dc68d658 Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Tue, 2 Sep 2025 14:33:13 +0800 Subject: [PATCH 5/6] fix bug --- cmd/gf/internal/cmd/gendao/gendao_structure.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gf/internal/cmd/gendao/gendao_structure.go b/cmd/gf/internal/cmd/gendao/gendao_structure.go index fc11bc3b437..b35fc0ceb96 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_structure.go +++ b/cmd/gf/internal/cmd/gendao/gendao_structure.go @@ -67,7 +67,7 @@ func getTypeMappingInfo( importStr = typeMapping.Import return } - tryTypeMatch, _ := gregex.MatchString(`(.+?)\(([^\(\)]+)\)(.*)`, fieldType) + tryTypeMatch, _ := gregex.MatchString(`(.+?)\(([^\(\)]+)\)([^\s]*)`, fieldType) var ( tryTypeName string moreTry bool From 6131b6e128420e982641bf845ab7f0c5c6cf9cf8 Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Tue, 2 Sep 2025 14:40:10 +0800 Subject: [PATCH 6/6] improve math rule. --- cmd/gf/internal/cmd/gendao/gendao_structure.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/gf/internal/cmd/gendao/gendao_structure.go b/cmd/gf/internal/cmd/gendao/gendao_structure.go index b35fc0ceb96..95084892f72 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_structure.go +++ b/cmd/gf/internal/cmd/gendao/gendao_structure.go @@ -67,13 +67,13 @@ func getTypeMappingInfo( importStr = typeMapping.Import return } - tryTypeMatch, _ := gregex.MatchString(`(.+?)\(([^\(\)]+)\)([^\s]*)`, fieldType) + tryTypeMatch, _ := gregex.MatchString(`(.+?)\(([^\(\)]+)\)([\s\)]*)`, fieldType) var ( tryTypeName string moreTry bool ) if len(tryTypeMatch) == 4 { - tryTypeMatch3 := gstr.Trim(tryTypeMatch[3]) + tryTypeMatch3, _ := gregex.ReplaceString(`\s+`, "", tryTypeMatch[3]) tryTypeName = gstr.Trim(tryTypeMatch[1]) + tryTypeMatch3 moreTry = tryTypeMatch3 != "" } else {