-
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.
* Renames proxy generator and model builder modules * Refactors the generate_proxy_command.go module; adds new generic (code-file) generator.go module * Adds new mocks generator command * Removes functions from the type model; registers template functions instead. Adopts changes to the proxy generator template. * Adds error handling to the generate_mocks_command.go module * The generator now formats generated code in canonical go fmt style * Exports the Msg field of the ParsleyError type, and fixes error wrapping in the generic_generator.go module. Extends the mocks.gotmpl template; derives generated mocks from MockBase to trace method calls. * Adds mock verification functionality
- Loading branch information
1 parent
267070d
commit 8533007
Showing
29 changed files
with
785 additions
and
225 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"github.com/matzefriedrich/cobra-extensions/pkg" | ||
"github.com/matzefriedrich/cobra-extensions/pkg/abstractions" | ||
"github.com/matzefriedrich/parsley/internal/generator" | ||
"github.com/matzefriedrich/parsley/internal/templates" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type mockGeneratorCommand struct { | ||
use abstractions.CommandName `flag:"mocks" short:"Generate configurable mocks for interface types."` | ||
} | ||
|
||
func (m *mockGeneratorCommand) Execute() { | ||
|
||
templateLoader := func(_ string) (string, error) { | ||
return templates.MockTemplate, nil | ||
} | ||
|
||
kind := "mocks" | ||
gen, _ := generator.NewCodeFileGenerator(kind, func(config *generator.CodeFileGeneratorOptions) { | ||
config.TemplateLoader = templateLoader | ||
config.ConfigureModelCallback = func(m *generator.Model) { | ||
m.AddImport("github.com/matzefriedrich/parsley/pkg/features") | ||
} | ||
}) | ||
|
||
err := gen.GenerateCode() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
var _ pkg.TypedCommand = (*mockGeneratorCommand)(nil) | ||
|
||
func NewGenerateMocksCommand() *cobra.Command { | ||
command := &mockGeneratorCommand{} | ||
return pkg.CreateTypedCommand(command) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package generator | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
type codeFileGenerator struct { | ||
options CodeFileGeneratorOptions | ||
} | ||
|
||
type CodeFileGeneratorOptions struct { | ||
TemplateLoader TemplateLoader | ||
ConfigureModelCallback ModelConfigurationFunc | ||
kind string | ||
} | ||
|
||
type CodeFileGenerator interface { | ||
GenerateCode() error | ||
} | ||
|
||
type CodeFileGeneratorOptionsFunc func(config *CodeFileGeneratorOptions) | ||
|
||
func NewCodeFileGenerator(kind string, config ...CodeFileGeneratorOptionsFunc) (CodeFileGenerator, error) { | ||
options := CodeFileGeneratorOptions{ | ||
kind: kind, | ||
} | ||
for _, f := range config { | ||
f(&options) | ||
} | ||
if options.TemplateLoader == nil { | ||
return nil, fmt.Errorf("template loader is not set") | ||
} | ||
return &codeFileGenerator{options: options}, nil | ||
} | ||
|
||
func (g *codeFileGenerator) GenerateCode() error { | ||
|
||
goFilePath, err := GetGoFilePath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
gen := NewGenericCodeGenerator(g.options.TemplateLoader) | ||
err = RegisterTemplateFunctions(gen, RegisterTypeModelFunctions, RegisterNamingFunctions) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
builder, err := NewTemplateModelBuilder(goFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
model, err := builder.Build() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if g.options.ConfigureModelCallback != nil { | ||
g.options.ConfigureModelCallback(model) | ||
} | ||
|
||
goFileName := path.Base(goFilePath) | ||
goFileNameWithoutExtension := strings.TrimSuffix(goFileName, filepath.Ext(goFileName)) | ||
goFileDirectory := path.Dir(goFilePath) | ||
|
||
targetFilePath := path.Join(goFileDirectory, fmt.Sprintf("%s.%s.g.go", goFileNameWithoutExtension, g.options.kind)) | ||
f, _ := os.OpenFile(targetFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) | ||
defer func(f *os.File) { | ||
_ = f.Close() | ||
}(f) | ||
|
||
err = gen.Generate(g.options.kind, model, f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.