compiler/protogen: use consistent options pattern
Throughout the module options usually expressed as a struct where the
acting function is a method hanging off the options type.
Use this pattern for protogen as well.
Change-Id: I533a61387cb74971e4efc9313d400b66b8aac451
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/221424
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/compiler/protogen/protogen.go b/compiler/protogen/protogen.go
index 35b7295..9299404 100644
--- a/compiler/protogen/protogen.go
+++ b/compiler/protogen/protogen.go
@@ -50,16 +50,14 @@
//
// If a failure occurs while reading or writing, Run prints an error to
// os.Stderr and calls os.Exit(1).
-//
-// Passing a nil options is equivalent to passing a zero-valued one.
-func Run(opts *Options, f func(*Plugin) error) {
+func (opts Options) Run(f func(*Plugin) error) {
if err := run(opts, f); err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), err)
os.Exit(1)
}
}
-func run(opts *Options, f func(*Plugin) error) error {
+func run(opts Options, f func(*Plugin) error) error {
if len(os.Args) > 1 {
return fmt.Errorf("unknown argument %q (this program should be run by protoc, not directly)", os.Args[1])
}
@@ -71,7 +69,7 @@
if err := proto.Unmarshal(in, req); err != nil {
return err
}
- gen, err := New(req, opts)
+ gen, err := opts.New(req)
if err != nil {
return err
}
@@ -112,11 +110,10 @@
annotateCode bool
pathType pathType
genFiles []*GeneratedFile
- opts *Options
+ opts Options
err error
}
-// Options are optional parameters to New.
type Options struct {
// If ParamFunc is non-nil, it will be called with each unknown
// generator parameter.
@@ -150,12 +147,7 @@
}
// New returns a new Plugin.
-//
-// Passing a nil Options is equivalent to passing a zero-valued one.
-func New(req *pluginpb.CodeGeneratorRequest, opts *Options) (*Plugin, error) {
- if opts == nil {
- opts = &Options{}
- }
+func (opts Options) New(req *pluginpb.CodeGeneratorRequest) (*Plugin, error) {
gen := &Plugin{
Request: req,
FilesByPath: make(map[string]*File),