Fix options defaults (Defaults() fn must be called before fill())

This commit is contained in:
Denis Arh
2022-02-16 20:34:37 +01:00
parent 23c7f357fb
commit 7fd719364e
7 changed files with 294 additions and 209 deletions
@@ -1,45 +0,0 @@
package {{ .package }}
{{ template "gocode/header-gentext.tpl" }}
{{ if .imports }}
import (
{{- range .imports }}
{{ . }}
{{- end }}
)
{{- end }}
type (
{{ .struct }} struct {
{{- range .options }}
{{ .expIdent }} {{ .type }} `env:"{{ .env }}"`
{{- end }}
}
)
// {{ .func }} initializes and returns a {{ .struct }} with default values
//
// This function is auto-generated
func {{ .func }}() (o *{{ .struct }}) {
o = &{{ .struct }}{
{{- range .options }}
{{- if .default }}
{{ .expIdent }}: {{ .default }},
{{- end }}
{{- end }}
}
fill(o)
// Function that allows access to custom logic inside the parent function.
// The custom logic in the other file should be like:
// func (o *{{ .struct }}) Defaults() {...}
func(o interface{}) {
if def, ok := o.(interface{ Defaults() }); ok {
def.Defaults()
}
}(o)
return
}
@@ -33,17 +33,22 @@ type (
{{- end }}
}
fill(o)
// Function that allows access to custom logic inside the parent function.
// The custom logic in the other file should be like:
// func (o *{{ .struct }}) Defaults() {...}
// Custom defaults
func(o interface{}) {
if def, ok := o.(interface{ Defaults() }); ok {
def.Defaults()
}
}(o)
fill(o)
// Custom cleanup
func(o interface{}) {
if def, ok := o.(interface{ Cleanup() }); ok {
def.Cleanup()
}
}(o)
return
}
{{ end }}
-14
View File
@@ -9,20 +9,6 @@ import (
options:
[...schema.#codegen] &
[
// placeholder
// for g in app.corteza.options {
// template: "gocode/options/$options_group.go.tpl"
// output: "pkg/options/\(g.ident).gen.go"
// payload: {
// package: "options"
// imports: g.imports
// func: g.expIdent
// struct: g.expIdent + "Opt"
// options: g.options
// }
// },
{
template: "gocode/options/options.go.tpl"
output: "pkg/options/options.gen.go"