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

added option for public new func #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <type>:\n\n", os.Args[0])
Expand All @@ -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 false to make the optional new function private`)
THE-COB marked this conversation as resolved.
Show resolved Hide resolved
flag.Usage = Usage
}

Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion render.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
2 changes: 1 addition & 1 deletion template_text.go

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

5 changes: 5 additions & 0 deletions test/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions test/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})