From 8cae39f231cb2f8c65c8aed9179fb3a5015af3fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Fri, 17 Sep 2021 12:43:27 +0200 Subject: [PATCH] Extend codegen for system resources * Rename generated services * Add definitions for reports --- automation/service/locale.gen.go | 18 +-- automation/service/locale.go | 2 +- compose/rest/module.go | 2 +- compose/rest/namespace.go | 2 +- compose/rest/page.go | 2 +- compose/service/locale.gen.go | 24 +-- compose/service/locale.go | 16 +- compose/service/module.go | 2 +- compose/service/namespace.go | 2 +- compose/service/page.go | 2 +- compose/service/service.go | 4 +- compose/types/locale.gen.go | 23 +-- def/system.report.yaml | 8 + .../templates/gocode/locale/service.go.tpl | 18 +-- pkg/report/frame.go | 2 +- system/service/locale.gen.go | 148 ++++++++++++++++++ system/service/locale.go | 49 ++++++ system/types/locale.gen.go | 113 +++++++++++++ system/types/report.go | 68 ++++++-- 19 files changed, 426 insertions(+), 79 deletions(-) create mode 100644 system/service/locale.gen.go create mode 100644 system/service/locale.go create mode 100644 system/types/locale.gen.go diff --git a/automation/service/locale.gen.go b/automation/service/locale.gen.go index 51823097c..d39c7f036 100644 --- a/automation/service/locale.gen.go +++ b/automation/service/locale.gen.go @@ -14,14 +14,14 @@ import ( "github.com/cortezaproject/corteza-server/automation/types" "github.com/cortezaproject/corteza-server/pkg/actionlog" - "github.com/cortezaproject/corteza-server/pkg/auth" + intAuth "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 { + resourceTranslationsManager struct { actionlog actionlog.Recorder locale locale.Resource store store.Storer @@ -32,7 +32,7 @@ type ( // CanManageResourceTranslation(context.Context) bool } - ResourceTranslationService interface { + ResourceTranslationsManagerService interface { Workflow(ctx context.Context, ID uint64) (locale.ResourceTranslationSet, error) Upsert(context.Context, locale.ResourceTranslationSet) error @@ -40,8 +40,8 @@ type ( } ) -func ResourceTranslation(ls locale.Resource) *resourceTranslation { - return &resourceTranslation{ +func ResourceTranslationsManager(ls locale.Resource) *resourceTranslationsManager { + return &resourceTranslationsManager{ actionlog: DefaultActionlog, store: DefaultStore, ac: DefaultAccessControl, @@ -49,7 +49,7 @@ func ResourceTranslation(ls locale.Resource) *resourceTranslation { } } -func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTranslationSet) (err error) { +func (svc resourceTranslationsManager) Upsert(ctx context.Context, rr locale.ResourceTranslationSet) (err error) { // @todo AC //if (!svc.ac.CanManageResourceTranslation(ctx)) { // return *****ErrNotAllowedToCreate() @@ -57,7 +57,7 @@ func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTra // @todo validation - me := auth.GetIdentityFromContext(ctx) + me := intAuth.GetIdentityFromContext(ctx) // - group by resource localeByRes := make(map[string]locale.ResourceTranslationSet) @@ -107,11 +107,11 @@ func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTra return nil } -func (svc resourceTranslation) Locale() locale.Resource { +func (svc resourceTranslationsManager) Locale() locale.Resource { return svc.locale } -func (svc resourceTranslation) Workflow(ctx context.Context, ID uint64) (locale.ResourceTranslationSet, error) { +func (svc resourceTranslationsManager) Workflow(ctx context.Context, ID uint64) (locale.ResourceTranslationSet, error) { var ( err error out locale.ResourceTranslationSet diff --git a/automation/service/locale.go b/automation/service/locale.go index c95bc03e9..de16afb47 100644 --- a/automation/service/locale.go +++ b/automation/service/locale.go @@ -7,6 +7,6 @@ import ( "github.com/cortezaproject/corteza-server/store" ) -func (svc resourceTranslation) loadWorkflow(ctx context.Context, s store.Storer, ID uint64) (*types.Workflow, error) { +func (svc resourceTranslationsManager) loadWorkflow(ctx context.Context, s store.Storer, ID uint64) (*types.Workflow, error) { return loadWorkflow(ctx, s, ID) } diff --git a/compose/rest/module.go b/compose/rest/module.go index d069a3263..d1d84d292 100644 --- a/compose/rest/module.go +++ b/compose/rest/module.go @@ -38,7 +38,7 @@ type ( Module struct { module service.ModuleService - locale service.ResourceTranslationService + locale service.ResourceTranslationsManagerService namespace service.NamespaceService ac moduleAccessController } diff --git a/compose/rest/namespace.go b/compose/rest/namespace.go index 4a6a83256..46cd9536a 100644 --- a/compose/rest/namespace.go +++ b/compose/rest/namespace.go @@ -32,7 +32,7 @@ type ( Namespace struct { namespace service.NamespaceService - locale service.ResourceTranslationService + locale service.ResourceTranslationsManagerService attachment service.AttachmentService ac namespaceAccessController } diff --git a/compose/rest/page.go b/compose/rest/page.go index 66d13af53..1fb1a16a4 100644 --- a/compose/rest/page.go +++ b/compose/rest/page.go @@ -44,7 +44,7 @@ type ( Reorder(ctx context.Context, namespaceID, selfID uint64, pageIDs []uint64) error } - locale service.ResourceTranslationService + locale service.ResourceTranslationsManagerService namespace service.NamespaceService attachment service.AttachmentService ac pageAccessController diff --git a/compose/service/locale.gen.go b/compose/service/locale.gen.go index 62af373c7..d1376c1f7 100644 --- a/compose/service/locale.gen.go +++ b/compose/service/locale.gen.go @@ -17,14 +17,14 @@ import ( "github.com/cortezaproject/corteza-server/compose/types" "github.com/cortezaproject/corteza-server/pkg/actionlog" - "github.com/cortezaproject/corteza-server/pkg/auth" + intAuth "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 { + resourceTranslationsManager struct { actionlog actionlog.Recorder locale locale.Resource store store.Storer @@ -35,7 +35,7 @@ type ( // CanManageResourceTranslation(context.Context) bool } - ResourceTranslationService interface { + ResourceTranslationsManagerService 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) @@ -46,8 +46,8 @@ type ( } ) -func ResourceTranslation(ls locale.Resource) *resourceTranslation { - return &resourceTranslation{ +func ResourceTranslationsManager(ls locale.Resource) *resourceTranslationsManager { + return &resourceTranslationsManager{ actionlog: DefaultActionlog, store: DefaultStore, ac: DefaultAccessControl, @@ -55,7 +55,7 @@ func ResourceTranslation(ls locale.Resource) *resourceTranslation { } } -func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTranslationSet) (err error) { +func (svc resourceTranslationsManager) Upsert(ctx context.Context, rr locale.ResourceTranslationSet) (err error) { // @todo AC //if (!svc.ac.CanManageResourceTranslation(ctx)) { // return *****ErrNotAllowedToCreate() @@ -63,7 +63,7 @@ func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTra // @todo validation - me := auth.GetIdentityFromContext(ctx) + me := intAuth.GetIdentityFromContext(ctx) // - group by resource localeByRes := make(map[string]locale.ResourceTranslationSet) @@ -113,11 +113,11 @@ func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTra return nil } -func (svc resourceTranslation) Locale() locale.Resource { +func (svc resourceTranslationsManager) Locale() locale.Resource { return svc.locale } -func (svc resourceTranslation) Chart(ctx context.Context, namespaceID uint64, ID uint64) (locale.ResourceTranslationSet, error) { +func (svc resourceTranslationsManager) Chart(ctx context.Context, namespaceID uint64, ID uint64) (locale.ResourceTranslationSet, error) { var ( err error out locale.ResourceTranslationSet @@ -143,7 +143,7 @@ func (svc resourceTranslation) Chart(ctx context.Context, namespaceID uint64, ID return out, nil } -func (svc resourceTranslation) Module(ctx context.Context, namespaceID uint64, ID uint64) (locale.ResourceTranslationSet, error) { +func (svc resourceTranslationsManager) Module(ctx context.Context, namespaceID uint64, ID uint64) (locale.ResourceTranslationSet, error) { var ( err error out locale.ResourceTranslationSet @@ -171,7 +171,7 @@ func (svc resourceTranslation) Module(ctx context.Context, namespaceID uint64, I return append(out, tmp...), err } -func (svc resourceTranslation) Namespace(ctx context.Context, ID uint64) (locale.ResourceTranslationSet, error) { +func (svc resourceTranslationsManager) Namespace(ctx context.Context, ID uint64) (locale.ResourceTranslationSet, error) { var ( err error out locale.ResourceTranslationSet @@ -213,7 +213,7 @@ func (svc resourceTranslation) Namespace(ctx context.Context, ID uint64) (locale return out, nil } -func (svc resourceTranslation) Page(ctx context.Context, namespaceID uint64, ID uint64) (locale.ResourceTranslationSet, error) { +func (svc resourceTranslationsManager) Page(ctx context.Context, namespaceID uint64, ID uint64) (locale.ResourceTranslationSet, error) { var ( err error out locale.ResourceTranslationSet diff --git a/compose/service/locale.go b/compose/service/locale.go index b732ba3b7..3ffbf8cb2 100644 --- a/compose/service/locale.go +++ b/compose/service/locale.go @@ -12,7 +12,7 @@ import ( "golang.org/x/text/language" ) -func (svc resourceTranslation) moduleExtended(ctx context.Context, res *types.Module) (out locale.ResourceTranslationSet, err error) { +func (svc resourceTranslationsManager) moduleExtended(ctx context.Context, res *types.Module) (out locale.ResourceTranslationSet, err error) { var ( k types.LocaleKey ) @@ -39,7 +39,7 @@ func (svc resourceTranslation) moduleExtended(ctx context.Context, res *types.Mo return out, nil } -func (svc resourceTranslation) moduleFieldValidatorErrorHandler(ctx context.Context, tag language.Tag, f *types.ModuleField, k string) (locale.ResourceTranslationSet, error) { +func (svc resourceTranslationsManager) moduleFieldValidatorErrorHandler(ctx context.Context, tag language.Tag, f *types.ModuleField, k string) (locale.ResourceTranslationSet, error) { out := make(locale.ResourceTranslationSet, 0, 10) for i, v := range f.Expressions.Validators { @@ -60,7 +60,7 @@ func (svc resourceTranslation) moduleFieldValidatorErrorHandler(ctx context.Cont return out, nil } -func (svc resourceTranslation) pageExtended(ctx context.Context, res *types.Page) (out locale.ResourceTranslationSet, err error) { +func (svc resourceTranslationsManager) pageExtended(ctx context.Context, res *types.Page) (out locale.ResourceTranslationSet, err error) { var ( k types.LocaleKey ) @@ -104,7 +104,7 @@ func (svc resourceTranslation) pageExtended(ctx context.Context, res *types.Page return } -func (svc resourceTranslation) pageExtendedAutomatinBlock(tag language.Tag, res *types.Page, block types.PageBlock, blockID uint64, k types.LocaleKey) (locale.ResourceTranslationSet, error) { +func (svc resourceTranslationsManager) pageExtendedAutomatinBlock(tag language.Tag, res *types.Page, block types.PageBlock, blockID uint64, k types.LocaleKey) (locale.ResourceTranslationSet, error) { out := make(locale.ResourceTranslationSet, 0, 10) bb, _ := block.Options["buttons"].([]interface{}) @@ -136,20 +136,20 @@ func (svc resourceTranslation) pageExtendedAutomatinBlock(tag language.Tag, res // Helper loaders -func (svc resourceTranslation) loadModule(ctx context.Context, s store.Storer, namespaceID, moduleID uint64) (m *types.Module, err error) { +func (svc resourceTranslationsManager) loadModule(ctx context.Context, s store.Storer, namespaceID, moduleID uint64) (m *types.Module, err error) { return loadModule(ctx, s, moduleID) } -func (svc resourceTranslation) loadNamespace(ctx context.Context, s store.Storer, namespaceID uint64) (m *types.Namespace, err error) { +func (svc resourceTranslationsManager) loadNamespace(ctx context.Context, s store.Storer, namespaceID uint64) (m *types.Namespace, err error) { return loadNamespace(ctx, s, namespaceID) } -func (svc resourceTranslation) loadPage(ctx context.Context, s store.Storer, namespaceID, pageID uint64) (m *types.Page, err error) { +func (svc resourceTranslationsManager) loadPage(ctx context.Context, s store.Storer, namespaceID, pageID uint64) (m *types.Page, err error) { _, 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) { +func (svc resourceTranslationsManager) 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/service/module.go b/compose/service/module.go index fb47606f4..c08ae2ec0 100644 --- a/compose/service/module.go +++ b/compose/service/module.go @@ -27,7 +27,7 @@ type ( ac moduleAccessController eventbus eventDispatcher store store.Storer - locale ResourceTranslationService + locale ResourceTranslationsManagerService } moduleAccessController interface { diff --git a/compose/service/namespace.go b/compose/service/namespace.go index cfa0ddfa1..d7174059f 100644 --- a/compose/service/namespace.go +++ b/compose/service/namespace.go @@ -24,7 +24,7 @@ type ( ac namespaceAccessController eventbus eventDispatcher store store.Storer - locale ResourceTranslationService + locale ResourceTranslationsManagerService } namespaceAccessController interface { diff --git a/compose/service/page.go b/compose/service/page.go index 65d9caaf7..cc3c082fb 100644 --- a/compose/service/page.go +++ b/compose/service/page.go @@ -22,7 +22,7 @@ type ( ac pageAccessController eventbus eventDispatcher store store.Storer - locale ResourceTranslationService + locale ResourceTranslationsManagerService } pageAccessController interface { diff --git a/compose/service/service.go b/compose/service/service.go index 57dda2e1a..db05a6a1b 100644 --- a/compose/service/service.go +++ b/compose/service/service.go @@ -61,7 +61,7 @@ var ( DefaultPage *page DefaultAttachment AttachmentService DefaultNotification *notification - DefaultResourceTranslation ResourceTranslationService + DefaultResourceTranslation ResourceTranslationsManagerService // wrapper around time.Now() that will aid service testing now = func() *time.Time { @@ -99,7 +99,7 @@ func Initialize(ctx context.Context, log *zap.Logger, s store.Storer, c Config) DefaultActionlog = actionlog.NewService(DefaultStore, log, tee, policy) } - DefaultResourceTranslation = ResourceTranslation(locale.Global()) + DefaultResourceTranslation = ResourceTranslationsManager(locale.Global()) DefaultAccessControl = AccessControl() if DefaultObjectStore == nil { diff --git a/compose/types/locale.gen.go b/compose/types/locale.gen.go index e5cbaee34..94d835813 100644 --- a/compose/types/locale.gen.go +++ b/compose/types/locale.gen.go @@ -15,9 +15,8 @@ package types import ( "fmt" - "strconv" - "github.com/cortezaproject/corteza-server/pkg/locale" + "strconv" ) type ( @@ -49,26 +48,6 @@ var ( Resource: ModuleFieldResourceTranslationType, Path: "label", } - LocaleKeyModuleFieldDescriptionView = LocaleKey{ - Name: "descriptionView", - Resource: ModuleFieldResourceTranslationType, - Path: "meta.description.view", - } - LocaleKeyModuleFieldDescriptionEdit = LocaleKey{ - Name: "descriptionEdit", - Resource: ModuleFieldResourceTranslationType, - Path: "meta.description.edit", - } - LocaleKeyModuleFieldHintView = LocaleKey{ - Name: "hintView", - Resource: ModuleFieldResourceTranslationType, - Path: "meta.hint.view", - } - LocaleKeyModuleFieldHintEdit = LocaleKey{ - Name: "hintEdit", - Resource: ModuleFieldResourceTranslationType, - Path: "meta.hint.edit", - } LocaleKeyModuleFieldValidatorError = LocaleKey{ Name: "validatorError", Resource: ModuleFieldResourceTranslationType, diff --git a/def/system.report.yaml b/def/system.report.yaml index a4d880e4e..79824b25d 100644 --- a/def/system.report.yaml +++ b/def/system.report.yaml @@ -8,3 +8,11 @@ rbac: description: Delete report run: description: Run report + +locale: + extended: true + keys: + - { path: name, field: "Meta.Name" } + - { path: description, field: "Meta.Description" } + - { name: projection title, path: "projection.{{projectionID}}.title", custom: true } + - { name: projection description, path: "projection.{{projectionID}}.description", custom: true } 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 78cabfb2c..e1d8822cb 100644 --- a/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl +++ b/pkg/codegen-v3/assets/templates/gocode/locale/service.go.tpl @@ -11,14 +11,14 @@ import ( {{- end }} "github.com/cortezaproject/corteza-server/pkg/actionlog" - "github.com/cortezaproject/corteza-server/pkg/auth" + intAuth "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 { + resourceTranslationsManager struct { actionlog actionlog.Recorder locale locale.Resource store store.Storer @@ -29,7 +29,7 @@ type ( // CanManageResourceTranslation(context.Context) bool } - ResourceTranslationService interface { + ResourceTranslationsManagerService interface { {{- range .Def }} {{ .Resource }}(ctx context.Context, {{ if .Locale.Resource }}{{ range .Locale.Resource.References }}{{ .Field }} uint64, {{ end }}{{ end }}) (locale.ResourceTranslationSet, error) {{- end }} @@ -39,8 +39,8 @@ type ( } ) -func ResourceTranslation(ls locale.Resource) *resourceTranslation { - return &resourceTranslation{ +func ResourceTranslationsManager(ls locale.Resource) *resourceTranslationsManager { + return &resourceTranslationsManager{ actionlog: DefaultActionlog, store: DefaultStore, ac: DefaultAccessControl, @@ -48,7 +48,7 @@ func ResourceTranslation(ls locale.Resource) *resourceTranslation { } } -func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTranslationSet) (err error) { +func (svc resourceTranslationsManager) Upsert(ctx context.Context, rr locale.ResourceTranslationSet) (err error) { // @todo AC //if (!svc.ac.CanManageResourceTranslation(ctx)) { // return *****ErrNotAllowedToCreate() @@ -56,7 +56,7 @@ func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTra // @todo validation - me := auth.GetIdentityFromContext(ctx) + me := intAuth.GetIdentityFromContext(ctx) // - group by resource localeByRes := make(map[string]locale.ResourceTranslationSet) @@ -106,7 +106,7 @@ func (svc resourceTranslation) Upsert(ctx context.Context, rr locale.ResourceTra return nil } -func (svc resourceTranslation) Locale() locale.Resource { +func (svc resourceTranslationsManager) Locale() locale.Resource { return svc.locale } @@ -114,7 +114,7 @@ func (svc resourceTranslation) Locale() locale.Resource { {{ $Resource := .Resource }} {{ $GoType := printf "types.%s" .Resource }} -func (svc resourceTranslation) {{ .Resource }}(ctx context.Context, {{ if .Locale.Resource }}{{ range .Locale.Resource.References }}{{ .Field }} uint64, {{ end }}{{ end }}) (locale.ResourceTranslationSet, error) { +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 diff --git a/pkg/report/frame.go b/pkg/report/frame.go index 858681a39..388be7333 100644 --- a/pkg/report/frame.go +++ b/pkg/report/frame.go @@ -51,7 +51,7 @@ type ( Unique bool `json:"unique"` System bool `json:"system"` - Caster frameCellCaster `json:"-"` + Caster frameCellCaster `json:"-" yaml:"-"` } FrameDefinitionSet []*FrameDefinition diff --git a/system/service/locale.gen.go b/system/service/locale.gen.go new file mode 100644 index 000000000..8962ecd2e --- /dev/null +++ b/system/service/locale.gen.go @@ -0,0 +1,148 @@ +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: +// - system.report.yaml + +import ( + "context" + "github.com/cortezaproject/corteza-server/system/types" + + "github.com/cortezaproject/corteza-server/pkg/actionlog" + intAuth "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 ( + resourceTranslationsManager struct { + actionlog actionlog.Recorder + locale locale.Resource + store store.Storer + ac localeAccessController + } + + localeAccessController interface { + // CanManageResourceTranslation(context.Context) bool + } + + ResourceTranslationsManagerService interface { + Report(ctx context.Context, ID uint64) (locale.ResourceTranslationSet, error) + + Upsert(context.Context, locale.ResourceTranslationSet) error + Locale() locale.Resource + } +) + +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) { + // @todo AC + //if (!svc.ac.CanManageResourceTranslation(ctx)) { + // return *****ErrNotAllowedToCreate() + //} + + // @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 +} + +func (svc resourceTranslationsManager) Report(ctx context.Context, ID uint64) (locale.ResourceTranslationSet, error) { + var ( + err error + out locale.ResourceTranslationSet + res *types.Report + k types.LocaleKey + ) + + res, err = svc.loadReport(ctx, svc.store, ID) + if err != nil { + return nil, err + } + + for _, tag := range svc.locale.Tags() { + k = types.LocaleKeyReportName + 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.LocaleKeyReportDescription + out = append(out, &locale.ResourceTranslation{ + Resource: res.ResourceTranslation(), + Lang: tag.String(), + Key: k.Path, + Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), k.Path), + }) + + } + + tmp, err := svc.reportExtended(ctx, res) + return append(out, tmp...), err +} diff --git a/system/service/locale.go b/system/service/locale.go new file mode 100644 index 000000000..40999fe05 --- /dev/null +++ b/system/service/locale.go @@ -0,0 +1,49 @@ +package service + +import ( + "context" + "strconv" + "strings" + + "github.com/cortezaproject/corteza-server/pkg/locale" + "github.com/cortezaproject/corteza-server/store" + "github.com/cortezaproject/corteza-server/system/types" +) + +func (svc resourceTranslationsManager) loadReport(ctx context.Context, s store.Storer, reportID uint64) (*types.Report, error) { + return store.LookupReportByID(ctx, s, reportID) +} + +func (svc resourceTranslationsManager) reportExtended(ctx context.Context, res *types.Report) (out locale.ResourceTranslationSet, er error) { + var ( + k types.LocaleKey + ) + + for _, tag := range svc.locale.Tags() { + for i, projection := range res.Projections { + projectionID := locale.ContentID(projection.ProjectionID, i) + rpl := strings.NewReplacer( + "{{projectionID}}", strconv.FormatUint(projectionID, 10), + ) + + // base stuff + k = types.LocaleKeyReportProjectionTitle + out = append(out, &locale.ResourceTranslation{ + Resource: res.ResourceTranslation(), + Lang: tag.String(), + Key: rpl.Replace(k.Path), + Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), rpl.Replace(k.Path)), + }) + + k = types.LocaleKeyReportProjectionDescription + out = append(out, &locale.ResourceTranslation{ + Resource: res.ResourceTranslation(), + Lang: tag.String(), + Key: rpl.Replace(k.Path), + Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), rpl.Replace(k.Path)), + }) + } + } + + return +} diff --git a/system/types/locale.gen.go b/system/types/locale.gen.go new file mode 100644 index 000000000..377919c97 --- /dev/null +++ b/system/types/locale.gen.go @@ -0,0 +1,113 @@ +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: +// - system.report.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 ( + ReportResourceTranslationType = "system:report" +) + +var ( + LocaleKeyReportName = LocaleKey{ + Name: "name", + Resource: ReportResourceTranslationType, + Path: "name", + } + LocaleKeyReportDescription = LocaleKey{ + Name: "description", + Resource: ReportResourceTranslationType, + Path: "description", + } + LocaleKeyReportProjectionTitle = LocaleKey{ + Name: "projection title", + Resource: ReportResourceTranslationType, + Path: "projection.{{projectionID}}.title", + } + LocaleKeyReportProjectionDescription = LocaleKey{ + Name: "projection description", + Resource: ReportResourceTranslationType, + Path: "projection.{{projectionID}}.description", + } +) + +// ResourceTranslation returns string representation of Locale resource for Report by calling ReportResourceTranslation fn +// +// Locale resource is in the system:report/... format +// +// This function is auto-generated +func (r Report) ResourceTranslation() string { + return ReportResourceTranslation(r.ID) +} + +// ReportResourceTranslation returns string representation of Locale resource for Report +// +// Locale resource is in the system:report/... format +// +// This function is auto-generated +func ReportResourceTranslation(id uint64) string { + cpts := []interface{}{ReportResourceTranslationType} + cpts = append(cpts, strconv.FormatUint(id, 10)) + + return fmt.Sprintf(ReportResourceTranslationTpl(), cpts...) +} + +// @todo template +func ReportResourceTranslationTpl() string { + return "%s/%s" +} + +func (r *Report) DecodeTranslations(tt locale.ResourceTranslationIndex) { + var aux *locale.ResourceTranslation + if aux = tt.FindByKey(LocaleKeyReportName.Path); aux != nil { + r.Meta.Name = aux.Msg + } + if aux = tt.FindByKey(LocaleKeyReportDescription.Path); aux != nil { + r.Meta.Description = aux.Msg + } + + r.decodeTranslations(tt) +} + +func (r *Report) EncodeTranslations() (out locale.ResourceTranslationSet) { + out = locale.ResourceTranslationSet{} + if r.Meta.Name != "" { + out = append(out, &locale.ResourceTranslation{ + Resource: r.ResourceTranslation(), + Key: LocaleKeyReportName.Path, + Msg: r.Meta.Name, + }) + } + if r.Meta.Description != "" { + out = append(out, &locale.ResourceTranslation{ + Resource: r.ResourceTranslation(), + Key: LocaleKeyReportDescription.Path, + Msg: r.Meta.Description, + }) + } + + out = append(out, r.encodeTranslations()...) + + return out +} diff --git a/system/types/report.go b/system/types/report.go index 724dfc426..5df8a7ff9 100644 --- a/system/types/report.go +++ b/system/types/report.go @@ -4,9 +4,12 @@ import ( "database/sql/driver" "encoding/json" "fmt" + "strconv" + "strings" "time" "github.com/cortezaproject/corteza-server/pkg/filter" + "github.com/cortezaproject/corteza-server/pkg/locale" "github.com/cortezaproject/corteza-server/pkg/report" ) @@ -43,15 +46,16 @@ type ( } ReportProjection struct { - Title string `json:"title"` - Description string `json:"description"` - Key string `json:"key"` - Kind string `json:"kind"` - Options map[string]interface{} `json:"options,omitempty"` - Elements []interface{} `json:"elements"` - Sources report.StepDefinitionSet `json:"sources"` - XYWH [4]int `json:"xywh"` - Layout string `json:"layout"` + ProjectionID uint64 `json:"projectionID"` + Title string `json:"title"` + Description string `json:"description"` + Key string `json:"key"` + Kind string `json:"kind"` + Options map[string]interface{} `json:"options,omitempty"` + Elements []interface{} `json:"elements"` + Sources report.StepDefinitionSet `json:"sources"` + XYWH [4]int `json:"xywh"` + Layout string `json:"layout"` } ReportProjectionSet []*ReportProjection @@ -153,3 +157,49 @@ func (vv *ReportDataSourceSet) Scan(value interface{}) error { return nil } + +func (r *Report) decodeTranslations(tt locale.ResourceTranslationIndex) { + var aux *locale.ResourceTranslation + + for i, p := range r.Projections { + projectionID := locale.ContentID(p.ProjectionID, i) + rpl := strings.NewReplacer( + "{{projectionID}}", strconv.FormatUint(projectionID, 10), + ) + + // - generic page block stuff + if aux = tt.FindByKey(rpl.Replace(LocaleKeyReportProjectionTitle.Path)); aux != nil { + p.Title = aux.Msg + } + if aux = tt.FindByKey(rpl.Replace(LocaleKeyReportProjectionDescription.Path)); aux != nil { + p.Description = aux.Msg + } + } +} + +func (r *Report) encodeTranslations() (out locale.ResourceTranslationSet) { + out = make(locale.ResourceTranslationSet, 0, 3) + + // Page blocks + for i, projection := range r.Projections { + projectionID := locale.ContentID(projection.ProjectionID, i) + rpl := strings.NewReplacer( + "{{projectionID}}", strconv.FormatUint(projectionID, 10), + ) + + // - generic page block stuff + out = append(out, &locale.ResourceTranslation{ + Resource: r.ResourceTranslation(), + Key: rpl.Replace(rpl.Replace(LocaleKeyReportProjectionTitle.Path)), + Msg: projection.Title, + }) + + out = append(out, &locale.ResourceTranslation{ + Resource: r.ResourceTranslation(), + Key: rpl.Replace(rpl.Replace(LocaleKeyReportProjectionDescription.Path)), + Msg: projection.Description, + }) + } + + return +}