3
0

Extend codegen for system resources

* Rename generated services
* Add definitions for reports
This commit is contained in:
Tomaž Jerman
2021-09-17 12:43:27 +02:00
parent cf35691c56
commit 8cae39f231
19 changed files with 426 additions and 79 deletions
+9 -9
View File
@@ -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
+1 -1
View File
@@ -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)
}
+1 -1
View File
@@ -38,7 +38,7 @@ type (
Module struct {
module service.ModuleService
locale service.ResourceTranslationService
locale service.ResourceTranslationsManagerService
namespace service.NamespaceService
ac moduleAccessController
}
+1 -1
View File
@@ -32,7 +32,7 @@ type (
Namespace struct {
namespace service.NamespaceService
locale service.ResourceTranslationService
locale service.ResourceTranslationsManagerService
attachment service.AttachmentService
ac namespaceAccessController
}
+1 -1
View File
@@ -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
+12 -12
View File
@@ -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
+8 -8
View File
@@ -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
}
+1 -1
View File
@@ -27,7 +27,7 @@ type (
ac moduleAccessController
eventbus eventDispatcher
store store.Storer
locale ResourceTranslationService
locale ResourceTranslationsManagerService
}
moduleAccessController interface {
+1 -1
View File
@@ -24,7 +24,7 @@ type (
ac namespaceAccessController
eventbus eventDispatcher
store store.Storer
locale ResourceTranslationService
locale ResourceTranslationsManagerService
}
namespaceAccessController interface {
+1 -1
View File
@@ -22,7 +22,7 @@ type (
ac pageAccessController
eventbus eventDispatcher
store store.Storer
locale ResourceTranslationService
locale ResourceTranslationsManagerService
}
pageAccessController interface {
+2 -2
View File
@@ -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 {
+1 -22
View File
@@ -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,
+8
View File
@@ -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 }
@@ -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
+1 -1
View File
@@ -51,7 +51,7 @@ type (
Unique bool `json:"unique"`
System bool `json:"system"`
Caster frameCellCaster `json:"-"`
Caster frameCellCaster `json:"-" yaml:"-"`
}
FrameDefinitionSet []*FrameDefinition
+148
View File
@@ -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
}
+49
View File
@@ -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
}
+113
View File
@@ -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
}
+59 -9
View File
@@ -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
}