3
0

Tweak logic & add base import/export test

This commit is contained in:
Tomaž Jerman
2023-02-26 11:44:11 +01:00
parent 0bfc2a7f87
commit cf30aeb75d
48 changed files with 1651 additions and 326 deletions
@@ -190,17 +190,19 @@ func (d StoreDecoder) decode{{.expIdent}}(ctx context.Context, s store.Storer, d
{{- end }}
)
refs := map[string]envoyx.Ref{
// Handle references
// Omit any non-defined values
refs := map[string]envoyx.Ref{}
{{- range .model.attributes -}}
{{- if eq .dal.type "Ref" }}
// Handle references
"{{ .expIdent }}": envoyx.Ref{
ResourceType: "{{ .dal.refModelResType }}",
Identifiers: envoyx.MakeIdentifiers(r.{{.expIdent}}),
},
if r.{{.expIdent}} > 0 {
refs["{{ .expIdent }}"] = envoyx.Ref{
ResourceType: "{{ .dal.refModelResType }}",
Identifiers: envoyx.MakeIdentifiers(r.{{.expIdent}}),
}
}
{{- end }}
{{- end }}
}
{{ if .envoy.store.extendedRefDecoder }}
refs = envoyx.MergeRefs(refs, d.decode{{.expIdent}}Refs(r))
@@ -11,7 +11,6 @@ import (
"github.com/cortezaproject/corteza/server/pkg/expr"
"github.com/cortezaproject/corteza/server/pkg/id"
"github.com/cortezaproject/corteza/server/store"
{{- range .imports }}
"{{ . }}"
{{- end }}
@@ -221,6 +220,12 @@ func (e StoreEncoder) encode{{.expIdent}}(ctx context.Context, p envoyx.EncodePa
}
}
{{ if .envoy.store.sanitizeBeforeSave }}
// Custom resource sanitization before saving.
// This can be used to cleanup arbitrary config fields.
e.sanitize{{.expIdent}}BeforeSave(n.Resource.(*types.{{.expIdent}}))
{{ end }}
// Flush to the DB
err = store.Upsert{{.store.expIdent}}(ctx, s, n.Resource.(*types.{{.expIdent}}))
if err != nil {
@@ -13,6 +13,7 @@ import (
"github.com/cortezaproject/corteza/server/pkg/rbac"
"github.com/cortezaproject/corteza/server/pkg/y7s"
"golang.org/x/text/language"
"github.com/spf13/cast"
"gopkg.in/yaml.v3"
{{- range .imports }}
@@ -245,7 +246,7 @@ func (d *auxYamlDoc) unmarshal{{ .expIdent }}Node(dctx documentContext, n *yaml.
auxOut envoyx.NodeSet
nestedNodes envoyx.NodeSet
scope envoyx.Scope
envoyConfig envoyx.NodeConfig
envoyConfig envoyx.EnvoyConfig
{{- if .rbac }}
rbacNodes envoyx.NodeSet
{{- end }}
@@ -293,7 +294,7 @@ func (d *auxYamlDoc) unmarshal{{ .expIdent }}Node(dctx documentContext, n *yaml.
"{{ $l }}"{{if not (eq $i (sub (len $identKeys) 1))}},{{end}}
{{- end}}:
{{- if and $attr.envoy.yaml.identKeyAlias (gt (len $attr.envoy.yaml.identKeyAlias) 0) }}
{{- if and $attr.envoy.yaml.identKeyAlias (gt (len $attr.envoy.yaml.identKeyAlias) 0) (not (eq $attr.dal.type "Ref")) }}
// Handle field alias
//
// @todo consider adding an is empty check before overwriting
@@ -318,6 +319,12 @@ func (d *auxYamlDoc) unmarshal{{ .expIdent }}Node(dctx documentContext, n *yaml.
if err != nil {
return err
}
// Omit if not defined
tmp := cast.ToString(auxNodeValue)
if tmp == "0" || tmp == "" {
break
}
refs["{{ $attr.expIdent }}"] = envoyx.Ref{
ResourceType: "{{ $attr.dal.refModelResType }}",
Identifiers: envoyx.MakeIdentifiers(auxNodeValue),
@@ -513,6 +520,11 @@ func (d *auxYamlDoc) unmarshal{{ .expIdent }}Node(dctx documentContext, n *yaml.
}
for f, ref := range refs {
// Only inherit root references
// @todo improve; this is a hack
if strings.Contains(f, ".") {
continue
}
a.References[f] = ref
}
}
@@ -694,7 +706,7 @@ func unmarshalLocaleNode(n *yaml.Node) (out envoyx.NodeSet, err error) {
// Envoy config unmarshal logic
// // // // // // // // // // // // // // // // // // // // // // // // //
func (d *auxYamlDoc) decodeEnvoyConfig(n *yaml.Node) (out envoyx.NodeConfig) {
func (d *auxYamlDoc) decodeEnvoyConfig(n *yaml.Node) (out envoyx.EnvoyConfig) {
y7s.EachMap(n, func(k, v *yaml.Node) (err error) {
switch strings.ToLower(k.Value) {
case "skipif", "skip":
@@ -51,7 +51,7 @@ func (e YamlEncoder) Encode(ctx context.Context, p envoyx.EncodeParams, rt strin
switch rt {
{{- range .resources }}
{{- if .envoy.omit}}
{{- if or .envoy.omit .envoy.yaml.omitEncoder}}
{{continue}}
{{ end -}}
@@ -66,6 +66,10 @@ func (e YamlEncoder) Encode(ctx context.Context, p envoyx.EncodeParams, rt strin
return
}
{{ end -}}
default:
// When this encoder doesn't handle any node it shouldn't write anything;
// this just removes the need for an extra check at the end.
return
}
return yaml.NewEncoder(w).Encode(out)
@@ -169,7 +173,25 @@ func (e YamlEncoder) encode{{.expIdent}}(ctx context.Context, p envoyx.EncodePar
*/}}
{{ $p := (index $cmp.parents (sub (len $cmp.parents) 1)) }}
{{ if (eq $p.handle $a.ident) }}
{{ if eq $cmp.ident "moduleField"}}
// When processing module fields, we need to filter out the ones that
// don't belong to this module
//
// @todo offload this to dependency resolution; this is a hack
children := tt.ChildrenForResourceType(node, types.ModuleFieldResourceType)
var proc envoyx.NodeSet
selfRef := node.ToRef()
for _, c := range children {
if c.References["ModuleID"].Equals(selfRef) {
proc = append(proc, c)
}
}
aux, err = e.encodeModuleFields(ctx, p, proc, tt)
{{- else }}
aux, err = e.encode{{$cmp.expIdent}}s(ctx, p, tt.ChildrenForResourceType(node, types.{{$cmp.expIdent}}ResourceType), tt)
{{- end }}
if err != nil {
return
}
@@ -210,7 +232,7 @@ func (e YamlEncoder) encodeTimestamp(p envoyx.EncodeParams, t time.Time) (any, e
return nil, nil
}
tz := p.Config.PreferredTimezone
tz := p.Encoder.PreferredTimezone
if tz != "" {
tzL, err := time.LoadLocation(tz)
if err != nil {
@@ -219,7 +241,7 @@ func (e YamlEncoder) encodeTimestamp(p envoyx.EncodeParams, t time.Time) (any, e
t = t.In(tzL)
}
ly := p.Config.PreferredTimeLayout
ly := p.Encoder.PreferredTimeLayout
if ly == "" {
ly = time.RFC3339
}
@@ -243,7 +265,7 @@ func (e YamlEncoder) encodeRef(p envoyx.EncodeParams, id uint64, field string, n
return id, nil
}
return node.Identifiers.FriendlyIdentifier(), nil
return parent.Identifiers.FriendlyIdentifier(), nil
}
+4
View File
@@ -17,6 +17,10 @@ import (
fqrt: platform + "::" + handle
envoy: {
omit: bool | *false
}
// All known RBAC operations for this component
rbac: #rbacComponent & {
operations: {
+5
View File
@@ -177,6 +177,8 @@ import (
// referencing this resource
identKeys: [...string] | *([identKeyLabel]+identKeyAlias)
omitEncoder: bool | *false
extendedResourcePostProcess: bool | *false
extendedResourceDecoders: [...{
ident: string
@@ -215,5 +217,8 @@ import (
// extendedDecoder is called after the built-in which you can use
// to append additional nodes into.
extendedDecoder: bool | *false
// the resource calls a sanitizer function before saving to the database
sanitizeBeforeSave: bool | *false
}
}
+6 -6
View File
@@ -8,7 +8,7 @@ import (
[...schema.#codegen] &
[
for cmp in app.corteza.components {
for cmp in app.corteza.components if !cmp.envoy.omit {
template: "gocode/envoy/yaml_decode.go.tpl"
output: "\(cmp.ident)/envoy/yaml_decode.gen.go"
payload: {
@@ -22,7 +22,7 @@ import (
resources: [ for res in cmp.resources { res }]
}
},
for cmp in app.corteza.components {
for cmp in app.corteza.components if !cmp.envoy.omit {
template: "gocode/envoy/store_decode.go.tpl"
output: "\(cmp.ident)/envoy/store_decode.gen.go"
payload: {
@@ -36,7 +36,7 @@ import (
resources: [ for res in cmp.resources { res }]
}
},
for cmp in app.corteza.components {
for cmp in app.corteza.components if !cmp.envoy.omit {
template: "gocode/envoy/store_encode.go.tpl"
output: "\(cmp.ident)/envoy/store_encode.gen.go"
payload: {
@@ -50,7 +50,7 @@ import (
resources: [ for res in cmp.resources { res }]
}
},
for cmp in app.corteza.components {
for cmp in app.corteza.components if !cmp.envoy.omit {
template: "gocode/envoy/yaml_encode.go.tpl"
output: "\(cmp.ident)/envoy/yaml_encode.gen.go"
payload: {
@@ -72,7 +72,7 @@ import (
payload: {
package: "envoyx"
components: [for cmp in app.corteza.components {
components: [for cmp in app.corteza.components if !cmp.envoy.omit {
ident: cmp.ident,
resources: cmp.resources
}]
@@ -85,7 +85,7 @@ import (
payload: {
package: "envoyx"
components: [for cmp in app.corteza.components {
components: [for cmp in app.corteza.components if !cmp.envoy.omit {
ident: cmp.ident,
resources: cmp.resources
}]