Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be feat ignore method #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cmd_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type GenerateCommand struct {
noGenerate bool
vars vars
localPrefix string
ignoreMethod string

loader templateLoader
filepath fs
Expand Down Expand Up @@ -56,6 +57,7 @@ func NewGenerateCommand(l remoteTemplateLoader) *GenerateCommand {
"run `gowrap template list` for details")
fs.Var(&gc.vars, "v", "a key-value pair to parametrize the template,\narguments without an equal sign are treated as a bool values,\ni.e. -v foo=bar -v disableChecks")
fs.StringVar(&gc.localPrefix, "l", "", "put imports beginning with this string after 3rd-party packages; comma-separated list")
fs.StringVar(&gc.ignoreMethod, "ignore_method", "", "ignore method")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make it boolean flag. Because I don't think people will be picking methods to ignore, most likely these will be just all unexported methods to ignore.

fs.BoolVar(&gc.ignoreUnexported, "u", false, "ignore unexported methods")


gc.BaseCommand = BaseCommand{
Short: "generate decorators",
Expand Down Expand Up @@ -122,6 +124,10 @@ func (gc *GenerateCommand) checkFlags() error {
}

func (gc *GenerateCommand) getOptions() (*generator.Options, error) {
var ignoreMethod []string
if len(gc.ignoreMethod) > 0 {
ignoreMethod = strings.Split(gc.ignoreMethod, ",")
}
options := generator.Options{
InterfaceName: gc.interfaceName,
OutputFile: gc.outputFile,
Expand All @@ -132,8 +138,9 @@ func (gc *GenerateCommand) getOptions() (*generator.Options, error) {
"OutputFileName": filepath.Base(gc.outputFile),
"VarsArgs": varsToArgs(gc.vars),
},
Vars: gc.vars.toMap(),
LocalPrefix: gc.localPrefix,
Vars: gc.vars.toMap(),
LocalPrefix: gc.localPrefix,
IgnoreMethod: ignoreMethod,
}

outputFileDir, err := gc.filepath.Abs(gc.filepath.Dir(gc.outputFile))
Expand Down Expand Up @@ -305,7 +312,7 @@ const headerTemplate = `// Code generated by gowrap. DO NOT EDIT.
package {{.Package.Name}}

{{if (not .Options.HeaderVars.DisableGoGenerate)}}
//{{"go:generate"}} gowrap gen -p {{.SourcePackage.PkgPath}} -i {{.Options.InterfaceName}} -t {{.Options.HeaderVars.Template}} -o {{.Options.HeaderVars.OutputFileName}}{{.Options.HeaderVars.VarsArgs}} -l "{{.Options.LocalPrefix}}"
//{{"go:generate"}} gowrap gen -p {{.SourcePackage.PkgPath}} -i {{.Options.InterfaceName}} -t {{.Options.HeaderVars.Template}} -o {{.Options.HeaderVars.OutputFileName}}{{.Options.HeaderVars.VarsArgs}} --ignore_method "{{ join "," .Options.IgnoreMethod }}" -l "{{.Options.LocalPrefix}}"
{{end}}

`
6 changes: 6 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ type Options struct {
//LocalPrefix is a comma-separated string of import path prefixes, which, if set, instructs Process to sort the import
//paths with the given prefixes into another group after 3rd-party packages.
LocalPrefix string

IgnoreMethod []string
}

type methodsList map[string]Method
Expand Down Expand Up @@ -221,6 +223,10 @@ func NewGenerator(options Options) (*Generator, error) {
return nil, errors.Wrap(err, "failed to parse interface declaration")
}

for _, one := range options.IgnoreMethod {
delete(output.methods, one)
}

if len(output.methods) == 0 {
return nil, errEmptyInterface
}
Expand Down