Skip to content
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
23 changes: 13 additions & 10 deletions cmd_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import (
type GenerateCommand struct {
BaseCommand

interfaceName string
template string
outputFile string
sourcePkg string
noGenerate bool
vars vars
localPrefix string
interfaceName string
template string
outputFile string
sourcePkg string
noGenerate bool
vars vars
localPrefix string
ignoreUnexported bool

loader templateLoader
filepath fs
Expand Down Expand Up @@ -57,6 +58,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.BoolVar(&gc.ignoreUnexported, "u", false, "ignore unexported methods")

gc.BaseCommand = BaseCommand{
Short: "generate decorators",
Expand Down Expand Up @@ -133,8 +135,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,
IgnoreUnexported: gc.ignoreUnexported,
}

outputFileDir, err := gc.filepath.Abs(gc.filepath.Dir(gc.outputFile))
Expand Down Expand Up @@ -307,7 +310,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}} -l "{{.Options.LocalPrefix}}"{{if .Options.IgnoreUnexported}} -u{{end}}
{{end}}
import(
{{range $import := .Options.Imports}}{{$import}}
Expand Down
12 changes: 10 additions & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ 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

//IgnoreUnexported skip generation of unexported methods instead of return an error
IgnoreUnexported bool
}

type methodsList map[string]Method
Expand Down Expand Up @@ -238,10 +241,15 @@ func NewGenerator(options Options) (*Generator, error) {
return nil, errEmptyInterface
}

for _, m := range output.methods {
if srcPackageAST.Name != "" && []rune(m.Name)[0] == []rune(strings.ToLower(m.Name))[0] {
for n, m := range output.methods {
unexported := srcPackageAST.Name != "" && []rune(m.Name)[0] == []rune(strings.ToLower(m.Name))[0]
if !options.IgnoreUnexported && unexported {
return nil, errors.Wrap(errUnexportedMethod, m.Name)
}

if unexported {
delete(output.methods, n)
}
}

options.Imports = append(options.Imports, makeImports(output.imports)...)
Expand Down