From cea8ca39d3de3ae902f8c0b2d6f60ef43a6fb531 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Wed, 15 Sep 2021 17:03:15 +0200 Subject: [PATCH] Additional codegen tweaks --- automation/service/locale.gen.go | 146 ++++++++++++++++++ automation/service/locale.go | 12 ++ automation/types/locale.gen.go | 109 +++++++++++++ compose/service/locale.gen.go | 30 +++- compose/service/locale.go | 5 + compose/types/locale.gen.go | 77 ++++++++- def/automation.workflow.yaml | 7 + def/compose.chart.yaml | 6 + .../templates/gocode/locale/service.go.tpl | 5 +- .../templates/gocode/locale/types.go.tpl | 11 +- pkg/codegen-v3/internal/gen/locale.go | 1 + 11 files changed, 399 insertions(+), 10 deletions(-) create mode 100644 automation/service/locale.gen.go create mode 100644 automation/service/locale.go create mode 100644 automation/types/locale.gen.go diff --git a/automation/service/locale.gen.go b/automation/service/locale.gen.go new file mode 100644 index 000000000..51823097c --- /dev/null +++ b/automation/service/locale.gen.go @@ -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 +} diff --git a/automation/service/locale.go b/automation/service/locale.go new file mode 100644 index 000000000..c95bc03e9 --- /dev/null +++ b/automation/service/locale.go @@ -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) +} diff --git a/automation/types/locale.gen.go b/automation/types/locale.gen.go new file mode 100644 index 000000000..f15e49196 --- /dev/null +++ b/automation/types/locale.gen.go @@ -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 "" +} diff --git a/compose/service/locale.gen.go b/compose/service/locale.gen.go index 388540180..62af373c7 100644 --- a/compose/service/locale.gen.go +++ b/compose/service/locale.gen.go @@ -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 diff --git a/compose/service/locale.go b/compose/service/locale.go index 39d92a42d..b732ba3b7 100644 --- a/compose/service/locale.go +++ b/compose/service/locale.go @@ -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 +} diff --git a/compose/types/locale.gen.go b/compose/types/locale.gen.go index b72d96753..c662c6f45 100644 --- a/compose/types/locale.gen.go +++ b/compose/types/locale.gen.go @@ -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 "" +} diff --git a/def/automation.workflow.yaml b/def/automation.workflow.yaml index 4b081a784..0738d0f49 100644 --- a/def/automation.workflow.yaml +++ b/def/automation.workflow.yaml @@ -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" } diff --git a/def/compose.chart.yaml b/def/compose.chart.yaml index 3f4435168..200d8cd82 100644 --- a/def/compose.chart.yaml +++ b/def/compose.chart.yaml @@ -9,3 +9,9 @@ rbac: description: Update chart delete: description: Delete chart + +locale: + resource: + references: [ namespace, ID ] + keys: + - name diff --git a/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl b/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl index d06f17c6b..78cabfb2c 100644 --- a/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl +++ b/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl @@ -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" diff --git a/pkg/codegen-v3/assets/templates/gocode/locale/types.go.tpl b/pkg/codegen-v3/assets/templates/gocode/locale/types.go.tpl index 34fe7a47e..2c2c50abc 100644 --- a/pkg/codegen-v3/assets/templates/gocode/locale/types.go.tpl +++ b/pkg/codegen-v3/assets/templates/gocode/locale/types.go.tpl @@ -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 "" +} diff --git a/pkg/codegen-v3/internal/gen/locale.go b/pkg/codegen-v3/internal/gen/locale.go index 01147e4cd..2f52ba3fd 100644 --- a/pkg/codegen-v3/internal/gen/locale.go +++ b/pkg/codegen-v3/internal/gen/locale.go @@ -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)