3
0

Fix resource translation codegen and empty string handling

This commit is contained in:
Tomaž Jerman
2022-03-02 15:32:57 +01:00
parent ac6e7332ae
commit 631811929e
7 changed files with 184 additions and 85 deletions

View File

@@ -11,6 +11,7 @@ import (
"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/filter"
"github.com/cortezaproject/corteza-server/pkg/locale"
"github.com/cortezaproject/corteza-server/pkg/options"
"github.com/cortezaproject/corteza-server/store"
@@ -36,7 +37,7 @@ type (
ResourceTranslationsManagerService interface {
{{- range .resources }}
{{ .expIdent }}(ctx context.Context, {{ range .references }}{{ . }} uint64, {{ end }}) (locale.ResourceTranslationSet, error)
{{ .expIdent }}(ctx context.Context, {{ range .references }}{{ .param }} uint64, {{ end }}) (locale.ResourceTranslationSet, error)
{{- end }}
Upsert(context.Context, locale.ResourceTranslationSet) error
@@ -85,6 +86,7 @@ func (svc resourceTranslationsManager) Upsert(ctx context.Context, rr locale.Res
for res, rr := range localeByRes {
current, _, err := store.SearchResourceTranslations(ctx, svc.store, systemTypes.ResourceTranslationFilter{
Resource: res,
Deleted: filter.StateInclusive,
})
if err != nil {
return err
@@ -101,13 +103,27 @@ func (svc resourceTranslationsManager) Upsert(ctx context.Context, rr locale.Res
})
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...)
for _, diff := range current.Old(rr) {
old := diff[0]
new := diff[1]
// soft delete; restore old message
if new.Message == "" {
new.Message = old.Message
if new.DeletedAt == nil {
new.DeletedAt = now()
new.DeletedBy = me.Identity()
}
} else {
new.UpdatedAt = now()
new.UpdatedBy = me.Identity()
new.DeletedAt = nil
new.DeletedBy = 0
}
sysLocale = append(sysLocale, new)
}
}
err = store.UpsertResourceTranslation(ctx, svc.store, sysLocale...)
@@ -128,7 +144,7 @@ func (svc resourceTranslationsManager) Locale() locale.Resource {
{{- range .resources }}
func (svc resourceTranslationsManager) {{ .expIdent }}(ctx context.Context, {{ range .references }}{{ . }} uint64, {{ end }}) (locale.ResourceTranslationSet, error) {
func (svc resourceTranslationsManager) {{ .expIdent }}(ctx context.Context, {{ range .references }}{{ .param }} uint64, {{ end }}) (locale.ResourceTranslationSet, error) {
var (
err error
out locale.ResourceTranslationSet
@@ -136,7 +152,7 @@ func (svc resourceTranslationsManager) {{ .expIdent }}(ctx context.Context, {{ r
k types.LocaleKey
)
res, err = svc.load{{ .expIdent }}(ctx, svc.store, {{ range .references }}{{ . }}, {{ end }})
res, err = svc.load{{ .expIdent }}(ctx, svc.store, {{ range .references }}{{ .param }}, {{ end }})
if err != nil {
return nil, err
}

View File

@@ -100,13 +100,11 @@ func (r *{{ .expIdent }}) EncodeTranslations() (out locale.ResourceTranslationSe
out = append(out, r.{{ .encodeFunc }}()...)
{{- end}}
{{ else }}
if r.{{ .fieldPath }} != "" {
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: {{ .struct }}.Path,
Msg: locale.SanitizeMessage(r.{{ .fieldPath }}),
})
}
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: {{ .struct }}.Path,
Msg: locale.SanitizeMessage(r.{{ .fieldPath }}),
})
{{- end}}
{{- end}}

View File

@@ -13,6 +13,7 @@ platform: [...schema.#codegen] &
rbacAccessControl+
rbacTypes+
localeTypes+
localeService+
envoyRBAC+
options+
[] // placeholder

View File

@@ -12,6 +12,7 @@ import (
"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/filter"
"github.com/cortezaproject/corteza-server/pkg/locale"
"github.com/cortezaproject/corteza-server/pkg/options"
"github.com/cortezaproject/corteza-server/store"
@@ -36,9 +37,9 @@ type (
}
ResourceTranslationsManagerService interface {
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)
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)
Upsert(context.Context, locale.ResourceTranslationSet) error
Locale() locale.Resource
@@ -86,6 +87,7 @@ func (svc resourceTranslationsManager) Upsert(ctx context.Context, rr locale.Res
for res, rr := range localeByRes {
current, _, err := store.SearchResourceTranslations(ctx, svc.store, systemTypes.ResourceTranslationFilter{
Resource: res,
Deleted: filter.StateInclusive,
})
if err != nil {
return err
@@ -102,13 +104,27 @@ func (svc resourceTranslationsManager) Upsert(ctx context.Context, rr locale.Res
})
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...)
for _, diff := range current.Old(rr) {
old := diff[0]
new := diff[1]
// soft delete; restore old message
if new.Message == "" {
new.Message = old.Message
if new.DeletedAt == nil {
new.DeletedAt = now()
new.DeletedBy = me.Identity()
}
} else {
new.UpdatedAt = now()
new.UpdatedBy = me.Identity()
new.DeletedAt = nil
new.DeletedBy = 0
}
sysLocale = append(sysLocale, new)
}
}
err = store.UpsertResourceTranslation(ctx, svc.store, sysLocale...)
@@ -127,7 +143,7 @@ func (svc resourceTranslationsManager) Locale() locale.Resource {
return svc.locale
}
func (svc resourceTranslationsManager) 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
@@ -135,7 +151,7 @@ func (svc resourceTranslationsManager) Module(ctx context.Context, NamespaceID u
k types.LocaleKey
)
res, err = svc.loadModule(ctx, svc.store, NamespaceID, ID)
res, err = svc.loadModule(ctx, svc.store, namespaceID, id)
if err != nil {
return nil, err
}
@@ -154,7 +170,7 @@ func (svc resourceTranslationsManager) Module(ctx context.Context, NamespaceID u
return out, nil
}
func (svc resourceTranslationsManager) 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
@@ -162,7 +178,7 @@ func (svc resourceTranslationsManager) Namespace(ctx context.Context, ID uint64)
k types.LocaleKey
)
res, err = svc.loadNamespace(ctx, svc.store, ID)
res, err = svc.loadNamespace(ctx, svc.store, id)
if err != nil {
return nil, err
}
@@ -197,7 +213,7 @@ func (svc resourceTranslationsManager) Namespace(ctx context.Context, ID uint64)
return out, nil
}
func (svc resourceTranslationsManager) 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
@@ -205,7 +221,7 @@ func (svc resourceTranslationsManager) Page(ctx context.Context, NamespaceID uin
k types.LocaleKey
)
res, err = svc.loadPage(ctx, svc.store, NamespaceID, ID)
res, err = svc.loadPage(ctx, svc.store, namespaceID, id)
if err != nil {
return nil, err
}

View File

@@ -89,13 +89,11 @@ func (r *Module) DecodeTranslations(tt locale.ResourceTranslationIndex) {
func (r *Module) EncodeTranslations() (out locale.ResourceTranslationSet) {
out = locale.ResourceTranslationSet{}
if r.Name != "" {
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyModuleName.Path,
Msg: locale.SanitizeMessage(r.Name),
})
}
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyModuleName.Path,
Msg: locale.SanitizeMessage(r.Name),
})
return out
}
@@ -153,13 +151,11 @@ func (r *ModuleField) DecodeTranslations(tt locale.ResourceTranslationIndex) {
func (r *ModuleField) EncodeTranslations() (out locale.ResourceTranslationSet) {
out = locale.ResourceTranslationSet{}
if r.Label != "" {
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyModuleFieldLabel.Path,
Msg: locale.SanitizeMessage(r.Label),
})
}
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyModuleFieldLabel.Path,
Msg: locale.SanitizeMessage(r.Label),
})
out = append(out, r.encodeTranslationsMetaDescriptionView()...)
@@ -222,29 +218,23 @@ func (r *Namespace) DecodeTranslations(tt locale.ResourceTranslationIndex) {
func (r *Namespace) EncodeTranslations() (out locale.ResourceTranslationSet) {
out = locale.ResourceTranslationSet{}
if r.Name != "" {
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyNamespaceName.Path,
Msg: locale.SanitizeMessage(r.Name),
})
}
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyNamespaceName.Path,
Msg: locale.SanitizeMessage(r.Name),
})
if r.Meta.Subtitle != "" {
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyNamespaceMetaSubtitle.Path,
Msg: locale.SanitizeMessage(r.Meta.Subtitle),
})
}
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyNamespaceMetaSubtitle.Path,
Msg: locale.SanitizeMessage(r.Meta.Subtitle),
})
if r.Meta.Description != "" {
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyNamespaceMetaDescription.Path,
Msg: locale.SanitizeMessage(r.Meta.Description),
})
}
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyNamespaceMetaDescription.Path,
Msg: locale.SanitizeMessage(r.Meta.Description),
})
return out
}
@@ -294,21 +284,17 @@ func (r *Page) DecodeTranslations(tt locale.ResourceTranslationIndex) {
func (r *Page) EncodeTranslations() (out locale.ResourceTranslationSet) {
out = locale.ResourceTranslationSet{}
if r.Title != "" {
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyPageTitle.Path,
Msg: locale.SanitizeMessage(r.Title),
})
}
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyPageTitle.Path,
Msg: locale.SanitizeMessage(r.Title),
})
if r.Description != "" {
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyPageDescription.Path,
Msg: locale.SanitizeMessage(r.Description),
})
}
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyPageDescription.Path,
Msg: locale.SanitizeMessage(r.Description),
})
out = append(out, r.encodeTranslations()...)

