-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature/full-control-over-mock-generation (#26)
* Refactors the type_model_builder.go module; adds the reflection package with extensible syntax walker and symbol visitor modules. * Extends the generate_mocks_command.go module
- Loading branch information
1 parent
ad80749
commit 321a30b
Showing
21 changed files
with
563 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,27 @@ | ||
package generator | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"github.com/matzefriedrich/parsley/internal/reflection" | ||
) | ||
|
||
type TemplateModelBuilder struct { | ||
node *ast.File | ||
accessor reflection.AstFileAccessor | ||
} | ||
|
||
func NewTemplateModelBuilder(accessor AstFileAccessor) (*TemplateModelBuilder, error) { | ||
node, err := accessor() | ||
if err != nil { | ||
return nil, err | ||
} | ||
func NewTemplateModelBuilder(accessor reflection.AstFileAccessor) *TemplateModelBuilder { | ||
return &TemplateModelBuilder{ | ||
node: node, | ||
}, nil | ||
} | ||
|
||
func (b *TemplateModelBuilder) Build() (*Model, error) { | ||
packageName := b.node.Name.Name | ||
m := NewModel(packageName) | ||
b.collectImports(m) | ||
b.collectInterfaces(m) | ||
return m, nil | ||
} | ||
|
||
func (b *TemplateModelBuilder) collectImports(m *Model) { | ||
ast.Inspect(b.node, func(n ast.Node) bool { | ||
importSpec, ok := n.(*ast.ImportSpec) | ||
if ok { | ||
m.Imports = append(m.Imports, importSpec.Path.Value) | ||
} | ||
return true | ||
}) | ||
} | ||
|
||
func (b *TemplateModelBuilder) collectInterfaces(m *Model) { | ||
ast.Inspect(b.node, func(n ast.Node) bool { | ||
typeSpec, interfaceType, ok := isInterfaceType(n) | ||
if ok { | ||
interfaceModel := InterfaceWithName(typeSpec.Name.Name) | ||
b.collectMethodsFor(interfaceType, &interfaceModel) | ||
m.Interfaces = append(m.Interfaces, interfaceModel) | ||
} | ||
return true | ||
}) | ||
} | ||
|
||
func (b *TemplateModelBuilder) collectMethodsFor(interfaceType *ast.InterfaceType, interfaceModel *Interface) { | ||
for _, method := range interfaceType.Methods.List { | ||
if funcType, ok := method.Type.(*ast.FuncType); ok { | ||
name := method.Names[0].Name | ||
parameters := b.collectParametersFor(funcType) | ||
results := b.collectResultFieldsFor(funcType) | ||
interfaceModel.Methods = append(interfaceModel.Methods, Method{ | ||
Name: name, | ||
Parameters: parameters, | ||
Results: results, | ||
}) | ||
} | ||
accessor: accessor, | ||
} | ||
} | ||
|
||
func (b *TemplateModelBuilder) collectParametersFor(funcType *ast.FuncType) []Parameter { | ||
parameters := make([]Parameter, 0) | ||
for _, param := range funcType.Params.List { | ||
paramType := param.Type | ||
paramArrayType, isArrayType := paramType.(*ast.ArrayType) | ||
if isArrayType { | ||
paramType = paramArrayType.Elt | ||
} | ||
paramTypeIdentifier, _ := paramType.(*ast.Ident) | ||
for _, paramName := range param.Names { | ||
parameters = append(parameters, Parameter{ | ||
Name: paramName.Name, | ||
TypeName: paramTypeIdentifier.Name, | ||
IsArray: isArrayType, | ||
}) | ||
} | ||
} | ||
return parameters | ||
} | ||
func (b *TemplateModelBuilder) Build() (*reflection.Model, error) { | ||
|
||
func (b *TemplateModelBuilder) collectResultFieldsFor(funcType *ast.FuncType) []Parameter { | ||
parameters := make([]Parameter, 0) | ||
if funcType.Results == nil { | ||
return parameters | ||
} | ||
for index, field := range funcType.Results.List { | ||
fieldType := field.Type | ||
fieldArrayType, isArrayType := fieldType.(*ast.ArrayType) | ||
if isArrayType { | ||
fieldType = fieldArrayType.Elt | ||
} | ||
fieldTypeIdentifier, ok := fieldType.(*ast.Ident) | ||
if ok { | ||
parameters = append(parameters, Parameter{ | ||
Name: fmt.Sprintf("result%d", index), | ||
TypeName: fieldTypeIdentifier.Name, | ||
IsArray: isArrayType, | ||
}) | ||
} | ||
fileVisitor := reflection.NewFileVisitor() | ||
walker := reflection.NewSyntaxWalker(fileVisitor) | ||
err := walker.WalkSyntaxTree(b.accessor) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return parameters | ||
} | ||
|
||
func isInterfaceType(n ast.Node) (*ast.TypeSpec, *ast.InterfaceType, bool) { | ||
typeSpec, ok := n.(*ast.TypeSpec) | ||
if ok { | ||
interfaceType, isInterface := typeSpec.Type.(*ast.InterfaceType) | ||
return typeSpec, interfaceType, isInterface | ||
} | ||
return nil, nil, false | ||
return fileVisitor.Model() | ||
} |
Oops, something went wrong.