Ported options codegen to cue
This commit is contained in:
+7
-4
@@ -9,6 +9,10 @@ See [old codegen](../pkh/codegen/README.md).
|
||||
Right now, Corteza is migrating its old YAML definitions to CUE.
|
||||
We are also simplifying all templates by moving as much data manipulation to Cue as possible.
|
||||
|
||||
### Todo
|
||||
- options documentation (see assets/templates/docs/options.gen.adoc.tpl)
|
||||
|
||||
|
||||
## Intro
|
||||
|
||||
Codegen tools are based on [cuelang](https://cuelang.org/docs/tutorials/) and golang templates.
|
||||
@@ -21,10 +25,10 @@ Codegen tools are based on [cuelang](https://cuelang.org/docs/tutorials/) and go
|
||||
|
||||
Platform, component and resource definitions (.cue files) can be found in:
|
||||
- `app`
|
||||
- `automation` @todo
|
||||
- `automation`
|
||||
- `system`
|
||||
- `compose`
|
||||
- `federation` @todo
|
||||
- `federation`
|
||||
|
||||
## Running code generator
|
||||
|
||||
@@ -58,8 +62,7 @@ Collection of `#codegen` structs with template + payload + output instructions.
|
||||
|
||||
Main entry point that combines all components
|
||||
|
||||
- @todo options
|
||||
|
||||
- options (see [options.cue](./options.cue))
|
||||
- @todo REST endpoints (unrelated to specific component)
|
||||
|
||||
#### Component
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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 }}
|
||||
@@ -0,0 +1,45 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package {{ .package }}
|
||||
|
||||
{{ template "gocode/header-gentext.tpl" }}
|
||||
|
||||
{{ if .imports }}
|
||||
import (
|
||||
{{- range .imports }}
|
||||
{{ . }}
|
||||
{{- end }}
|
||||
)
|
||||
{{- end }}
|
||||
|
||||
type (
|
||||
{{ range .groups }}
|
||||
{{ .struct }} struct {
|
||||
{{- range .options }}
|
||||
{{ .expIdent }} {{ .type }} `env:"{{ .env }}"`
|
||||
{{- end }}
|
||||
}
|
||||
{{ end }}
|
||||
)
|
||||
|
||||
{{ range .groups }}
|
||||
// {{ .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
|
||||
}
|
||||
{{ end }}
|
||||
@@ -0,0 +1,42 @@
|
||||
package codegen
|
||||
|
||||
import (
|
||||
"github.com/cortezaproject/corteza-server/app"
|
||||
"github.com/cortezaproject/corteza-server/codegen/schema"
|
||||
)
|
||||
|
||||
options:
|
||||
[...schema.#codegen] &
|
||||
[
|
||||
// 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"
|
||||
payload: {
|
||||
package: "options"
|
||||
|
||||
// make unique list of packages we'll import
|
||||
imports: [ for i in {for g in app.corteza.options for i in g.imports {"\(i)": i}} {i}]
|
||||
|
||||
groups: [
|
||||
for g in app.corteza.options {
|
||||
func: g.expIdent
|
||||
struct: g.expIdent + "Opt"
|
||||
options: g.options
|
||||
}
|
||||
|
||||
]
|
||||
|
||||
}
|
||||
},
|
||||
]
|
||||
@@ -14,4 +14,5 @@ platform: [...schema.#codegen] &
|
||||
rbacTypes+
|
||||
localeTypes+
|
||||
envoyRBAC+
|
||||
options+
|
||||
[] // placeholder
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
#_ENV: =~ "^[A-Z][A-Z0-9_]*[A-Z0-9]?$"
|
||||
//#_optName: =~ "^[a-zA-Z][a-zA-Z0-9\\s]*[a-zA-Z0-9]+$"
|
||||
|
||||
#optionsGroup: #_base & {
|
||||
imports: [...string] | *([])
|
||||
|
||||
handle: #handle
|
||||
|
||||
title?: string
|
||||
description?: string
|
||||
|
||||
env: #_ENV | *(strings.ToUpper(strings.Replace(handle, "-", "_", -1)))
|
||||
_envPrefix: env
|
||||
|
||||
options: {
|
||||
[_opt=_]: #option & {
|
||||
handle: _opt
|
||||
env: #_ENV | *(_envPrefix + "_" + strings.ToUpper(strings.Replace(handle, " ", "_", -1)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#option: {
|
||||
handle: #handle
|
||||
_words: strings.Replace(strings.Replace(strings.Replace(handle, "-", " ", -1), "_", " ", -1), ".", " ", -1)
|
||||
|
||||
// lowercased (unexported, golang) identifier
|
||||
ident: #ident | *strings.ToCamel(strings.Replace(strings.ToTitle(_words), " ", "", -1))
|
||||
|
||||
// upercased (exported, golang) identifier
|
||||
expIdent: #expIdent | *strings.Replace(strings.ToTitle(_words), " ", "", -1)
|
||||
|
||||
type: string | *"string"
|
||||
description?: string
|
||||
default?: string
|
||||
env?: #_ENV
|
||||
}
|
||||
@@ -3,8 +3,8 @@ package schema
|
||||
#platform: {
|
||||
ident: #baseHandle | *"corteza"
|
||||
|
||||
options: [...#optionsGroup]
|
||||
components: [...{platform: ident} & #component]
|
||||
|
||||
// env-var definitions
|
||||
// options: {}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
// More liberal then identifier, allows underscores and dots
|
||||
#baseHandle: =~"^[a-z][a-z0-9-]*[a-z0-9]+$"
|
||||
|
||||
|
||||
#_base: {
|
||||
// lowercase dash-separated words
|
||||
// used to build ident and exported identifiers
|
||||
|
||||
Reference in New Issue
Block a user