Skip to content

Commit

Permalink
Allow the option to not return an error (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
tian000 authored Jan 24, 2022
1 parent d57ba93 commit c1c04b1
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 16 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ test:
$(MAKE) lint
go test ./...
diff test/config_options.go test/golden/config_options.go.txt
diff test/configWithNoError_options.go test/golden/configWithNoError_options.go.txt

generate:
go generate .
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var imports string
var quoteStrings bool
var implementEqual bool
var implementString bool
var returnError bool

var Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s <type>:\n\n", os.Args[0])
Expand All @@ -53,6 +54,7 @@ func initFlags() {
flag.BoolVar(&quoteStrings, "quote-default-strings", true, `set to false to disable automatic quoting of string field defaults`)
flag.BoolVar(&implementString, "stringer", true, `set to false to disable creating String() method for options`)
flag.BoolVar(&implementEqual, "cmp", true, `set to false to disable creating Equals() method for options`)
flag.BoolVar(&returnError, "noerror", true, `set to false if you do not want to return an error when creating a new config`)
flag.BoolVar(&runGoFmt, "fmt", true, `set to false to skip go format`)
flag.Usage = Usage
}
Expand Down Expand Up @@ -285,6 +287,7 @@ func writeOptionsFile(types []string, packageName string, node ast.Node, fset *t
"createNewFunc": createNewFunc,
"implementEqual": implementEqual,
"implementString": implementString,
"returnError": returnError,
})
if err != nil {
log.Fatal(fmt.Errorf("template execute failed: %s", err))
Expand Down
29 changes: 21 additions & 8 deletions render.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,56 @@ import "github.com/google/go-cmp/cmp"

{{ $applyOptionFuncType := or $.applyOptionFuncType (printf "Apply%sFunc" (ToPublic $.optionTypeName)) }}

type {{ $applyOptionFuncType }} func(c *{{ $.configTypeName }}) error
type {{ $applyOptionFuncType }} func(c *{{ $.configTypeName }}) {{ if $.returnError -}} error {{ end }}

func (f {{ $applyOptionFuncType }}) apply(c *{{ $.configTypeName }}) error {
return f(c)
func (f {{ $applyOptionFuncType }}) apply(c *{{ $.configTypeName }}) {{ if $.returnError -}} error {{ end }} {
{{ if $.returnError -}} return {{ end }} f(c)
}

{{ $applyFuncName := or $.applyFuncName (printf "apply%sOptions" (ToPublic $.configTypeName)) }}

{{ if $.createNewFunc}}
func new{{ $.configTypeName | ToPublic}}(options ...{{ $.optionTypeName }}) ({{ $.configTypeName }}, error) {
func new{{ $.configTypeName | ToPublic}}(options ...{{ $.optionTypeName }}) {{ if $.returnError -}} ({{ $.configTypeName }} , error) {{else}} {{ $.configTypeName }} {{ end }} {
var c {{ $.configTypeName }}
{{ if $.returnError -}}
err := {{ $applyFuncName }}(&c, options...)
return c, err
{{- else -}}
{{ $applyFuncName }}(&c, options...)
return c
{{- end }}
}
{{ end }}

func {{ $applyFuncName }}(c *{{ $.configTypeName }}, options ...{{ $.optionTypeName }}) error {
func {{ $applyFuncName }}(c *{{ $.configTypeName }}, options ...{{ $.optionTypeName }}) {{ if $.returnError -}} error {{ end }} {
{{- range .options -}}{{ $optionName := .Name }}{{ if .DefaultValue }}
c.{{ .Name }} = {{ .DefaultValue }}
{{- end }}{{ if .IsStruct }}{{ range .Fields }}{{ if .DefaultValue }}
c.{{ $optionName }}.{{ .Name }} = {{ .DefaultValue }}
{{- end }}{{ end }}
{{- end }}{{ end }}
{{ if $.returnError -}}
for _, o := range options {
if err := o.apply(c); err != nil {
return err
}
}
return nil
{{- else -}}
for _, o := range options {
o.apply(c)
}
{{- end }}
}

type {{ $.optionTypeName }} interface {
apply(*{{ $.configTypeName }}) error
apply(*{{ $.configTypeName }}) {{ if $.returnError -}} error {{ end }}
}

{{ range .options }}{{ $option := . }}

{{ $name := .PublicName | ToPublic | printf "%s%s" $.optionPrefix }}
{{ if $.optionSuffix }}{{ $name = $.optionSuffix | printf "%s%s" (.PublicName | ToPublic) }}{{ end }}
{{ if $.optionSuffix }}{{ $name = $.optionSuffix | printf "%s%s" (.PublicName | ToPublic) }}{{ end }}

{{ $implName := $name | printf "%sImpl" | ToPrivate }}

Expand All @@ -66,7 +77,7 @@ type {{ $implName }} struct {
{{- end }}
}

func (o {{ $implName }}) apply(c *{{ $.configTypeName }}) error {
func (o {{ $implName }}) apply(c *{{ $.configTypeName }}) {{ if $.returnError -}} error {{ end }} {
{{- if and $option.IsStruct $option.DefaultIsNil }}
c.{{ $option.Name }} = new({{ $option.Type }})
{{- end }}
Expand All @@ -75,7 +86,9 @@ func (o {{ $implName }}) apply(c *{{ $.configTypeName }}) error {
{{- else }}
c.{{ $option.Name }} = {{ if $option.DefaultIsNil }}&{{ end }}o.{{ .ParamName }}
{{- end }}{{- end }}
{{ if $.returnError -}}
return nil
{{- end }}
}

{{ if $.implementEqual -}}
Expand Down
29 changes: 21 additions & 8 deletions template_text.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions test/golden/configWithNoError_options.go.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package test

// Code generated by github.com/launchdarkly/go-options. DO NOT EDIT.

import "fmt"

import "github.com/google/go-cmp/cmp"

type ApplyNoErrorOptionFunc func(c *configWithNoError)

func (f ApplyNoErrorOptionFunc) apply(c *configWithNoError) {
f(c)
}

func newConfigWithNoError(options ...NoErrorOption) configWithNoError {
var c configWithNoError
applyConfigWithNoErrorOptions(&c, options...)
return c
}

func applyConfigWithNoErrorOptions(c *configWithNoError, options ...NoErrorOption) {
for _, o := range options {
o.apply(c)
}
}

type NoErrorOption interface {
apply(*configWithNoError)
}

type noErrorOptionMyIntImpl struct {
o int
}

func (o noErrorOptionMyIntImpl) apply(c *configWithNoError) {
c.myInt = o.o

}

func (o noErrorOptionMyIntImpl) Equal(v noErrorOptionMyIntImpl) bool {
switch {
case !cmp.Equal(o.o, v.o):
return false
}
return true
}

func (o noErrorOptionMyIntImpl) String() string {
name := "NoErrorOptionMyInt"

// hack to avoid go vet error about passing a function to Sprintf
var value interface{} = o.o
return fmt.Sprintf("%s: %+v", name, value)
}

func NoErrorOptionMyInt(o int) NoErrorOption {
return noErrorOptionMyIntImpl{
o: o,
}
}
6 changes: 6 additions & 0 deletions test/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ type configWithoutCmp struct {
type configWithoutStringer struct {
myInt int
}


//go:generate go-options -noerror=false -option NoErrorOption configWithNoError
type configWithNoError struct {
myInt int
}

0 comments on commit c1c04b1

Please sign in to comment.