Port all v3 codegen to cue-based codegen

This commit is contained in:
Denis Arh
2022-01-26 16:25:54 +01:00
parent a437913f85
commit 52209c476b
38 changed files with 155 additions and 1621 deletions
@@ -1,33 +0,0 @@
package {{ .Package }}
{{ template "header-gentext.tpl" }}
{{ template "header-definitions.tpl" . }}
import (
{{- range .Imports }}
{{ . }}
{{- end }}
)
{{- range .Def }}
{{- if .Locale }}
{{- if gt (len .Locale.Resource.References) 0 }}
// {{ export .Component .Resource }}ResourceTranslationReferences generates Locale references
//
// Resources with "envoy: false" are skipped
//
// This function is auto-generated
func {{ export .Component .Resource }}ResourceTranslationReferences({{- range .Locale.Resource.References }}{{ unexport .Resource }} string, {{- end }}) (res *Ref, pp []*Ref, err error) {
{{- range .Locale.Resource.References }}
{{- if eq .Field "ID" }}
res = &Ref{ResourceType: types.{{ export .Resource }}ResourceType, Identifiers: MakeIdentifiers({{ unexport .Resource }})}
{{- else }}
pp = append(pp, &Ref{ResourceType: types.{{ export .Resource }}ResourceType, Identifiers: MakeIdentifiers({{ unexport .Resource }})})
{{- end }}
{{- end }}
return
}
{{- end }}
{{- end }}
{{- end }}
@@ -1,27 +1,20 @@
package {{ .Package }}
package {{ .package }}
{{ template "header-gentext.tpl" }}
{{ template "header-definitions.tpl" . }}
{{ template "gocode/header-gentext.tpl" }}
import (
systemTypes "github.com/cortezaproject/corteza-server/system/types"
{{- range .Imports }}
{{ . }}
{{- end }}
)
{{- range .Def }}
{{ $Component := .Component }}
{{ $Resource := .Resource }}
{{ $GoType := printf "types.%s" .Resource }}
func (r *{{if not (eq $Component "system")}}{{export $Component}}{{ end }}{{$Resource}}) EncodeTranslations() ([]*ResourceTranslation, error) {
{{- range .resources }}
func (r *{{ .expIdent }}) EncodeTranslations() ([]*ResourceTranslation, error) {
out := make([]*ResourceTranslation, 0, 10)
rr := r.Res.EncodeTranslations()
rr.SetLanguage(defaultLanguage)
res, ref, pp := r.ResourceTranslationParts()
out = append(out, NewResourceTranslation(systemTypes.FromLocale(rr), res, ref, pp...))
{{ if .Locale.Extended }}
{{ if .extended }}
tmp, err := r.encodeTranslations()
return append(out, tmp...), err
{{ else }}
@@ -29,3 +22,4 @@ func (r *{{if not (eq $Component "system")}}{{export $Component}}{{ end }}{{$Res
{{- end }}
}
{{- end }}
@@ -1,18 +1,17 @@
package {{ .Package }}
package {{ .package }}
{{ template "header-gentext.tpl" }}
{{ template "header-definitions.tpl" . }}
{{ template "gocode/header-gentext.tpl" }}
import (
"fmt"
"strings"
{{- range .Imports }}
{{- range .imports }}
{{ . }}
{{- end }}
)
// Parse generates resource setting logic for each resource
// ParseResourceTranslation generates resource setting logic for each resource
//
// Resources with "envoy: false" are skipped
//
@@ -47,25 +46,22 @@ func ParseResourceTranslation(res string) (string, *Ref, []*Ref, error) {
// make the resource provide the slice of parent resources we should nest under
switch resourceType {
{{- range .Def }}
case {{ unexport .Component "types" }}.{{ export .Resource }}ResourceTranslationType:
if len(path) != {{ len .Locale.Resource.References }} {
return "", nil, nil, fmt.Errorf("expecting {{ len .Locale.Resource.References }} reference components in path, got %d", len(path))
{{- range .resources }}
case {{ .typeConst }}:
if len(path) != {{ len .references }} {
return "", nil, nil, fmt.Errorf("expecting {{ len .references }} reference components in path, got %d", len(path))
}
{{- if gt (len .Locale.Resource.References) 0 }}
ref, pp, err := {{ export .Component .Resource }}ResourceTranslationReferences(
{{- range $i, $r := .Locale.Resource.References }}
// {{ unexport $r.Resource }}
ref, pp, err := {{ .resTrRefFunc }}(
{{- range $i, $r := .references }}
path[{{ $i }}],
{{ end }}
{{- end }}
)
return {{ unexport .Component "types" }}.{{ export .Resource }}ResourceTranslationType, ref, pp, err
return {{ .typeConst }}, ref, pp, err
{{ else }}
// Component resource, no path
return {{ unexport .Component "types" }}.{{ export .Resource }}ResourceTranslationType, nil, nil, nil
return {{ .typeConst }}, nil, nil, nil
{{- end }}
{{- end}}
}
// return unhandled resource as-is
@@ -0,0 +1,24 @@
package {{ .package }}
{{ template "gocode/header-gentext.tpl" }}
import (
{{- range .imports }}
{{ . }}
{{- end }}
)
{{- range .resources }}
// {{ .resTrRefFunc }} generates Locale references
//
// This function is auto-generated
func {{ .resTrRefFunc }}({{- range .references }}{{ .param }} string, {{- end }} self string) (res *Ref, pp []*Ref, err error) {
res = &Ref{ResourceType: types.{{ .expIdent }}ResourceType, Identifiers: MakeIdentifiers(self)}
{{- range .references }}
pp = append(pp, &Ref{ResourceType: types.{{ .expIdent }}ResourceType, Identifiers: MakeIdentifiers({{ .param }})})
{{- end }}
return
}
{{- end }}
+70 -8
View File
@@ -10,7 +10,7 @@ envoyRBAC:
[...schema.#codegen] &
[
for cmp in app.corteza.components {
template: "gocode/envoy/rbac-references.go.tpl"
template: "gocode/envoy/rbac_references_$component.go.tpl"
output: "pkg/envoy/resource/rbac_references_\(cmp.ident).gen.go"
payload: {
package: "resource"
@@ -18,8 +18,6 @@ envoyRBAC:
"\"github.com/cortezaproject/corteza-server/\(cmp.ident)/types\"",
]
// cmpIdent: cmp.ident
// Operation/resource validators, grouped by resource
resources: [
for res in cmp.resources {
rbacRefFunc: "\(cmp.expIdent)\(res.expIdent)RbacReferences"
@@ -34,7 +32,7 @@ envoyRBAC:
]+
[
{
template: "gocode/envoy/rbac-rules-parse.go.tpl"
template: "gocode/envoy/rbac_rules_parse.go.tpl"
output: "pkg/envoy/resource/rbac_rules_parse.gen.go"
payload: {
package: "resource"
@@ -44,14 +42,12 @@ envoyRBAC:
},
]
// cmpIdent: cmp.ident
// Operation/resource validators, grouped by resource
resources: [
for cmp in app.corteza.components for res in cmp.resources {
importAlias: "\(cmp.ident)Types"
expIdent: res.expIdent
typeConst: "\(importAlias).\(expIdent)ResourceType"
typeConst: "\(importAlias).\(expIdent)ResourceType"
rbacRefFunc: "\(cmp.expIdent)\(res.expIdent)RbacReferences"
references: [
for p in res.parents {p},
@@ -69,4 +65,70 @@ envoyRBAC:
]
}
},
]
]+
[
{
template: "gocode/envoy/resource_translation.go.tpl"
output: "pkg/envoy/resource/resource_translation.gen.go"
payload: {
package: "resource"
resources: [
for cmp in app.corteza.components for res in cmp.resources if res.locale != _|_ {
expIdent: "\(cmp.expIdent)\(res.expIdent)"
extended: res.locale.extended
},
]
}
},
]+
[
{
template: "gocode/envoy/resource_translation_parse.go.tpl"
output: "pkg/envoy/resource/resource_translation_parse.gen.go"
payload: {
package: "resource"
imports: [
for cmp in app.corteza.components {
"\(cmp.ident)Types \"github.com/cortezaproject/corteza-server/\(cmp.ident)/types\""
},
]
resources: [
for cmp in app.corteza.components for res in cmp.resources if res.locale != _|_ {
importAlias: "\(cmp.ident)Types"
typeConst: "\(importAlias).\(res.expIdent)ResourceTranslationType"
resTrRefFunc: "\(cmp.expIdent)\(res.expIdent)ResourceTranslationReferences"
references: [
for p in res.parents {p},
{param: res.ident, refField: "ID", expIdent: res.expIdent},
]
},
]
}
},
]+
[
// wrapped with additional for loop to trim out templates with empty types list
for tpl in [
for cmp in app.corteza.components {
template: "gocode/envoy/resource_translation_references_$component.go.tpl"
output: "pkg/envoy/resource/resource_translation_references_\(cmp.ident).gen.go"
payload: {
package: "resource"
imports: [
"\"github.com/cortezaproject/corteza-server/\(cmp.ident)/types\"",
]
resources: [
for res in cmp.resources if res.locale != _|_ {
resTrRefFunc: "\(cmp.expIdent)\(res.expIdent)ResourceTranslationReferences"
expIdent: res.expIdent
references: [
for p in res.parents {p},
]
},
]
}
},
] if len(tpl.payload.resources) > 0 {tpl}]+
[]
+1 -1
View File
@@ -12,7 +12,7 @@ localeService:
// wrapped with additional for loop to trim out templates with empty types list
for tpl in [
for cmp in app.corteza.components {
template: "gocode/locale/service.go.tpl"
template: "gocode/locale/$component_service.go.tpl"
output: "\(cmp.ident)/service/locale.gen.go"
payload: {
package: "service"
+1 -1
View File
@@ -12,7 +12,7 @@ localeTypes:
// wrapped with additional for loop to trim out templates with empty types list
for tpl in [
for cmp in app.corteza.components {
template: "gocode/locale/types.go.tpl"
template: "gocode/locale/$component_types.go.tpl"
output: "\(cmp.ident)/types/locale.gen.go"
payload: {
package: "types"
+1 -1
View File
@@ -9,7 +9,7 @@ rbacAccessControl:
[...schema.#codegen] &
[
for cmp in app.corteza.components {
template: "gocode/rbac/access_control.go.tpl"
template: "gocode/rbac/$component_access_control.go.tpl"
output: "\(cmp.ident)/service/access_control.gen.go"
payload: {
package: "service"
+1 -1
View File
@@ -9,7 +9,7 @@ rbacTypes:
[...schema.#codegen] &
[
for cmp in app.corteza.components {
template: "gocode/rbac/types.go.tpl"
template: "gocode/rbac/$component_types.go.tpl"
output: "\(cmp.ident)/types/rbac.gen.go"
payload: {
package: "types"