Ported options codegen to cue
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
// This file is auto-generated.
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
//
|
||||
// Definitions file that controls how this file is generated:
|
||||
{{- range .Definitions }}
|
||||
// - {{ .Source }}
|
||||
{{- end }}
|
||||
include::ROOT:partial$variables.adoc[]
|
||||
|
||||
{{ range .Definitions }}
|
||||
= {{ .Docs.Title }}
|
||||
{{ if .Docs.Intro }}
|
||||
{{ .Docs.Intro }}
|
||||
{{ end }}
|
||||
{{- range .Properties }}
|
||||
== *{{ toUpper .Env }}*
|
||||
|
||||
=== Type
|
||||
|
||||
`{{ .Type }}`
|
||||
|
||||
{{ if or .Default .Description -}}
|
||||
{{ if .Default -}}
|
||||
=== Default
|
||||
|
||||
[source]
|
||||
----
|
||||
{{ .Default }}
|
||||
----
|
||||
|
||||
{{ end -}}
|
||||
{{ if .Description -}}
|
||||
=== Description
|
||||
|
||||
{{ .Description }}
|
||||
{{ end -}}{{ end -}}
|
||||
{{ end }}{{ end }}
|
||||
@@ -1,48 +0,0 @@
|
||||
package {{ .Package }}
|
||||
|
||||
// This file is auto-generated.
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
//
|
||||
// Definitions file that controls how this file is generated:
|
||||
// {{ .Source }}
|
||||
|
||||
{{ if $.Imports -}}
|
||||
import (
|
||||
{{- range .Imports }}
|
||||
{{ normalizeImport . }}
|
||||
{{- end }}
|
||||
){{ end }}
|
||||
|
||||
type (
|
||||
{{ export $.Name }}Opt struct {
|
||||
{{- range $prop := $.Properties}}
|
||||
{{ export $prop.Name }} {{ $prop.Type }} `env:"{{ toUpper $prop.Env}}"`
|
||||
{{- end }}
|
||||
}
|
||||
)
|
||||
|
||||
// {{ export $.Name }} initializes and returns a {{ export $.Name }}Opt with default values
|
||||
func {{ export $.Name }}() (o *{{ export $.Name }}Opt) {
|
||||
o = &{{ export $.Name }}Opt{
|
||||
{{- range $prop := $.Properties }}
|
||||
{{- if $prop.Default }}
|
||||
{{ export $prop.Name }}: {{ $prop.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 *{{ export $.Name}}) Defaults() {...}
|
||||
func(o interface{}) {
|
||||
if def, ok := o.(interface{ Defaults() }); ok {
|
||||
def.Defaults()
|
||||
}
|
||||
}(o)
|
||||
|
||||
return
|
||||
}
|
||||
Generated
-18
@@ -61,10 +61,6 @@ func Proc() {
|
||||
storeSrc []string
|
||||
storeDefs []*storeDef
|
||||
|
||||
optionSrcPath = filepath.Join("pkg", "options", "*.yaml")
|
||||
optionSrc []string
|
||||
optionDefs []*optionsDef
|
||||
|
||||
aFuncsSrcPath = filepath.Join("*", "automation", "*_handler.yaml")
|
||||
aFuncsSrc []string
|
||||
aFuncsDefs []*aFuncDefs
|
||||
@@ -163,9 +159,6 @@ func Proc() {
|
||||
storeSrc = glob(storeSrcPath)
|
||||
output("loaded %d store definitions from %s\n", len(storeSrc), storeSrcPath)
|
||||
|
||||
optionSrc = glob(optionSrcPath)
|
||||
output("loaded %d option definitions from %s\n", len(optionSrc), optionSrcPath)
|
||||
|
||||
aFuncsSrc = glob(aFuncsSrcPath)
|
||||
output("loaded %d function definitions from %s\n", len(aFuncsSrc), aFuncsSrcPath)
|
||||
|
||||
@@ -184,7 +177,6 @@ func Proc() {
|
||||
fileList = append(fileList, exprTypeSrc...)
|
||||
fileList = append(fileList, restSrc...)
|
||||
fileList = append(fileList, storeSrc...)
|
||||
fileList = append(fileList, optionSrc...)
|
||||
fileList = append(fileList, aFuncsSrc...)
|
||||
|
||||
for _, d := range fileList {
|
||||
@@ -265,16 +257,6 @@ func Proc() {
|
||||
return
|
||||
}
|
||||
|
||||
if optionDefs, err = procOptions(optionSrc...); err == nil {
|
||||
if genCode {
|
||||
err = genOptions(tpls, optionDefs...)
|
||||
}
|
||||
|
||||
if genDocs && err == nil {
|
||||
err = genOptionsDocs(tpls, docPath+docGenBase, optionDefs...)
|
||||
}
|
||||
}
|
||||
|
||||
if outputErr(err, "fail to process options:\n") {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,151 +0,0 @@
|
||||
package codegen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type (
|
||||
optionsDef struct {
|
||||
Source string
|
||||
outputDir string
|
||||
|
||||
Name string
|
||||
|
||||
Docs struct {
|
||||
Title string
|
||||
Intro string
|
||||
}
|
||||
|
||||
// List of imports
|
||||
// Used only by generated file and not pre-generated-user-file
|
||||
Imports []string `yaml:"imports"`
|
||||
|
||||
Properties optionsPropSet `yaml:"props"`
|
||||
}
|
||||
|
||||
optionsPropSet []*optionsProp
|
||||
|
||||
optionsProp struct {
|
||||
Name string
|
||||
Type string
|
||||
Env string
|
||||
Default *optionsPropDefault
|
||||
|
||||
Description string
|
||||
}
|
||||
|
||||
optionsPropDefault string
|
||||
)
|
||||
|
||||
// Processes multiple options defenitions
|
||||
func procOptions(mm ...string) (dd []*optionsDef, err error) {
|
||||
var (
|
||||
f io.ReadCloser
|
||||
d *optionsDef
|
||||
)
|
||||
|
||||
dd = make([]*optionsDef, 0)
|
||||
for _, m := range mm {
|
||||
err = func() error {
|
||||
if f, err = os.Open(m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
fname := path.Base(m)
|
||||
|
||||
d = &optionsDef{
|
||||
Name: fname[:len(fname)-len(path.Ext(fname))],
|
||||
}
|
||||
|
||||
if d.Docs.Title == "" {
|
||||
d.Docs.Title = d.Name
|
||||
}
|
||||
|
||||
if err := yaml.NewDecoder(f).Decode(d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, j := range d.Properties {
|
||||
|
||||
if j.Type == "" {
|
||||
j.Type = "string"
|
||||
}
|
||||
|
||||
if j.Env == "" {
|
||||
j.Env = strings.ToUpper(d.Name + "_" + cc2underscore(j.Name))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
d.Source = m
|
||||
d.outputDir = path.Dir(m)
|
||||
|
||||
dd = append(dd, d)
|
||||
|
||||
return nil
|
||||
}()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not process %s: %w", m, err)
|
||||
}
|
||||
}
|
||||
return dd, nil
|
||||
}
|
||||
|
||||
// Custom UnmarshalYAML function for
|
||||
func (pd *optionsPropDefault) UnmarshalYAML(n *yaml.Node) error {
|
||||
|
||||
val := n.Value
|
||||
|
||||
if n.Style == yaml.DoubleQuotedStyle {
|
||||
val = "\"" + val + "\""
|
||||
}
|
||||
|
||||
*pd = optionsPropDefault(val)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Gets package name from file path
|
||||
func (o optionsDef) Package() string {
|
||||
return path.Base(path.Dir(o.Source))
|
||||
}
|
||||
|
||||
func genOptions(tpl *template.Template, dd ...*optionsDef) (err error) {
|
||||
var (
|
||||
tplOptions = tpl.Lookup("options.gen.go.tpl")
|
||||
|
||||
dst string
|
||||
)
|
||||
|
||||
for _, d := range dd {
|
||||
dst = path.Join(d.outputDir, path.Base(d.Source)[:strings.LastIndex(path.Base(d.Source), ".")]+".gen.go")
|
||||
err = goTemplate(dst, tplOptions, d)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func genOptionsDocs(tpl *template.Template, docsPath string, dd ...*optionsDef) (err error) {
|
||||
var (
|
||||
tplOptionsAdoc = tpl.Lookup("options.gen.adoc.tpl")
|
||||
|
||||
dst string
|
||||
)
|
||||
|
||||
dst = path.Join(docsPath, "env-options.gen.adoc")
|
||||
return plainTemplate(dst, tplOptionsAdoc, map[string]interface{}{
|
||||
"Definitions": dd,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user