3
0

Port all v3 codegen to cue-based codegen

This commit is contained in:
Denis Arh
2021-12-30 14:59:16 +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"
-12
View File
@@ -1,12 +0,0 @@
This is 3rd generation of corteza code generator
(1st was written in PHP, apologies 2nd is active and in use)
Goal for the 3rd generation is:
- improved definition structure that addresses all elements that are used for code generation (types, store, rbac)
- unify most if not all definitions (stuff in YAML files) into what you can find in /def/...
- JSON schema validation for YAML files
How to run it (temp):
```
go run pkg/codegen-v3/*.go def
```
@@ -1,35 +0,0 @@
package {{ .Package }}
{{ template "header-gentext.tpl" }}
{{ template "header-definitions.tpl" . }}
import (
{{- range .Imports }}
{{ . }}
{{- end }}
)
{{- range .Def }}
{{- if gt (len .RBAC.Resource.References) 0 }}
// {{ export .Component .Resource }}RbacReferences generates RBAC references
//
// Resources with "envoy: false" are skipped
//
// This function is auto-generated
func {{ export .Component .Resource }}RbacReferences({{- range .RBAC.Resource.References }}{{ unexport .Resource }} string, {{- end }}) (res *Ref, pp []*Ref, err error) {
{{- range .RBAC.Resource.References }}
{{- if eq .Field "ID" }}
if {{ unexport .Resource }} != "*" {
res = &Ref{ResourceType: types.{{ export .Resource }}ResourceType, Identifiers: MakeIdentifiers({{ unexport .Resource }})}
}
{{- else }}
if {{ unexport .Resource }} != "*" {
pp = append(pp, &Ref{ResourceType: types.{{ export .Resource }}ResourceType, Identifiers: MakeIdentifiers({{ unexport .Resource }})})
}
{{- end }}
{{- end }}
return
}
{{- end }}
{{- end }}
@@ -1,71 +0,0 @@
package {{ .Package }}
{{ template "header-gentext.tpl" }}
{{ template "header-definitions.tpl" . }}
import (
"fmt"
"strings"
{{- range .Imports }}
{{ . }}
{{- end }}
)
// Parse generates resource setting logic for each resource
//
// Resources with "envoy: false" are skipped
//
// This function is auto-generated
func ParseRule(res string) (string, *Ref, []*Ref, error) {
if res == "" {
return "", nil, nil, fmt.Errorf("empty resource")
}
sp := "/"
res = strings.TrimSpace(res)
res = strings.TrimRight(res, sp)
rr := strings.Split(res, sp)
// only service defined (corteza::system, corteza::compose, ...)
if len(rr) == 1 {
return res, nil, nil, nil
}
// full thing
resourceType, path := rr[0], rr[1:]
for p := 1; p < len(path); p++ {
if path[p] != "*" && path[p-1] == "*" {
return "", nil, nil, fmt.Errorf("invalid path wildcard combination for '%s'", res)
}
}
// make the resource provide the slice of parent resources we should nest under
switch resourceType {
{{- range .Def }}
case {{ unexport .Component "types" }}.{{ export .Resource }}ResourceType:
if len(path) != {{ len .RBAC.Resource.References }} {
return "", nil, nil, fmt.Errorf("expecting {{ len .RBAC.Resource.References }} reference components in path, got %d", len(path))
}
{{- if gt (len .RBAC.Resource.References) 0 }}
ref, pp, err := {{ export .Component .Resource }}RbacReferences(
{{- range $i, $r := .RBAC.Resource.References }}
// {{ unexport $r.Resource }}
path[{{ $i }}],
{{ end }}
)
return {{ unexport .Component "types" }}.{{ export .Resource }}ResourceType, ref, pp, err
{{ else }}
// Component resource, no path
return {{ unexport .Component "types" }}.{{ export .Resource }}ResourceType, nil, nil, nil
{{- end }}
{{- end}}
}
// return unhandled resource as-is
return resourceType, nil, nil, nil
}
@@ -1,31 +0,0 @@
package {{ .Package }}
{{ template "header-gentext.tpl" }}
{{ template "header-definitions.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) {
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 }}
tmp, err := r.encodeTranslations()
return append(out, tmp...), err
{{ else }}
return out, nil
{{- end }}
}
{{- end }}
@@ -1,73 +0,0 @@
package {{ .Package }}
{{ template "header-gentext.tpl" }}
{{ template "header-definitions.tpl" . }}
import (
"fmt"
"strings"
{{- range .Imports }}
{{ . }}
{{- end }}
)
// Parse generates resource setting logic for each resource
//
// Resources with "envoy: false" are skipped
//
// This function is auto-generated
func ParseResourceTranslation(res string) (string, *Ref, []*Ref, error) {
if res == "" {
return "", nil, nil, fmt.Errorf("empty resource")
}
sp := "/"
if strings.Index(res, "corteza::") == 0 {
res = res[9:]
}
res = strings.TrimSpace(res)
res = strings.TrimRight(res, sp)
rr := strings.Split(res, sp)
// only service defined (corteza::system, corteza::compose, ...)
if len(rr) == 1 {
return "", nil, nil, fmt.Errorf("only service defined: %s", res)
}
// full thing
resourceType, path := rr[0], rr[1:]
for p := 1; p < len(path); p++ {
if path[p] == "*" {
return "", nil, nil, fmt.Errorf("path wildcard not allowed for locale resources: '%s'", res)
}
}
// 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))
}
{{- if gt (len .Locale.Resource.References) 0 }}
ref, pp, err := {{ export .Component .Resource }}ResourceTranslationReferences(
{{- range $i, $r := .Locale.Resource.References }}
// {{ unexport $r.Resource }}
path[{{ $i }}],
{{ end }}
)
return {{ unexport .Component "types" }}.{{ export .Resource }}ResourceTranslationType, ref, pp, err
{{ else }}
// Component resource, no path
return {{ unexport .Component "types" }}.{{ export .Resource }}ResourceTranslationType, nil, nil, nil
{{- end }}
{{- end}}
}
// return unhandled resource as-is
return resourceType, nil, nil, nil
}
@@ -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,4 +0,0 @@
// Definitions file that controls how this file is generated:
{{- range .Def }}
// - {{ .Source }}
{{- end }}
@@ -1,5 +0,0 @@
// This file is auto-generated.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
@@ -1,216 +0,0 @@
package {{ .Package }}
{{ template "header-gentext.tpl" }}
{{ template "header-definitions.tpl" . }}
import (
"context"
{{- range .Imports }}
{{ . }}
{{- end }}
"github.com/cortezaproject/corteza-server/pkg/actionlog"
intAuth "github.com/cortezaproject/corteza-server/pkg/auth"
"github.com/cortezaproject/corteza-server/pkg/errors"
"github.com/cortezaproject/corteza-server/pkg/locale"
"github.com/cortezaproject/corteza-server/pkg/options"
"github.com/cortezaproject/corteza-server/store"
systemTypes "github.com/cortezaproject/corteza-server/system/types"
"golang.org/x/text/language"
)
type (
localeAccessControl interface {
CanManageResourceTranslations(ctx context.Context) bool
}
resourceTranslationsManager struct {
actionlog actionlog.Recorder
locale locale.Resource
store store.Storer
ac localeAccessController
}
localeAccessController interface {
CanManageResourceTranslations(context.Context) bool
}
ResourceTranslationsManagerService interface {
{{- range .Def }}
{{ .Resource }}(ctx context.Context, {{ if .Locale.Resource }}{{ range .Locale.Resource.References }}{{ .Field }} uint64, {{ end }}{{ end }}) (locale.ResourceTranslationSet, error)
{{- end }}
Upsert(context.Context, locale.ResourceTranslationSet) error
Locale() locale.Resource
}
)
var ErrNotAllowedToManageResourceTranslations = errors.Unauthorized("not allowed to manage resource translations")
func ResourceTranslationsManager(ls locale.Resource) *resourceTranslationsManager {
return &resourceTranslationsManager{
actionlog: DefaultActionlog,
store: DefaultStore,
ac: DefaultAccessControl,
locale: ls,
}
}
func (svc resourceTranslationsManager) Upsert(ctx context.Context, rr locale.ResourceTranslationSet) (err error) {
// User is allowed to manage resource translations when:
// - managed resource translation strings are all for default language
// or
// - user is allowed to manage resource translations
if rr.ContainsForeign(svc.Locale().Default().Tag) {
if !svc.ac.CanManageResourceTranslations(ctx) {
return ErrNotAllowedToManageResourceTranslations
}
}
for _, r := range rr {
r.Msg = locale.SanitizeMessage(r.Msg)
}
// @todo validation
me := intAuth.GetIdentityFromContext(ctx)
// - group by resource
localeByRes := make(map[string]locale.ResourceTranslationSet)
for _, r := range rr {
localeByRes[r.Resource] = append(localeByRes[r.Resource], r)
}
// - for each resource, fetch the current state
sysLocale := make(systemTypes.ResourceTranslationSet, 0, len(rr))
for res, rr := range localeByRes {
current, _, err := store.SearchResourceTranslations(ctx, svc.store, systemTypes.ResourceTranslationFilter{
Resource: res,
})
if err != nil {
return err
}
// get deltas and prepare upsert accordingly
aux := current.New(rr)
aux.Walk(func(cc *systemTypes.ResourceTranslation) error {
cc.ID = nextID()
cc.CreatedAt = *now()
cc.CreatedBy = me.Identity()
return nil
})
sysLocale = append(sysLocale, aux...)
aux = current.Old(rr)
aux.Walk(func(cc *systemTypes.ResourceTranslation) error {
cc.UpdatedAt = now()
cc.UpdatedBy = me.Identity()
return nil
})
sysLocale = append(sysLocale, aux...)
}
err = store.UpsertResourceTranslation(ctx, svc.store, sysLocale...)
if err != nil {
return err
}
// Reload ALL resource translations
// @todo we could probably do this more selectively and refresh only updated resources?
_ = locale.Global().ReloadResourceTranslations(ctx)
return nil
}
func (svc resourceTranslationsManager) Locale() locale.Resource {
return svc.locale
}
{{- range .Def }}
{{ $Resource := .Resource }}
{{ $GoType := printf "types.%s" .Resource }}
func (svc resourceTranslationsManager) {{ .Resource }}(ctx context.Context, {{ if .Locale.Resource }}{{ range .Locale.Resource.References }}{{ .Field }} uint64, {{ end }}{{ end }}) (locale.ResourceTranslationSet, error) {
var (
err error
out locale.ResourceTranslationSet
res *{{ $GoType }}
k types.LocaleKey
)
res, err = svc.load{{$Resource}}(ctx, svc.store, {{ if .Locale.Resource }}{{ range .Locale.Resource.References }}{{ .Field }}, {{ end }}{{ end }})
if err != nil {
return nil, err
}
for _, tag := range svc.locale.Tags() {
{{- range .Locale.Keys}}
{{- if not .Custom }}
k = types.LocaleKey{{ $Resource }}{{coalesce (export .Name) (export .Path) }}
out = append(out, &locale.ResourceTranslation{
Resource: res.ResourceTranslation(),
Lang: tag.String(),
Key: k.Path,
Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), k.Path),
})
{{ end }}
{{- end}}
{{- range .Locale.Keys}}
{{- if and .Custom .CustomHandler }}
auxResSet, err = svc.{{unexport $Resource}}{{export .CustomHandler}}Handler(ctx, tag, res, k.Path)
{{- end}}
{{- end}}
}
{{- if .Locale.Extended }}
tmp, err := svc.{{unexport $Resource}}Extended(ctx, res)
return append(out, tmp...), err
{{- else }}
return out, nil
{{- end }}
}
{{- end }}
func updateTranslations(ctx context.Context, ac localeAccessControl, lsvc ResourceTranslationsManagerService, tt ...*locale.ResourceTranslation) error {
if lsvc == nil || lsvc.Locale() == nil || lsvc.Locale().Default() == nil {
// gracefully handle partial initializations
return nil
}
var (
// assuming options will not change after start
contentLang = lsvc.Locale().Default().Tag
)
if options.Locale().ResourceTranslationsEnabled {
contentLang = locale.GetContentLanguageFromContext(ctx)
// Resource translations enabled
if contentLang == language.Und {
// If no content-language meta (HTTP header) info was
// used, do not run update translations - we do not know
// what is the language that we're sending in
return nil
}
if !lsvc.Locale().SupportedLang(contentLang) {
// unsupported language
return errors.InvalidData("unsupported language")
}
if !ac.CanManageResourceTranslations(ctx) {
return errors.Unauthorized("not allowed to manage resource translations")
}
}
locale.ResourceTranslationSet(tt).SetLanguage(contentLang)
if err := lsvc.Upsert(ctx, tt); err != nil {
return err
}
return nil
}
-91
View File
@@ -1,91 +0,0 @@
package def
import (
"fmt"
"strings"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/tpl"
"github.com/cortezaproject/corteza-server/pkg/y7s"
"gopkg.in/yaml.v3"
)
type (
Document struct {
Skip bool `yaml:"(skip)"`
Imports []string
Component string
IsComponentResource bool `yaml:"-"`
Resource string
Source string
RBAC *rbac
Locale *locale
Envoy bool `yaml:"envoy"`
}
)
func (set *rbacOperations) UnmarshalYAML(n *yaml.Node) error {
return y7s.Each(n, func(k *yaml.Node, v *yaml.Node) (err error) {
def := rbacOperation{}
if k != nil {
def.Operation = k.Value
}
*set = append(*set, &def)
return v.Decode(&def)
})
}
// Preproc preprocesses the document and sets defaults
func (doc *Document) Proc(filename string) error {
doc.Source = filename
// filename parts
fp := strings.Split(filename, ".")
// trim extension
fp = fp[:len(fp)-1]
if len(fp) > 0 && doc.Component == "" {
// set component from the 1st part
// component is system, compose, ...
doc.Component = fp[0]
}
if len(fp) > 1 && doc.Resource == "" {
// if there are more parts, set resource
// resource is user, module, record, workflow, ...
doc.Resource = fp[1]
}
if strings.ToLower(doc.Resource) == "component" {
return fmt.Errorf("can not use 'component' as a resource name, this is done automatically")
} else if doc.Resource == "" {
doc.Resource = "component"
doc.IsComponentResource = true
}
doc.Imports = normalizeImport(doc.Imports...)
if err := doc.RBAC.proc(doc.Component, doc.Resource); err != nil {
return err
}
if err := doc.Locale.proc(doc.Component, doc.Resource); err != nil {
return err
}
doc.Resource = tpl.Export(doc.Resource)
return nil
}
func normalizeImport(ii ...string) []string {
for i := range ii {
if strings.Contains(ii[i], " ") {
p := strings.SplitN(ii[i], " ", 2)
ii[i] = fmt.Sprintf(`%s "%s"`, p[0], strings.Trim(p[1], `"`))
} else {
ii[i] = fmt.Sprintf(`"%s"`, strings.Trim(ii[i], `"'`+"`"))
}
}
return ii
}
-127
View File
@@ -1,127 +0,0 @@
package def
import (
"fmt"
"strings"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/tpl"
"github.com/cortezaproject/corteza-server/pkg/y7s"
"gopkg.in/yaml.v3"
)
type (
locale struct {
ResourceType string `yaml:"resourceType"`
Resource *resourceTranslation
Extended bool
SkipSvc bool `yaml:"skipSvc"`
Keys localeKeys
}
resourceTranslation struct {
References []*resourceTranslationRef
}
resourceTranslationRef struct {
Field string
ResourceType string
Resource string
Component string
custom bool
}
localeKeys []*localeKey
localeKey struct {
Name string
Path string
Custom bool
CustomHandler string `yaml:"customHandler"`
Description string
Field string
}
)
func (r *locale) proc(component, resource string) error {
if r == nil {
return nil
}
if r.ResourceType == "" {
if strings.ToLower(resource) == "component" {
r.ResourceType = component
} else {
r.ResourceType = fmt.Sprintf("%s:%s", component, resource)
}
}
if r.Resource == nil {
r.Resource = &resourceTranslation{References: []*resourceTranslationRef{{Field: "ID"}}}
}
// check types of each referenced component
// and self-references (field==ID) with own resource type
for _, rc := range r.Resource.References {
if !rc.custom {
if rc.Field == "ID" {
rc.ResourceType = r.ResourceType
rc.Component = component
rc.Resource = resource
} else {
rc.ResourceType = fmt.Sprintf("%s:%s", component, rc.Field)
rc.Component = component
rc.Resource = rc.Field
rc.Field = rc.Field + "ID"
}
}
}
// assure missing key field paths
for _, k := range r.Keys {
k.Custom = k.Custom || k.CustomHandler != ""
// Guess the key path
if k.Field == "" {
k.Field = tpl.Export(k.Path)
}
// Guess the name
if k.Name == "" {
k.Name = k.Path
}
}
return nil
}
func (op *localeKey) UnmarshalYAML(n *yaml.Node) error {
type auxType localeKey
switch {
case y7s.IsKind(n, yaml.ScalarNode):
return y7s.DecodeScalar(n, "locale key", &op.Path)
case y7s.IsKind(n, yaml.MappingNode):
var aux = (*auxType)(op)
return n.Decode(aux)
}
return y7s.NodeErr(n, "unsupported formatting")
}
func (op *resourceTranslationRef) UnmarshalYAML(n *yaml.Node) error {
if y7s.IsKind(n, yaml.ScalarNode) {
op.Field = n.Value
if n.Value != "ID" {
op.ResourceType = n.Value
}
return nil
}
type auxType resourceTranslationRef
var aux = (*auxType)(op)
aux.custom = true
return n.Decode(aux)
}
-165
View File
@@ -1,165 +0,0 @@
package def
import (
"fmt"
"strings"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/tpl"
"github.com/cortezaproject/corteza-server/pkg/y7s"
"gopkg.in/yaml.v3"
)
type (
rbac struct {
// fully qualified resource name
ResourceType string `yaml:"resourceType"`
Resource *rbacResource
Operations rbacOperations
}
rbacResource struct {
References []*rbacResourceRef
Attributes *rbacAttributes
}
rbacResourceRef struct {
Field string
ResourceType string
Resource string
Component string
custom bool
}
rbacOperations []*rbacOperation
rbacOperation struct {
Operation string
CanFnName string `yaml:"canFnName"`
Description string
}
rbacAttributes struct {
Fields []string `yaml:"-"`
}
)
func (r *rbac) proc(component, resource string) error {
const (
defaultNS = "corteza"
nsDelimiter = "::"
)
if r.ResourceType == "" {
if strings.ToLower(resource) == "component" {
r.ResourceType = component
} else {
r.ResourceType = fmt.Sprintf("%s:%s", component, resource)
}
r.ResourceType = defaultNS + nsDelimiter + r.ResourceType
}
if !strings.Contains(r.ResourceType, nsDelimiter) {
return fmt.Errorf("no namespace prefix found (e.g.: 'corteza::') in resource type")
}
for _, op := range r.Operations {
// Generate all check name
if op.CanFnName == "" {
op.CanFnName = RbacOperationCanFnName(resource, op.Operation)
}
}
if r.Resource == nil {
r.Resource = &rbacResource{References: []*rbacResourceRef{{Field: "ID"}}}
}
// check types of each referenced component
// and prefix non-custom components with corteza::<component>
// and self-references (field==ID) with own resource type
for _, rc := range r.Resource.References {
if !rc.custom {
if rc.Field == "ID" {
rc.ResourceType = r.ResourceType
rc.Component = component
rc.Resource = resource
} else {
rc.ResourceType = defaultNS + nsDelimiter + fmt.Sprintf("%s:%s", component, rc.Field)
rc.Component = component
rc.Resource = rc.Field
rc.Field = rc.Field + "ID"
}
}
}
return nil
}
func (op *rbacOperation) UnmarshalYAML(n *yaml.Node) error {
if y7s.IsKind(n, yaml.ScalarNode) {
// @todo handle disabled operations
// the idea is that when service operations are defined we implicitly define
// RBAC operations. Here, we'll be able to remove implicitly defined operation
return nil
}
type auxType rbacOperation
var aux = (*auxType)(op)
return n.Decode(aux)
}
func (op *rbacResourceRef) UnmarshalYAML(n *yaml.Node) error {
if y7s.IsKind(n, yaml.ScalarNode) {
op.Field = n.Value
if n.Value != "ID" {
op.ResourceType = n.Value
// @todo expand resource & component
}
return nil
}
type auxType rbacResourceRef
var aux = (*auxType)(op)
aux.custom = true
return n.Decode(aux)
}
func (a *rbacAttributes) UnmarshalYAML(n *yaml.Node) error {
if y7s.IsKind(n, yaml.ScalarNode) {
return nil
}
// if not scalar, assume we will get list of fields
a.Fields = make([]string, 0)
return n.Decode(&a.Fields)
}
func RbacOperationCanFnName(res, op string) string {
// when check function name is not explicitly defined we try
// to use resource and operation name and generate easy-to-read name
//
// <res> + <op> => Can<Op><Res>
// <res> + <op:foo.bar.verb> => Can<Verb><Foo><Bar>On<Res>
if strings.ToLower(res) == "component" {
res = ""
}
if strings.Contains(op, ".") {
parts := strings.Split(op, ".")
l := len(parts)
parts = append(parts[l-1:], parts[:l-1]...)
if res != "" {
// Only append "on" if there is resource
parts = append(parts, "on")
}
op = tpl.Export(parts...)
}
return tpl.Export("can", op, res)
}
-168
View File
@@ -1,168 +0,0 @@
package gen
import (
"fmt"
"text/template"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/def"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/tpl"
"github.com/cortezaproject/corteza-server/pkg/slice"
)
func Envoy(t *template.Template, dd []*def.Document) error {
return List{
"resource rbac parse": envoyResourceRbacUnmarshal,
"resource rbac references": envoyResourceRbacReferences,
"resource translation": envoyResourceTranslation,
"resource translation parse": envoyResourceTranslationUnmarshal,
"resource translation references": envoyResourceTranslationReferences,
}.Generate(t, dd)
}
// EnvoyResourceRbacUnmarshal envoy rbac unmarshal
// <service>/service/rbac.gen.go
//
// Contains all RBAC related definitions
func envoyResourceRbacUnmarshal(t *template.Template, dd []*def.Document) (err error) {
const (
templateName = "envoy/resource-rbac_rules_parse.go.tpl"
outputPathTpl = "pkg/envoy/resource/rbac_rules_parse.gen.go"
)
dd = filter(dd, func(d *def.Document) bool { return d.Envoy })
// build list of component type imports
ctImports := make([]string, 0)
for _, d := range dd {
imp := d.Component + "Types " + cImport(d.Component, "types")
if !slice.HasString(ctImports, imp) {
ctImports = append(ctImports, imp)
}
}
w := tpl.Wrap{
Package: "resource",
Def: dd,
Imports: append(collectImports(dd...), ctImports...),
}
err = tpl.GoTemplate(outputPathTpl, t.Lookup(templateName), w)
if err != nil {
return
}
return
}
// EnvoyResourceRbacReferences generates one rbac definition file per component
// <service>/service/rbac.gen.go
//
// Contains all RBAC related definitions
func envoyResourceRbacReferences(t *template.Template, dd []*def.Document) (err error) {
const (
templateName = "envoy/resource-rbac_references.go.tpl"
outputPathTpl = "pkg/envoy/resource/rbac_references_%s.gen.go"
)
dd = filter(dd, func(d *def.Document) bool { return d.Envoy })
for component, perComponent := range partByComponent(dd) {
w := tpl.Wrap{
Package: "resource",
Component: component,
Def: perComponent,
Imports: append(collectImports(perComponent...), cImport(component, "types")),
}
err = tpl.GoTemplate(fmt.Sprintf(outputPathTpl, component), t.Lookup(templateName), w)
if err != nil {
return
}
}
return
}
func envoyResourceTranslation(t *template.Template, dd []*def.Document) (err error) {
const (
templateName = "envoy/resource-resource_translation.go.tpl"
outputPathTpl = "pkg/envoy/resource/resource_translation.gen.go"
)
dd = filter(dd, func(d *def.Document) bool { return d.Envoy && d.Locale != nil })
// build list of component type imports
ctImports := make([]string, 0)
w := tpl.Wrap{
Package: "resource",
Def: dd,
Imports: append(collectImports(dd...), ctImports...),
}
err = tpl.GoTemplate(outputPathTpl, t.Lookup(templateName), w)
if err != nil {
return
}
return
}
func envoyResourceTranslationUnmarshal(t *template.Template, dd []*def.Document) (err error) {
const (
templateName = "envoy/resource-resource_translation_parse.go.tpl"
outputPathTpl = "pkg/envoy/resource/resource_translation_parse.gen.go"
)
dd = filter(dd, func(d *def.Document) bool { return d.Envoy && d.Locale != nil })
// build list of component type imports
ctImports := make([]string, 0)
for _, d := range dd {
imp := d.Component + "Types " + cImport(d.Component, "types")
if !slice.HasString(ctImports, imp) {
ctImports = append(ctImports, imp)
}
}
w := tpl.Wrap{
Package: "resource",
Def: dd,
Imports: append(collectImports(dd...), ctImports...),
}
err = tpl.GoTemplate(outputPathTpl, t.Lookup(templateName), w)
if err != nil {
return
}
return
}
func envoyResourceTranslationReferences(t *template.Template, dd []*def.Document) (err error) {
const (
templateName = "envoy/resource-resource_translation_references.go.tpl"
outputPathTpl = "pkg/envoy/resource/resource_translation_references_%s.gen.go"
)
dd = filter(dd, func(d *def.Document) bool { return d.Envoy && d.Locale != nil })
for component, perComponent := range partByComponent(dd) {
w := tpl.Wrap{
Package: "resource",
Component: component,
Def: perComponent,
Imports: append(collectImports(perComponent...), cImport(component, "types")),
}
err = tpl.GoTemplate(fmt.Sprintf(outputPathTpl, component), t.Lookup(templateName), w)
if err != nil {
return
}
}
return
}
-67
View File
@@ -1,67 +0,0 @@
package gen
import (
"fmt"
"text/template"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/def"
)
type (
List map[string]func(*template.Template, []*def.Document) error
)
func (gg List) Generate(tpls *template.Template, dd []*def.Document) (err error) {
for l, g := range gg {
if err = g(tpls, dd); err != nil {
return fmt.Errorf("codegen for %s failed: %w", l, err)
}
}
return
}
func filter(dd []*def.Document, check func(*def.Document) bool) []*def.Document {
aux := make([]*def.Document, 0, len(dd))
for _, d := range dd {
if !check(d) {
continue
}
aux = append(aux, d)
}
return aux
}
func partByComponent(dd []*def.Document) map[string][]*def.Document {
var (
parted = make(map[string][]*def.Document)
)
for _, d := range dd {
parted[d.Component] = append(parted[d.Component], d)
}
return parted
}
func collectImports(dd ...*def.Document) []string {
mm := make(map[string]bool)
for _, d := range dd {
for _, i := range d.Imports {
mm[i] = true
}
}
ii := make([]string, 0, len(mm))
for i := range mm {
ii = append(ii, i)
}
return ii
}
// component import
func cImport(c, s string) string {
return fmt.Sprintf(`"github.com/cortezaproject/corteza-server/%s/%s"`, c, s)
}
-70
View File
@@ -1,70 +0,0 @@
package gen
import (
"fmt"
"text/template"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/def"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/tpl"
)
func Locale(t *template.Template, dd []*def.Document) error {
return List{
"types": localeTypes,
"services": localeServices,
}.Generate(t, dd)
}
func localeTypes(t *template.Template, dd []*def.Document) (err error) {
const (
templateName = "locale/types.go.tpl"
outputPathTpl = "%s/types/locale.gen.go"
)
dd = filter(dd, func(d *def.Document) bool { return d.Locale != nil })
for component, perComponent := range partByComponent(dd) {
w := tpl.Wrap{
Package: "types",
Component: component,
Def: perComponent,
}
err = tpl.GoTemplate(fmt.Sprintf(outputPathTpl, component), t.Lookup(templateName), w)
if err != nil {
return
}
}
return
}
func localeServices(t *template.Template, dd []*def.Document) (err error) {
const (
templateName = "locale/service.go.tpl"
outputPathTpl = "%s/service/locale.gen.go"
)
dd = filter(dd, func(d *def.Document) bool { return d.Locale != nil && !d.Locale.SkipSvc })
for component, perComponent := range partByComponent(dd) {
w := tpl.Wrap{
Package: "service",
Component: component,
Def: perComponent,
Imports: append(collectImports(perComponent...), cImport(component, "types")),
}
err = tpl.GoTemplate(fmt.Sprintf(outputPathTpl, component), t.Lookup(templateName), w)
if err != nil {
return
}
}
for _, d := range dd {
if d.Locale != nil {
}
}
return
}
-69
View File
@@ -1,69 +0,0 @@
package gen
import (
"fmt"
"text/template"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/def"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/tpl"
)
func RBAC(t *template.Template, dd []*def.Document) error {
return List{
"type": rbacTypes,
"service access control": rbacAccessControlService,
}.Generate(t, dd)
}
// RbacTypes generates rbac definitions (one per component)
// <service>/service/rbac.gen.go
//
// Contains all RBAC related definitions
func rbacTypes(t *template.Template, dd []*def.Document) (err error) {
const (
templateName = "rbac/types.go.tpl"
outputPathTpl = "%s/types/rbac.gen.go"
)
for component, perComponent := range partByComponent(dd) {
w := tpl.Wrap{
Package: "types",
Component: component,
Def: perComponent,
}
err = tpl.GoTemplate(fmt.Sprintf(outputPathTpl, component), t.Lookup(templateName), w)
if err != nil {
return
}
}
return
}
// RbacAccessControlService generates access control functions (one file per component)
// <service>/service/rbac.gen.go
//
// Contains all RBAC related definitions
func rbacAccessControlService(t *template.Template, dd []*def.Document) (err error) {
const (
templateName = "rbac/access_control.go.tpl"
outputPathTpl = "%s/service/access_control.gen.go"
)
for component, perComponent := range partByComponent(dd) {
w := tpl.Wrap{
Package: "service",
Component: component,
Def: perComponent,
Imports: append(collectImports(perComponent...), cImport(component, "types")),
}
err = tpl.GoTemplate(fmt.Sprintf(outputPathTpl, component), t.Lookup(templateName), w)
if err != nil {
return
}
}
return
}
-126
View File
@@ -1,126 +0,0 @@
package tpl
import (
"bytes"
"fmt"
"go/format"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/Masterminds/sprig"
)
type (
Wrap struct {
Package string
// will be set when grouping definitions by component
Component string
Imports []string
Def interface{}
}
)
var nonIdentChars = regexp.MustCompile(`[\s\\/\-.]+`)
func Export(pp ...string) (out string) {
for _, p := range pp {
if len(p) > 1 {
p = strings.ToUpper(p[:1]) + p[1:]
}
if ss := nonIdentChars.Split(p, -1); len(ss) > 1 {
p = Export(ss...)
}
out = out + p
}
return out
}
func Unexport(pp ...string) (out string) {
out = Export(pp...)
if len(out) == 0 {
return
}
if out == "ID" {
return "id"
}
return strings.ToLower(out[:1]) + out[1:]
}
func BaseTemplate() *template.Template {
return template.New("").
Funcs(sprig.TxtFuncMap()).
Funcs(map[string]interface{}{
"export": Export,
"unexport": Unexport,
})
}
func LoadTemplates(rTpl *template.Template, rootDir string) (*template.Template, error) {
cleanRoot := filepath.Clean(rootDir)
pfx := len(cleanRoot) + 1
return rTpl, filepath.Walk(cleanRoot, func(path string, info os.FileInfo, err error) error {
if info.IsDir() || !strings.HasSuffix(path, ".tpl") || err != nil {
return err
}
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
name := path[pfx:]
rTpl, err = rTpl.New(name).Parse(string(b))
return err
})
}
func GoTemplate(dst string, tpl *template.Template, payload Wrap) (err error) {
var output io.WriteCloser
buf := bytes.Buffer{}
if tpl == nil {
return fmt.Errorf("could not find template for %s", dst)
}
if err := tpl.Execute(&buf, payload); err != nil {
return err
}
fmtsrc, err := format.Source(buf.Bytes())
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s fmt warn: %v\n", dst, err)
err = nil
fmtsrc = buf.Bytes()
}
if dst == "" || dst == "-" {
output = os.Stdout
} else {
if output, err = os.Create(dst); err != nil {
return err
}
defer output.Close()
}
if _, err = output.Write(fmtsrc); err != nil {
return err
}
return nil
}
-78
View File
@@ -1,78 +0,0 @@
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/cortezaproject/corteza-server/pkg/cli"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/def"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/gen"
"github.com/cortezaproject/corteza-server/pkg/codegen-v3/internal/tpl"
"github.com/davecgh/go-spew/spew"
"gopkg.in/yaml.v3"
)
var _ = spew.Dump
func main() {
dd, err := loadDefinitions(os.Args[1])
cli.HandleError(err)
tpls, err := tpl.LoadTemplates(tpl.BaseTemplate(), "./pkg/codegen-v3/assets/templates/gocode")
if err != nil {
cli.HandleError(fmt.Errorf("could not load templates: %w", err))
}
cli.HandleError(gen.List{
"RBAC": gen.RBAC,
"Envoy": gen.Envoy,
"Locale": gen.Locale,
}.Generate(tpls, dd))
}
func loadDefinition(r io.Reader) (*def.Document, error) {
doc := &def.Document{
Envoy: true,
}
return doc, yaml.NewDecoder(r).Decode(doc)
}
func loadDefinitions(path string) (dd []*def.Document, err error) {
var (
fh *os.File
doc *def.Document
files []string
)
files, err = filepath.Glob(path + "/*.yaml")
if err != nil {
return nil, fmt.Errorf("could not load ddefinitions form path '%s': %w", path, err)
}
for _, file := range files {
fh, err = os.Open(file)
if err != nil {
return nil, fmt.Errorf("could not load definiton file '%s': %w", file, err)
}
doc, err = loadDefinition(fh)
if err != nil {
return nil, fmt.Errorf("could not load definiton from '%s': %w", file, err)
}
if doc.Skip {
continue
}
if err = doc.Proc(filepath.Base(file)); err != nil {
return nil, fmt.Errorf("failed to preprocess definitions from '%s': %w", file, err)
}
dd = append(dd, doc)
}
return
}
+1 -2
View File
@@ -8,10 +8,9 @@ package resource
import (
"fmt"
"strings"
composeTypes "github.com/cortezaproject/corteza-server/compose/types"
systemTypes "github.com/cortezaproject/corteza-server/system/types"
"strings"
)
// Parse generates resource setting logic for each resource
+10 -21
View File
@@ -6,16 +6,20 @@ package resource
// the code is regenerated.
//
// Definitions file that controls how this file is generated:
// - compose.module-field.yaml
// - compose.module.yaml
// - compose.namespace.yaml
// - compose.page.yaml
import (
systemTypes "github.com/cortezaproject/corteza-server/system/types"
)
func (r *ComposeModule) 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...))
return out, nil
}
func (r *ComposeModuleField) EncodeTranslations() ([]*ResourceTranslation, error) {
out := make([]*ResourceTranslation, 0, 10)
@@ -26,20 +30,6 @@ func (r *ComposeModuleField) EncodeTranslations() ([]*ResourceTranslation, error
return out, nil
}
func (r *ComposeModule) 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...))
tmp, err := r.encodeTranslations()
return append(out, tmp...), err
}
func (r *ComposeNamespace) EncodeTranslations() ([]*ResourceTranslation, error) {
out := make([]*ResourceTranslation, 0, 10)
@@ -50,7 +40,6 @@ func (r *ComposeNamespace) EncodeTranslations() ([]*ResourceTranslation, error)
return out, nil
}
func (r *ComposePage) EncodeTranslations() ([]*ResourceTranslation, error) {
out := make([]*ResourceTranslation, 0, 10)
+13 -30
View File
@@ -6,19 +6,14 @@ package resource
// the code is regenerated.
//
// Definitions file that controls how this file is generated:
// - compose.module-field.yaml
// - compose.module.yaml
// - compose.namespace.yaml
// - compose.page.yaml
import (
"fmt"
composeTypes "github.com/cortezaproject/corteza-server/compose/types"
systemTypes "github.com/cortezaproject/corteza-server/system/types"
"strings"
)
// Parse generates resource setting logic for each resource
// ParseResourceTranslation generates resource setting logic for each resource
//
// Resources with "envoy: false" are skipped
//
@@ -53,41 +48,32 @@ func ParseResourceTranslation(res string) (string, *Ref, []*Ref, error) {
// make the resource provide the slice of parent resources we should nest under
switch resourceType {
case composeTypes.ModuleFieldResourceTranslationType:
if len(path) != 3 {
return "", nil, nil, fmt.Errorf("expecting 3 reference components in path, got %d", len(path))
}
ref, pp, err := ComposeModuleFieldResourceTranslationReferences(
// namespace
path[0],
// module
path[1],
// moduleField
path[2],
)
return composeTypes.ModuleFieldResourceTranslationType, ref, pp, err
case composeTypes.ModuleResourceTranslationType:
if len(path) != 2 {
return "", nil, nil, fmt.Errorf("expecting 2 reference components in path, got %d", len(path))
}
ref, pp, err := ComposeModuleResourceTranslationReferences(
// namespace
path[0],
// module
path[1],
)
return composeTypes.ModuleResourceTranslationType, ref, pp, err
case composeTypes.ModuleFieldResourceTranslationType:
if len(path) != 3 {
return "", nil, nil, fmt.Errorf("expecting 3 reference components in path, got %d", len(path))
}
ref, pp, err := ComposeModuleFieldResourceTranslationReferences(
path[0],
path[1],
path[2],
)
return composeTypes.ModuleFieldResourceTranslationType, ref, pp, err
case composeTypes.NamespaceResourceTranslationType:
if len(path) != 1 {
return "", nil, nil, fmt.Errorf("expecting 1 reference components in path, got %d", len(path))
}
ref, pp, err := ComposeNamespaceResourceTranslationReferences(
// namespace
path[0],
)
return composeTypes.NamespaceResourceTranslationType, ref, pp, err
@@ -97,10 +83,7 @@ func ParseResourceTranslation(res string) (string, *Ref, []*Ref, error) {
return "", nil, nil, fmt.Errorf("expecting 2 reference components in path, got %d", len(path))
}
ref, pp, err := ComposePageResourceTranslationReferences(
// namespace
path[0],
// page
path[1],
)
return composeTypes.PageResourceTranslationType, ref, pp, err
@@ -6,60 +6,46 @@ package resource
// the code is regenerated.
//
// Definitions file that controls how this file is generated:
// - compose.module-field.yaml
// - compose.module.yaml
// - compose.namespace.yaml
// - compose.page.yaml
import (
"github.com/cortezaproject/corteza-server/compose/types"
)
// ComposeModuleFieldResourceTranslationReferences generates Locale references
//
// Resources with "envoy: false" are skipped
// ComposeModuleResourceTranslationReferences generates Locale references
//
// This function is auto-generated
func ComposeModuleFieldResourceTranslationReferences(namespace string, module string, moduleField string) (res *Ref, pp []*Ref, err error) {
pp = append(pp, &Ref{ResourceType: types.NamespaceResourceType, Identifiers: MakeIdentifiers(namespace)})
pp = append(pp, &Ref{ResourceType: types.ModuleResourceType, Identifiers: MakeIdentifiers(module)})
res = &Ref{ResourceType: types.ModuleFieldResourceType, Identifiers: MakeIdentifiers(moduleField)}
func ComposeModuleResourceTranslationReferences(namespaceID string, self string) (res *Ref, pp []*Ref, err error) {
res = &Ref{ResourceType: types.ModuleResourceType, Identifiers: MakeIdentifiers(self)}
pp = append(pp, &Ref{ResourceType: types.NamespaceResourceType, Identifiers: MakeIdentifiers(namespaceID)})
return
}
// ComposeModuleResourceTranslationReferences generates Locale references
//
// Resources with "envoy: false" are skipped
// ComposeModuleFieldResourceTranslationReferences generates Locale references
//
// This function is auto-generated
func ComposeModuleResourceTranslationReferences(namespace string, module string) (res *Ref, pp []*Ref, err error) {
pp = append(pp, &Ref{ResourceType: types.NamespaceResourceType, Identifiers: MakeIdentifiers(namespace)})
res = &Ref{ResourceType: types.ModuleResourceType, Identifiers: MakeIdentifiers(module)}
func ComposeModuleFieldResourceTranslationReferences(namespaceID string, moduleID string, self string) (res *Ref, pp []*Ref, err error) {
res = &Ref{ResourceType: types.ModuleFieldResourceType, Identifiers: MakeIdentifiers(self)}
pp = append(pp, &Ref{ResourceType: types.NamespaceResourceType, Identifiers: MakeIdentifiers(namespaceID)})
pp = append(pp, &Ref{ResourceType: types.ModuleResourceType, Identifiers: MakeIdentifiers(moduleID)})
return
}
// ComposeNamespaceResourceTranslationReferences generates Locale references
//
// Resources with "envoy: false" are skipped
//
// This function is auto-generated
func ComposeNamespaceResourceTranslationReferences(namespace string) (res *Ref, pp []*Ref, err error) {
res = &Ref{ResourceType: types.NamespaceResourceType, Identifiers: MakeIdentifiers(namespace)}
func ComposeNamespaceResourceTranslationReferences(self string) (res *Ref, pp []*Ref, err error) {
res = &Ref{ResourceType: types.NamespaceResourceType, Identifiers: MakeIdentifiers(self)}
return
}
// ComposePageResourceTranslationReferences generates Locale references
//
// Resources with "envoy: false" are skipped
//
// This function is auto-generated
func ComposePageResourceTranslationReferences(namespace string, page string) (res *Ref, pp []*Ref, err error) {
pp = append(pp, &Ref{ResourceType: types.NamespaceResourceType, Identifiers: MakeIdentifiers(namespace)})
res = &Ref{ResourceType: types.PageResourceType, Identifiers: MakeIdentifiers(page)}
func ComposePageResourceTranslationReferences(namespaceID string, self string) (res *Ref, pp []*Ref, err error) {
res = &Ref{ResourceType: types.PageResourceType, Identifiers: MakeIdentifiers(self)}
pp = append(pp, &Ref{ResourceType: types.NamespaceResourceType, Identifiers: MakeIdentifiers(namespaceID)})
return
}
@@ -1,25 +0,0 @@
package resource
// 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:
// - system.report.yaml
import (
"github.com/cortezaproject/corteza-server/system/types"
)
// SystemReportResourceTranslationReferences generates Locale references
//
// Resources with "envoy: false" are skipped
//
// This function is auto-generated
func SystemReportResourceTranslationReferences(report string) (res *Ref, pp []*Ref, err error) {
res = &Ref{ResourceType: types.ReportResourceType, Identifiers: MakeIdentifiers(report)}
return
}