diff --git a/main.go b/main.go index d1cdd58..feea880 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "go/ast" "go/printer" "go/token" - "io/ioutil" "log" "os" "os/exec" @@ -302,7 +301,7 @@ func writeOptionsFile(types []string, packageName string, node ast.Node, fset *t if err != nil { log.Fatal(fmt.Errorf("template execute failed: %s", err)) } - if err := ioutil.WriteFile(outputFileName, buf.Bytes(), 0644); err != nil { + if err := os.WriteFile(outputFileName, buf.Bytes(), 0644); err != nil { log.Fatal(fmt.Errorf("write failed: %s", err)) } cmd := exec.Command("gofmt", "-w", outputFileName) diff --git a/template.go b/template.go index e172066..d92ca75 100644 --- a/template.go +++ b/template.go @@ -1,10 +1,17 @@ package main import ( + _ "embed" "strings" "text/template" ) +//go:embed render.gotmpl +var codeTemplateText string + +// store code generating template in constant +var codeTemplate = template.Must(template.New("code").Funcs(funcMap).Parse(codeTemplateText)) + var funcMap = template.FuncMap{ "ToPrivate": func(s string) string { return strings.ToLower(s[:1]) + s[1:] @@ -13,9 +20,3 @@ var funcMap = template.FuncMap{ return strings.ToUpper(s[:1]) + s[1:] }, } - -// store code generating template in constant -//go:generate bash -c "echo -e '// generated code, DO NOT EDIT\npackage main\n\nconst codeTemplateText = `' > template_text.go" -//go:generate bash -c "cat render.gotmpl >> template_text.go" -//go:generate bash -c "echo '`' >> template_text.go" -var codeTemplate = template.Must(template.New("code").Funcs(funcMap).Parse(codeTemplateText)) diff --git a/template_text.go b/template_text.go deleted file mode 100644 index 8c3a345..0000000 --- a/template_text.go +++ /dev/null @@ -1,139 +0,0 @@ -// generated code, DO NOT EDIT -package main - -const codeTemplateText = ` -// Code generated by github.com/launchdarkly/go-options. DO NOT EDIT. - -{{ if and .implementString .options -}} -import "fmt" -{{ end }} - -{{ if .imports -}} -import ( -{{- range .imports }} - {{ if .Alias }} {{ .Alias }} "{{ .Path }}"{{ else }} "{{ .Path }}"{{ end -}} -{{ end }} -) -{{ end }} - -{{ if and .implementEqual .options -}} -import "github.com/google/go-cmp/cmp" -{{ end }} - -{{ $applyOptionFuncType := or $.applyOptionFuncType (printf "Apply%sFunc" (ToPublic $.optionTypeName)) }} - -type {{ $applyOptionFuncType }} func(c *{{ $.configTypeName }}) {{ if $.returnError -}} error {{ end }} - -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 {{ 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...) - return c, err - {{- else -}} - {{ $applyFuncName }}(&c, options...) - return c - {{- end }} -} -{{ end }} - -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 }}) {{ if $.returnError -}} error {{ end }} -} - -{{ range .options }}{{ $option := . }} - -{{ $name := .PublicName | ToPublic | printf "%s%s" $.optionPrefix }} -{{ if $.optionSuffix }}{{ $name = $.optionSuffix | printf "%s%s" (.PublicName | ToPublic) }}{{ end }} - -{{ $implName := $name | printf "%sImpl" | ToPrivate }} - -type {{ $implName }} struct { -{{- range .Fields }} - {{ .ParamName }} {{ .Type }} -{{- end }} -} - -func (o {{ $implName }}) apply(c *{{ $.configTypeName }}) {{ if $.returnError -}} error {{ end }} { -{{- if and $option.IsStruct $option.DefaultIsNil }} - c.{{ $option.Name }} = new({{ $option.Type }}) -{{- end }} -{{- range .Fields }}{{ if $option.IsStruct }} - c.{{ $option.Name }}.{{ .Name }} = o.{{ .ParamName }} -{{- else }} - c.{{ $option.Name }} = {{ if $option.DefaultIsNil }}&{{ end }}o.{{ .ParamName }} -{{- end }}{{- end }} -{{ if $.returnError -}} - return nil -{{- end }} -} - -{{ if $.implementEqual -}} -func (o {{ $implName }}) Equal(v {{ $implName }}) bool { - switch { -{{- range .Fields }} - case !cmp.Equal(o.{{ .ParamName }}, v.{{ .ParamName }}): - return false -{{- end }} - } - return true -} -{{ end }} - -{{ if $.implementString -}} -func (o {{ $implName }}) String() string { - name := "{{ $name }}" -{{ if $option.IsStruct }} - type stripped {{ $implName }} - value := stripped(o) -{{- else -}} -{{- range .Fields }}{{/* there should only be one field since this isn't a struct */}} - // hack to avoid go vet error about passing a function to Sprintf - var value interface{} = o.{{ .ParamName }} -{{- end }} -{{- end }} - return fmt.Sprintf("%s: %+v", name, value) -} -{{ end }} - -{{ if .Docs }} -{{- range $i, $doc := .Docs }}// {{ if eq $i 0 }}{{ $name }} {{ end }}{{ $doc }}{{ end -}} -{{ end -}} -func {{ $name }}( -{{- range $i, $f := .Fields }}{{ if ne $i 0 }},{{ end }}{{ $f.ParamName }} {{ $f.ParamType }}{{ end -}} -) {{ $.optionTypeName }} { - return {{ $implName }}{ -{{- range .Fields }} - {{ .ParamName }}: {{ .ParamName }}, -{{- end }} - } -} -{{ end }} -`