Add logic for resource translation import/export
This commit is contained in:
@@ -14,6 +14,8 @@ func (e StoreEncoder) prepare(ctx context.Context, p envoyx.EncodeParams, s stor
|
||||
switch rt {
|
||||
case rbac.RuleResourceType:
|
||||
return e.prepareRbacRule(ctx, p, s, nn)
|
||||
case types.ResourceTranslationResourceType:
|
||||
return e.prepareResourceTranslation(ctx, p, s, nn)
|
||||
}
|
||||
|
||||
return
|
||||
@@ -23,6 +25,8 @@ func (e StoreEncoder) encode(ctx context.Context, p envoyx.EncodeParams, s store
|
||||
switch rt {
|
||||
case rbac.RuleResourceType:
|
||||
return e.encodeRbacRules(ctx, p, s, nn, tree)
|
||||
case types.ResourceTranslationResourceType:
|
||||
return e.encodeResourceTranslations(ctx, p, s, nn, tree)
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package envoy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/envoyx"
|
||||
"github.com/cortezaproject/corteza/server/pkg/id"
|
||||
"github.com/cortezaproject/corteza/server/store"
|
||||
"github.com/cortezaproject/corteza/server/system/types"
|
||||
)
|
||||
|
||||
func (e StoreEncoder) prepareResourceTranslation(ctx context.Context, p envoyx.EncodeParams, s store.Storer, nn envoyx.NodeSet) (err error) {
|
||||
// @todo existing resource translations?
|
||||
for _, n := range nn {
|
||||
if n.Resource == nil {
|
||||
panic("unexpected state: cannot call prepareResourceTranslation with nodes without a defined Resource")
|
||||
}
|
||||
|
||||
res, ok := n.Resource.(*types.ResourceTranslation)
|
||||
if !ok {
|
||||
panic("unexpected resource type: node expecting type of ResourceTranslation")
|
||||
}
|
||||
|
||||
// Run expressions on the nodes
|
||||
err = e.runEvals(ctx, false, n)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
res.ID = id.Next()
|
||||
|
||||
// @todo merge conflicts if we do existing assertion
|
||||
|
||||
n.Resource = res
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// encodeResourceTranslations encodes a set of resource into the database
|
||||
func (e StoreEncoder) encodeResourceTranslations(ctx context.Context, p envoyx.EncodeParams, s store.Storer, nn envoyx.NodeSet, tree envoyx.Traverser) (err error) {
|
||||
for _, n := range nn {
|
||||
err = e.encodeResourceTranslation(ctx, p, s, n, tree)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// encodeResourceTranslation encodes the resource into the database
|
||||
func (e StoreEncoder) encodeResourceTranslation(ctx context.Context, p envoyx.EncodeParams, s store.Storer, n *envoyx.Node, tree envoyx.Traverser) (err error) {
|
||||
// Grab dependency references
|
||||
var auxID uint64
|
||||
for fieldLabel, ref := range n.References {
|
||||
rn := tree.ParentForRef(n, ref)
|
||||
if rn == nil {
|
||||
err = fmt.Errorf("missing node for ref %v", ref)
|
||||
return
|
||||
}
|
||||
|
||||
auxID = rn.Resource.GetID()
|
||||
if auxID == 0 {
|
||||
err = fmt.Errorf("related resource doesn't provide an ID")
|
||||
return
|
||||
}
|
||||
|
||||
err = n.Resource.SetValue(fieldLabel, 0, auxID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Flush to the DB
|
||||
err = store.UpsertResourceTranslation(ctx, s, n.Resource.(*types.ResourceTranslation))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Generated
+20
-4
@@ -3309,16 +3309,17 @@ func unmarshalFlatRBACNode(n *yaml.Node, acc rbac.Access) (out envoyx.NodeSet, e
|
||||
// // // // // // // // // // // // // // // // // // // // // // // // //
|
||||
|
||||
func unmarshalLocaleNode(n *yaml.Node) (out envoyx.NodeSet, err error) {
|
||||
return out, y7s.EachMap(n, func(lang, loc *yaml.Node) error {
|
||||
err = y7s.EachMap(n, func(lang, loc *yaml.Node) error {
|
||||
langTag := systemTypes.Lang{Tag: language.Make(lang.Value)}
|
||||
|
||||
return y7s.EachMap(loc, func(res, kv *yaml.Node) error {
|
||||
return y7s.EachMap(kv, func(k, msg *yaml.Node) error {
|
||||
out = append(out, &envoyx.Node{
|
||||
Resource: &systemTypes.ResourceTranslation{
|
||||
Lang: langTag,
|
||||
K: k.Value,
|
||||
Message: msg.Value,
|
||||
Resource: res.Value,
|
||||
Lang: langTag,
|
||||
K: k.Value,
|
||||
Message: msg.Value,
|
||||
},
|
||||
// Providing resource type as plain text to reduce cross component references
|
||||
ResourceType: "corteza::system:resource-translation",
|
||||
@@ -3328,6 +3329,21 @@ func unmarshalLocaleNode(n *yaml.Node) (out envoyx.NodeSet, err error) {
|
||||
})
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, o := range out {
|
||||
for _, r := range o.References {
|
||||
if r.Scope.IsEmpty() {
|
||||
continue
|
||||
}
|
||||
o.Scope = r.Scope
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// // // // // // // // // // // // // // // // // // // // // // // // //
|
||||
|
||||
@@ -31,6 +31,75 @@ func (e YamlEncoder) encode(ctx context.Context, base *yaml.Node, p envoyx.Encod
|
||||
switch rt {
|
||||
case rbac.RuleResourceType:
|
||||
return e.encodeRbacRules(ctx, base, p, nodes, tt)
|
||||
case types.ResourceTranslationResourceType:
|
||||
return e.encodeResourceTranslations(ctx, base, p, nodes, tt)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (e YamlEncoder) encodeResourceTranslations(ctx context.Context, base *yaml.Node, p envoyx.EncodeParams, nodes envoyx.NodeSet, tt envoyx.Traverser) (out *yaml.Node, err error) {
|
||||
err = e.resolveRulePathDeps(ctx, tt, nodes)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
byLang := make(map[string][]*envoyx.Node)
|
||||
|
||||
for _, n := range nodes {
|
||||
byLang[n.Resource.(*types.ResourceTranslation).Lang.String()] = append(byLang[n.Resource.(*types.ResourceTranslation).Lang.String()], n)
|
||||
}
|
||||
|
||||
out = base
|
||||
|
||||
var aux *yaml.Node
|
||||
for lang, nodes := range byLang {
|
||||
aux, err = e.encodeResourceTranslationsByResource(p, nodes, tt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
out, err = y7s.AddMap(out, lang, aux)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return y7s.MakeMap("locale", out)
|
||||
}
|
||||
|
||||
func (e YamlEncoder) encodeResourceTranslationsByResource(p envoyx.EncodeParams, nodes envoyx.NodeSet, tt envoyx.Traverser) (out *yaml.Node, err error) {
|
||||
byResource := make(map[string][]*envoyx.Node)
|
||||
|
||||
for _, n := range nodes {
|
||||
byResource[n.Resource.(*types.ResourceTranslation).Resource] = append(byResource[n.Resource.(*types.ResourceTranslation).Resource], n)
|
||||
}
|
||||
|
||||
var aux *yaml.Node
|
||||
for resource, nodes := range byResource {
|
||||
aux, err = e.encodeResourceTranslationsKv(p, nodes, tt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
out, err = y7s.AddMap(out, resource, aux)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (e YamlEncoder) encodeResourceTranslationsKv(p envoyx.EncodeParams, nodes envoyx.NodeSet, tt envoyx.Traverser) (out *yaml.Node, err error) {
|
||||
out, _ = y7s.MakeMap()
|
||||
|
||||
for _, n := range nodes {
|
||||
rt := n.Resource.(*types.ResourceTranslation)
|
||||
out, err = y7s.AddMap(out, rt.K, rt.Message)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
@@ -11,6 +11,8 @@ resource_translation: {
|
||||
}
|
||||
|
||||
model: {
|
||||
defaultSetter: true
|
||||
|
||||
// lengths for the lang, resource fields are now a bit shorter
|
||||
// Reason for that is supported index length in MySQL
|
||||
attributes: {
|
||||
|
||||
+3
@@ -530,6 +530,9 @@ func (r *ResourceTranslation) SetValue(name string, pos uint, value any) (err er
|
||||
case "updatedBy", "UpdatedBy":
|
||||
return cast2.Uint64(value, &r.UpdatedBy)
|
||||
|
||||
default:
|
||||
return r.setValue(name, pos, value)
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,9 +2,11 @@ package types
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/cast2"
|
||||
"github.com/cortezaproject/corteza/server/pkg/filter"
|
||||
"github.com/cortezaproject/corteza/server/pkg/locale"
|
||||
"golang.org/x/text/language"
|
||||
@@ -72,6 +74,30 @@ func (a *ResourceTranslation) Compare(b *locale.ResourceTranslation) bool {
|
||||
return strings.EqualFold(a.K, b.Key) && a.Lang.Tag.String() == b.Lang
|
||||
}
|
||||
|
||||
func (rt *ResourceTranslation) setValue(name string, pos uint, v any) (err error) {
|
||||
pp := strings.Split(name, ".")
|
||||
|
||||
switch pp[0] {
|
||||
case "resource", "Resource", "Path", "path":
|
||||
ix, err := strconv.ParseUint(pp[1], 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res := strings.Split(rt.Resource, "/")
|
||||
|
||||
aux := ""
|
||||
err = cast2.String(v, &aux)
|
||||
|
||||
// +1 bacause the first bit is the resource
|
||||
res[ix+1] = aux
|
||||
rt.Resource = strings.Join(res, "/")
|
||||
return err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (set ResourceTranslationSet) New(bb locale.ResourceTranslationSet) (out ResourceTranslationSet) {
|
||||
outer:
|
||||
for _, b := range bb {
|
||||
@@ -109,6 +135,24 @@ func (set ResourceTranslationSet) Old(bb locale.ResourceTranslationSet) (out [][
|
||||
return
|
||||
}
|
||||
|
||||
func (set ResourceTranslationSet) FilterLanguage(tag language.Tag) (out ResourceTranslationSet) {
|
||||
for _, a := range set {
|
||||
if a.Lang.Tag == tag {
|
||||
out = append(out, a)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (set ResourceTranslationSet) FilterKey(key string) (out ResourceTranslationSet) {
|
||||
for _, a := range set {
|
||||
if a.K == key {
|
||||
out = append(out, a)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func FromLocale(ll locale.ResourceTranslationSet) (out ResourceTranslationSet) {
|
||||
for _, l := range ll {
|
||||
out = append(out, &ResourceTranslation{
|
||||
|
||||
Reference in New Issue
Block a user