Additional codegen tweaks

This commit is contained in:
Tomaž Jerman
2021-09-22 11:26:14 +02:00
parent e6a3232ea1
commit cea8ca39d3
11 changed files with 399 additions and 10 deletions
+146
View File
@@ -0,0 +1,146 @@
package service
// 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:
// - automation.workflow.yaml
import (
"context"
"github.com/cortezaproject/corteza-server/automation/types"
"github.com/cortezaproject/corteza-server/pkg/actionlog"
"github.com/cortezaproject/corteza-server/pkg/auth"
"github.com/cortezaproject/corteza-server/pkg/locale"
"github.com/cortezaproject/corteza-server/store"
systemTypes "github.com/cortezaproject/corteza-server/system/types"
)
type (
resourceTranslation struct {
actionlog actionlog.Recorder
locale locale.Resource
store store.Storer
ac localeAccessController
}
localeAccessController interface {
// CanManageResourceTranslation(context.Context) bool
}
ResourceTranslationService interface {
Workflow(ctx context.Context, ID uint64) (locale.ResourceTranslationSet, error)
Upsert(context.Context, locale.ResourceTranslationSet) error
Locale() locale.Resource
}
)
func ResourceTranslation(ls locale.Resource) *resourceTranslation {
return &resourceTranslation{
actionlog: DefaultActionlog,
store: DefaultStore,
ac: DefaultAccessControl,
locale: ls,
}
}
func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTranslationSet) (err error) {
// @todo AC
//if (!svc.ac.CanManageResourceTranslation(ctx)) {
// return *****ErrNotAllowedToCreate()
//}
// @todo validation
me := auth.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 resourceTranslation) Locale() locale.Resource {
return svc.locale
}
func (svc resourceTranslation) Workflow(ctx context.Context, ID uint64) (locale.ResourceTranslationSet, error) {
var (
err error
out locale.ResourceTranslationSet
res *types.Workflow
k types.LocaleKey
)
res, err = svc.loadWorkflow(ctx, svc.store, ID)
if err != nil {
return nil, err
}
for _, tag := range svc.locale.Tags() {
k = types.LocaleKeyWorkflowName
out = append(out, &locale.ResourceTranslation{
Resource: res.ResourceTranslation(),
Lang: tag.String(),
Key: k.Path,
Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), k.Path),
})
k = types.LocaleKeyWorkflowDescription
out = append(out, &locale.ResourceTranslation{
Resource: res.ResourceTranslation(),
Lang: tag.String(),
Key: k.Path,
Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), k.Path),
})
}
return out, nil
}
+12
View File
@@ -0,0 +1,12 @@
package service
import (
"context"
"github.com/cortezaproject/corteza-server/automation/types"
"github.com/cortezaproject/corteza-server/store"
)
func (svc resourceTranslation) loadWorkflow(ctx context.Context, s store.Storer, ID uint64) (*types.Workflow, error) {
return loadWorkflow(ctx, s, ID)
}
+109
View File
@@ -0,0 +1,109 @@
package types
// 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:
// - automation.workflow.yaml
import (
"fmt"
"github.com/cortezaproject/corteza-server/pkg/locale"
"strconv"
)
type (
LocaleKey struct {
Name string
Resource string
Path string
CustomHandler string
}
)
// Types and stuff
const (
WorkflowResourceTranslationType = "automation:workflow"
)
var (
LocaleKeyWorkflowName = LocaleKey{
Name: "name",
Resource: WorkflowResourceTranslationType,
Path: "name",
}
LocaleKeyWorkflowDescription = LocaleKey{
Name: "description",
Resource: WorkflowResourceTranslationType,
Path: "description",
}
)
// ResourceTranslation returns string representation of Locale resource for Workflow by calling WorkflowResourceTranslation fn
//
// Locale resource is in the automation:workflow/... format
//
// This function is auto-generated
func (r Workflow) ResourceTranslation() string {
return WorkflowResourceTranslation(r.ID)
}
// WorkflowResourceTranslation returns string representation of Locale resource for Workflow
//
// Locale resource is in the automation:workflow/... format
//
// This function is auto-generated
func WorkflowResourceTranslation(id uint64) string {
cpts := []interface{}{WorkflowResourceTranslationType}
cpts = append(cpts, strconv.FormatUint(id, 10))
return fmt.Sprintf(WorkflowResourceTranslationTpl(), cpts...)
}
// @todo template
func WorkflowResourceTranslationTpl() string {
return "%s/%s"
}
func (r *Workflow) DecodeTranslations(tt locale.ResourceTranslationIndex) {
var aux *locale.ResourceTranslation
if aux = tt.FindByKey(LocaleKeyWorkflowName.Path); aux != nil {
r.Meta.Name = aux.Msg
} else {
r.Meta.Name = LocaleKeyWorkflowName.Path
}
if aux = tt.FindByKey(LocaleKeyWorkflowDescription.Path); aux != nil {
r.Meta.Description = aux.Msg
} else {
r.Meta.Description = LocaleKeyWorkflowDescription.Path
}
}
func (r *Workflow) EncodeTranslations() (out locale.ResourceTranslationSet) {
out = locale.ResourceTranslationSet{
{
Resource: r.ResourceTranslation(),
Key: LocaleKeyWorkflowName.Path,
Msg: firstOkString(r.Meta.Name, LocaleKeyWorkflowName.Path),
},
{
Resource: r.ResourceTranslation(),
Key: LocaleKeyWorkflowDescription.Path,
Msg: firstOkString(r.Meta.Description, LocaleKeyWorkflowDescription.Path),
},
}
return out
}
func firstOkString(ss ...string) string {
for _, s := range ss {
if s != "" {
return s
}
}
return ""
}
+29 -1
View File
@@ -7,14 +7,15 @@ package service
//
// Definitions file that controls how this file is generated:
// - compose.chart.yaml
// - compose.module.yaml
// - compose.namespace.yaml
// - compose.page.yaml
import (
"context"
"github.com/cortezaproject/corteza-server/compose/types"
"github.com/cortezaproject/corteza-server/pkg/actionlog"
"github.com/cortezaproject/corteza-server/pkg/auth"
"github.com/cortezaproject/corteza-server/pkg/locale"
@@ -35,6 +36,7 @@ type (
}
ResourceTranslationService interface {
Chart(ctx context.Context, namespaceID uint64, ID uint64) (locale.ResourceTranslationSet, error)
Module(ctx context.Context, namespaceID uint64, ID uint64) (locale.ResourceTranslationSet, error)
Namespace(ctx context.Context, ID uint64) (locale.ResourceTranslationSet, error)
Page(ctx context.Context, namespaceID uint64, ID uint64) (locale.ResourceTranslationSet, error)
@@ -115,6 +117,32 @@ func (svc resourceTranslation) Locale() locale.Resource {
return svc.locale
}
func (svc resourceTranslation) Chart(ctx context.Context, namespaceID uint64, ID uint64) (locale.ResourceTranslationSet, error) {
var (
err error
out locale.ResourceTranslationSet
res *types.Chart
k types.LocaleKey
)
res, err = svc.loadChart(ctx, svc.store, namespaceID, ID)
if err != nil {
return nil, err
}
for _, tag := range svc.locale.Tags() {
k = types.LocaleKeyChartName
out = append(out, &locale.ResourceTranslation{
Resource: res.ResourceTranslation(),
Lang: tag.String(),
Key: k.Path,
Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), k.Path),
})
}
return out, nil
}
func (svc resourceTranslation) Module(ctx context.Context, namespaceID uint64, ID uint64) (locale.ResourceTranslationSet, error) {
var (
err error
+5
View File
@@ -148,3 +148,8 @@ func (svc resourceTranslation) loadPage(ctx context.Context, s store.Storer, nam
_, m, err = loadPage(ctx, s, namespaceID, pageID)
return m, err
}
func (svc resourceTranslation) loadChart(ctx context.Context, s store.Storer, namespaceID, chartID uint64) (m *types.Chart, err error) {
_, m, err = loadChart(ctx, s, namespaceID, chartID)
return m, err
}
+70 -7
View File
@@ -7,6 +7,7 @@ package types
//
// Definitions file that controls how this file is generated:
// - compose.chart.yaml
// - compose.module-field.yaml
// - compose.module.yaml
// - compose.namespace.yaml
@@ -29,6 +30,7 @@ type (
// Types and stuff
const (
ChartResourceTranslationType = "compose:chart"
ModuleFieldResourceTranslationType = "compose:module-field"
ModuleResourceTranslationType = "compose:module"
NamespaceResourceTranslationType = "compose:namespace"
@@ -36,6 +38,11 @@ const (
)
var (
LocaleKeyChartName = LocaleKey{
Name: "name",
Resource: ChartResourceTranslationType,
Path: "name",
}
LocaleKeyModuleFieldLabel = LocaleKey{
Name: "label",
Resource: ModuleFieldResourceTranslationType,
@@ -94,6 +101,53 @@ var (
}
)
// ResourceTranslation returns string representation of Locale resource for Chart by calling ChartResourceTranslation fn
//
// Locale resource is in the compose:chart/... format
//
// This function is auto-generated
func (r Chart) ResourceTranslation() string {
return ChartResourceTranslation(r.NamespaceID, r.ID)
}
// ChartResourceTranslation returns string representation of Locale resource for Chart
//
// Locale resource is in the compose:chart/... format
//
// This function is auto-generated
func ChartResourceTranslation(namespaceID uint64, id uint64) string {
cpts := []interface{}{ChartResourceTranslationType}
cpts = append(cpts, strconv.FormatUint(namespaceID, 10), strconv.FormatUint(id, 10))
return fmt.Sprintf(ChartResourceTranslationTpl(), cpts...)
}
// @todo template
func ChartResourceTranslationTpl() string {
return "%s/%s/%s"
}
func (r *Chart) DecodeTranslations(tt locale.ResourceTranslationIndex) {
var aux *locale.ResourceTranslation
if aux = tt.FindByKey(LocaleKeyChartName.Path); aux != nil {
r.Name = aux.Msg
} else {
r.Name = LocaleKeyChartName.Path
}
}
func (r *Chart) EncodeTranslations() (out locale.ResourceTranslationSet) {
out = locale.ResourceTranslationSet{
{
Resource: r.ResourceTranslation(),
Key: LocaleKeyChartName.Path,
Msg: firstOkString(r.Name, LocaleKeyChartName.Path),
},
}
return out
}
// ResourceTranslation returns string representation of Locale resource for ModuleField by calling ModuleFieldResourceTranslation fn
//
// Locale resource is in the compose:module-field/... format
@@ -135,7 +189,7 @@ func (r *ModuleField) EncodeTranslations() (out locale.ResourceTranslationSet) {
{
Resource: r.ResourceTranslation(),
Key: LocaleKeyModuleFieldLabel.Path,
Msg: r.Label,
Msg: firstOkString(r.Label, LocaleKeyModuleFieldLabel.Path),
},
}
@@ -186,7 +240,7 @@ func (r *Module) EncodeTranslations() (out locale.ResourceTranslationSet) {
{
Resource: r.ResourceTranslation(),
Key: LocaleKeyModuleName.Path,
Msg: r.Name,
Msg: firstOkString(r.Name, LocaleKeyModuleName.Path),
},
}
@@ -245,17 +299,17 @@ func (r *Namespace) EncodeTranslations() (out locale.ResourceTranslationSet) {
{
Resource: r.ResourceTranslation(),
Key: LocaleKeyNamespaceName.Path,
Msg: r.Name,
Msg: firstOkString(r.Name, LocaleKeyNamespaceName.Path),
},
{
Resource: r.ResourceTranslation(),
Key: LocaleKeyNamespaceSubtitle.Path,
Msg: r.Meta.Subtitle,
Msg: firstOkString(r.Meta.Subtitle, LocaleKeyNamespaceSubtitle.Path),
},
{
Resource: r.ResourceTranslation(),
Key: LocaleKeyNamespaceDescription.Path,
Msg: r.Meta.Description,
Msg: firstOkString(r.Meta.Description, LocaleKeyNamespaceDescription.Path),
},
}
@@ -309,12 +363,12 @@ func (r *Page) EncodeTranslations() (out locale.ResourceTranslationSet) {
{
Resource: r.ResourceTranslation(),
Key: LocaleKeyPageTitle.Path,
Msg: r.Title,
Msg: firstOkString(r.Title, LocaleKeyPageTitle.Path),
},
{
Resource: r.ResourceTranslation(),
Key: LocaleKeyPageDescription.Path,
Msg: r.Description,
Msg: firstOkString(r.Description, LocaleKeyPageDescription.Path),
},
}
@@ -322,3 +376,12 @@ func (r *Page) EncodeTranslations() (out locale.ResourceTranslationSet) {
return out
}
func firstOkString(ss ...string) string {
for _, s := range ss {
if s != "" {
return s
}
}
return ""
}
+7
View File
@@ -14,3 +14,10 @@ rbac:
description: Manage workflow triggers
sessions.manage:
description: Manage workflow sessions
locale:
resource:
references: [ ID ]
keys:
- { path: name, field: "Meta.Name" }
- { path: description, field: "Meta.Description" }
+6
View File
@@ -9,3 +9,9 @@ rbac:
description: Update chart
delete:
description: Delete chart
locale:
resource:
references: [ namespace, ID ]
keys:
- name
@@ -6,7 +6,10 @@ package {{ .Package }}
import (
"context"
"github.com/cortezaproject/corteza-server/compose/types"
{{- range .Imports }}
{{ . }}
{{- end }}
"github.com/cortezaproject/corteza-server/pkg/actionlog"
"github.com/cortezaproject/corteza-server/pkg/auth"
"github.com/cortezaproject/corteza-server/pkg/locale"
@@ -113,7 +113,7 @@ func (r *{{ .Resource }}) EncodeTranslations() (out locale.ResourceTranslationSe
{
Resource: r.ResourceTranslation(),
Key: LocaleKey{{ $Resource }}{{coalesce (export .Name) (export .Path) }}.Path,
Msg: r.{{ .Field }},
Msg: firstOkString(r.{{ .Field }}, LocaleKey{{ $Resource }}{{coalesce (export .Name) (export .Path) }}.Path),
},
{{- end}}
{{- end}}
@@ -132,3 +132,12 @@ func (r *{{ .Resource }}) EncodeTranslations() (out locale.ResourceTranslationSe
}
{{- end }}
func firstOkString(ss ...string) string {
for _, s := range ss {
if s != "" {
return s
}
}
return ""
}
+1
View File
@@ -52,6 +52,7 @@ func localeServices(t *template.Template, dd []*def.Document) (err error) {
Package: "service",
Component: component,
Def: perComponent,
Imports: append(collectImports(perComponent...), cImport(component, "types")),
}
err = tpl.GoTemplate(fmt.Sprintf(outputPathTpl, component), t.Lookup(templateName), w)