diff --git a/main.go b/main.go index 69e3256..8f828b3 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,7 @@ var quoteStrings bool var implementEqual bool var implementString bool var returnError bool +var newFuncPublic bool var Usage = func() { fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s :\n\n", os.Args[0]) @@ -56,6 +57,7 @@ func initFlags() { 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.BoolVar(&newFuncPublic, "public", false, `set to true to make the 'new' function public`) flag.Usage = Usage } @@ -288,6 +290,7 @@ func writeOptionsFile(types []string, packageName string, node ast.Node, fset *t "implementEqual": implementEqual, "implementString": implementString, "returnError": returnError, + "newFuncPublic": newFuncPublic, }) if err != nil { log.Fatal(fmt.Errorf("template execute failed: %s", err)) diff --git a/render.gotmpl b/render.gotmpl index 9f360e2..6260f74 100644 --- a/render.gotmpl +++ b/render.gotmpl @@ -27,7 +27,7 @@ func (f {{ $applyOptionFuncType }}) apply(c *{{ $.configTypeName }}) {{ if $.ret {{ $applyFuncName := or $.applyFuncName (printf "apply%sOptions" (ToPublic $.configTypeName)) }} {{ if $.createNewFunc}} -func new{{ $.configTypeName | ToPublic}}(options ...{{ $.optionTypeName }}) {{ if $.returnError -}} ({{ $.configTypeName }} , error) {{else}} {{ $.configTypeName }} {{ end }} { +func {{ if $.newFuncPublic -}}New{{- else -}}new{{- end -}}{{ $.configTypeName | ToPublic}}(options ...{{ $.optionTypeName }}) {{ if $.returnError -}} ({{ $.configTypeName }} , error) {{else}} {{ $.configTypeName }} {{ end }} { var c {{ $.configTypeName }} {{ if $.returnError -}} err := {{ $applyFuncName }}(&c, options...) diff --git a/template_text.go b/template_text.go index f5be069..8c3a345 100644 --- a/template_text.go +++ b/template_text.go @@ -31,7 +31,7 @@ func (f {{ $applyOptionFuncType }}) apply(c *{{ $.configTypeName }}) {{ if $.ret {{ $applyFuncName := or $.applyFuncName (printf "apply%sOptions" (ToPublic $.configTypeName)) }} {{ if $.createNewFunc}} -func new{{ $.configTypeName | ToPublic}}(options ...{{ $.optionTypeName }}) {{ if $.returnError -}} ({{ $.configTypeName }} , error) {{else}} {{ $.configTypeName }} {{ end }} { +func {{ if $.newFuncPublic -}}New{{- else -}}new{{- end -}}{{ $.configTypeName | ToPublic}}(options ...{{ $.optionTypeName }}) {{ if $.returnError -}} ({{ $.configTypeName }} , error) {{else}} {{ $.configTypeName }} {{ end }} { var c {{ $.configTypeName }} {{ if $.returnError -}} err := {{ $applyFuncName }}(&c, options...) diff --git a/test/sample.go b/test/sample.go index bacd688..f05ba1d 100644 --- a/test/sample.go +++ b/test/sample.go @@ -94,3 +94,8 @@ type configWithoutStringer struct { type configWithNoError struct { myInt int } + +//go:generate go-options -public=true -option PublicFuncOption configWithPublicNewFunc +type configWithPublicNewFunc struct { + myInt int +} diff --git a/test/sample_test.go b/test/sample_test.go index 73866d6..1a0c933 100644 --- a/test/sample_test.go +++ b/test/sample_test.go @@ -265,3 +265,11 @@ var _ = Describe("Disabling stringer", func() { Ω(fmt.Sprintf("%v", NoStringerOptionMyInt(1))).Should(Equal("{1}")) }) }) + +var _ = Describe("Public new function", func() { + It("Makes the new confic cunction public", func() { + cfg, err := NewConfigWithPublicNewFunc(PublicFuncOptionMyInt(10)) + Ω(err).ShouldNot(HaveOccurred()) + Ω(cfg.myInt).Should(Equal(10)) + }) +})