Merge pull request #16 from sunboyy/refactor-extractor

Refactor code extractor
This commit is contained in:
sunboyy 2021-02-11 22:19:18 +07:00 committed by GitHub
commit a2892425d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 69 deletions

View file

@ -6,6 +6,9 @@
<a href="https://codecov.io/gh/sunboyy/repogen"> <a href="https://codecov.io/gh/sunboyy/repogen">
<img src="https://codecov.io/gh/sunboyy/repogen/branch/main/graph/badge.svg?token=9BD5Y8X7NO"/> <img src="https://codecov.io/gh/sunboyy/repogen/branch/main/graph/badge.svg?token=9BD5Y8X7NO"/>
</a> </a>
<a href="https://codeclimate.com/github/sunboyy/repogen/maintainability">
<img src="https://api.codeclimate.com/v1/badges/d0270245c28814200c5f/maintainability" />
</a>
Repogen is a code generator for database repository in Golang inspired by Spring Data JPA. (WIP) Repogen is a code generator for database repository in Golang inspired by Spring Data JPA. (WIP)

View file

@ -6,4 +6,4 @@ coverage:
threshold: 4% threshold: 4%
patch: patch:
default: default:
target: 60% target: 75%

2
go.sum
View file

@ -95,6 +95,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY= golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
@ -129,6 +130,7 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbq
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

View file

@ -37,10 +37,21 @@ func ExtractComponents(f *ast.File) File {
typeSpec, ok := spec.(*ast.TypeSpec) typeSpec, ok := spec.(*ast.TypeSpec)
if ok { if ok {
structType, ok := typeSpec.Type.(*ast.StructType) switch t := typeSpec.Type.(type) {
if ok { case *ast.StructType:
file.Structs = append(file.Structs, extractStructType(typeSpec.Name.Name, t))
case *ast.InterfaceType:
file.Interfaces = append(file.Interfaces, extractInterfaceType(typeSpec.Name.Name, t))
}
}
}
}
return file
}
func extractStructType(name string, structType *ast.StructType) Struct {
str := Struct{ str := Struct{
Name: typeSpec.Name.Name, Name: name,
} }
for _, field := range structType.Fields.List { for _, field := range structType.Fields.List {
@ -57,13 +68,12 @@ func ExtractComponents(f *ast.File) File {
str.Fields = append(str.Fields, strField) str.Fields = append(str.Fields, strField)
} }
file.Structs = append(file.Structs, str) return str
} }
interfaceType, ok := typeSpec.Type.(*ast.InterfaceType) func extractInterfaceType(name string, interfaceType *ast.InterfaceType) InterfaceType {
if ok {
intf := InterfaceType{ intf := InterfaceType{
Name: typeSpec.Name.Name, Name: name,
} }
for _, method := range interfaceType.Methods.List { for _, method := range interfaceType.Methods.List {
@ -83,12 +93,7 @@ func ExtractComponents(f *ast.File) File {
intf.Methods = append(intf.Methods, meth) intf.Methods = append(intf.Methods, meth)
} }
file.Interfaces = append(file.Interfaces, intf) return intf
}
}
}
}
return file
} }
func extractStructTag(tagValue string) map[string][]string { func extractStructTag(tagValue string) map[string][]string {
@ -143,36 +148,28 @@ func extractFunction(name string, funcType *ast.FuncType) Method {
} }
func getType(expr ast.Expr) Type { func getType(expr ast.Expr) Type {
identExpr, ok := expr.(*ast.Ident) switch expr := expr.(type) {
if ok { case *ast.Ident:
return SimpleType(identExpr.Name) return SimpleType(expr.Name)
}
selectorExpr, ok := expr.(*ast.SelectorExpr) case *ast.SelectorExpr:
if ok { xExpr, ok := expr.X.(*ast.Ident)
xExpr, ok := selectorExpr.X.(*ast.Ident)
if !ok { if !ok {
return ExternalType{Name: selectorExpr.Sel.Name} return ExternalType{Name: expr.Sel.Name}
}
return ExternalType{PackageAlias: xExpr.Name, Name: selectorExpr.Sel.Name}
} }
return ExternalType{PackageAlias: xExpr.Name, Name: expr.Sel.Name}
starExpr, ok := expr.(*ast.StarExpr) case *ast.StarExpr:
if ok { containedType := getType(expr.X)
containedType := getType(starExpr.X)
return PointerType{ContainedType: containedType} return PointerType{ContainedType: containedType}
}
arrayType, ok := expr.(*ast.ArrayType) case *ast.ArrayType:
if ok { containedType := getType(expr.Elt)
containedType := getType(arrayType.Elt)
return ArrayType{containedType} return ArrayType{containedType}
}
intfType, ok := expr.(*ast.InterfaceType) case *ast.InterfaceType:
if ok {
var methods []Method var methods []Method
for _, method := range intfType.Methods.List { for _, method := range expr.Methods.List {
funcType, ok := method.Type.(*ast.FuncType) funcType, ok := method.Type.(*ast.FuncType)
if !ok { if !ok {
continue continue