feat: deduplication

This commit is contained in:
2025-01-04 23:48:20 +07:00
parent 50979ed70e
commit 29e12da89f
3 changed files with 12 additions and 5 deletions

1
.idea/vcs.xml generated
View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/cmd/crudgen" vcs="Git" /> <mapping directory="$PROJECT_DIR$/cmd/crudgen" vcs="Git" />
</component> </component>
</project> </project>

BIN
cmd/crudgen/crudgen Executable file

Binary file not shown.

View File

@@ -17,14 +17,16 @@ import (
func ImplementServiceStruct(entityName string, file *ast.File, reimplement bool) { func ImplementServiceStruct(entityName string, file *ast.File, reimplement bool) {
serviceName := entityName + "Service" serviceName := entityName + "Service"
isServiceStructDeclarated := false isServiceStructDefined := false
var insertPos int var insertPos int
var decls []ast.Decl
for i, decl := range file.Decls { for i, decl := range file.Decls {
genDecl, ok := decl.(*ast.GenDecl) genDecl, ok := decl.(*ast.GenDecl)
if !ok { if !ok {
continue continue
} }
decls = append(decls, decl)
if genDecl.Tok == token.IMPORT { if genDecl.Tok == token.IMPORT {
insertPos = i + 1 insertPos = i + 1
continue continue
@@ -40,14 +42,16 @@ func ImplementServiceStruct(entityName string, file *ast.File, reimplement bool)
} }
if _, ok := typeSpec.Type.(*ast.StructType); ok { if _, ok := typeSpec.Type.(*ast.StructType); ok {
if typeSpec.Name != nil && typeSpec.Name.Name == serviceName { if typeSpec.Name != nil && typeSpec.Name.Name == serviceName {
isServiceStructDeclarated = true isServiceStructDefined = true
if reimplement {
decls = decls[:1]
}
}
}
} }
} }
} if isServiceStructDefined && !reimplement {
}
if isServiceStructDeclarated && !reimplement {
return return
} }
@@ -63,7 +67,7 @@ func ImplementServiceStruct(entityName string, file *ast.File, reimplement bool)
}, },
} }
file.Decls = append(file.Decls[:insertPos], append([]ast.Decl{serviceStruct}, file.Decls[insertPos:]...)...) file.Decls = append(decls[:insertPos], append([]ast.Decl{serviceStruct}, decls[insertPos:]...)...)
} }
func importExists(fset *token.FileSet, file *ast.File, importPath string) bool { func importExists(fset *token.FileSet, file *ast.File, importPath string) bool {
@@ -147,5 +151,7 @@ func ImplementService(mainPkgPath string, modelName string, reimplement bool) er
return err return err
} }
defer file.Close()
return nil return nil
} }