View File

@@ -92,13 +92,16 @@ outer:
return
}
func (set ResourceTranslationSet) Old(bb locale.ResourceTranslationSet) (out ResourceTranslationSet) {
func (set ResourceTranslationSet) Old(bb locale.ResourceTranslationSet) (out [][2]*ResourceTranslation) {
for _, b := range bb {
for _, a := range set {
// It's not new
if a.Compare(b) {
a.Message = b.Msg
out = append(out, a)
aux := *a
aux.Message = b.Msg
// old, new
out = append(out, [2]*ResourceTranslation{a, &aux})
break
}
}

View File

@@ -0,0 +1,79 @@
package types
import (
"reflect"
"testing"
"github.com/cortezaproject/corteza-server/pkg/locale"
"golang.org/x/text/language"
)
func TestNew(t *testing.T) {
lang := Lang{Tag: language.English}
cases := []struct {
name string
existing ResourceTranslationSet
assert locale.ResourceTranslationSet
expect ResourceTranslationSet
}{{
name: "all empty",
existing: nil,
assert: nil,
expect: nil,
}, {
name: "no existing",
existing: nil,
assert: locale.ResourceTranslationSet{{Lang: "en", Key: "k1", Msg: "k1 trans"}},
expect: ResourceTranslationSet{{Lang: lang, K: "k1", Message: "k1 trans"}},
}, {
name: "determine diff",
existing: ResourceTranslationSet{{Lang: lang, K: "k1", Message: "k1 trans"}, {Lang: lang, K: "k2", Message: "k2 trans"}},
assert: locale.ResourceTranslationSet{{Lang: "en", Key: "k1", Msg: "k1 trans"}, {Lang: "en", Key: "k2", Msg: "k2 trans"}, {Lang: "en", Key: "k3", Msg: "k3 trans"}},
expect: ResourceTranslationSet{{Lang: lang, K: "k3", Message: "k3 trans"}},
}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
out := c.existing.New(c.assert)
if !reflect.DeepEqual(c.expect, out) {
t.Errorf("gotP = %v, want %v", out, c.expect)
}
})
}
}
func TestOld(t *testing.T) {
lang := Lang{Tag: language.English}
cases := []struct {
name string
existing ResourceTranslationSet
assert locale.ResourceTranslationSet
expect [][2]*ResourceTranslation
}{{
name: "all empty",
existing: nil,
assert: nil,
expect: nil,
}, {
name: "no existing",
existing: nil,
assert: locale.ResourceTranslationSet{{Lang: "en", Key: "k1", Msg: "k1 trans"}},
expect: nil,
}, {
name: "determine diff",
existing: ResourceTranslationSet{{Lang: lang, K: "k1", Message: "k1 trans"}, {Lang: lang, K: "k2", Message: "k2 trans"}},
assert: locale.ResourceTranslationSet{{Lang: "en", Key: "k1", Msg: "k1 trans (edit)"}, {Lang: "en", Key: "k2", Msg: "k2 trans (edit)"}, {Lang: "en", Key: "k3", Msg: "k3 trans (edit)"}},
expect: [][2]*ResourceTranslation{{{Lang: lang, K: "k1", Message: "k1 trans"}, {Lang: lang, K: "k1", Message: "k1 trans (edit)"}}, {{Lang: lang, K: "k2", Message: "k2 trans"}, {Lang: lang, K: "k2", Message: "k2 trans (edit)"}}},
}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
out := c.existing.Old(c.assert)
if !reflect.DeepEqual(c.expect, out) {
t.Errorf("gotP = %v, want %v", out, c.expect)
}
})
}
}