feat: deduplication
This commit is contained in:
1
.idea/vcs.xml
generated
1
.idea/vcs.xml
generated
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/cmd/crudgen" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
BIN
cmd/crudgen/crudgen
Executable file
BIN
cmd/crudgen/crudgen
Executable file
Binary file not shown.
@@ -17,14 +17,16 @@ import (
|
||||
|
||||
func ImplementServiceStruct(entityName string, file *ast.File, reimplement bool) {
|
||||
serviceName := entityName + "Service"
|
||||
isServiceStructDeclarated := false
|
||||
isServiceStructDefined := false
|
||||
var insertPos int
|
||||
var decls []ast.Decl
|
||||
|
||||
for i, decl := range file.Decls {
|
||||
genDecl, ok := decl.(*ast.GenDecl)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
decls = append(decls, decl)
|
||||
if genDecl.Tok == token.IMPORT {
|
||||
insertPos = i + 1
|
||||
continue
|
||||
@@ -40,14 +42,16 @@ func ImplementServiceStruct(entityName string, file *ast.File, reimplement bool)
|
||||
}
|
||||
if _, ok := typeSpec.Type.(*ast.StructType); ok {
|
||||
if typeSpec.Name != nil && typeSpec.Name.Name == serviceName {
|
||||
isServiceStructDeclarated = true
|
||||
isServiceStructDefined = true
|
||||
if reimplement {
|
||||
decls = decls[:1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if isServiceStructDeclarated && !reimplement {
|
||||
if isServiceStructDefined && !reimplement {
|
||||
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 {
|
||||
@@ -147,5 +151,7 @@ func ImplementService(mainPkgPath string, modelName string, reimplement bool) er
|
||||
return err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user