From 64d57809832ee2f32378871c2e24403a864eb14b Mon Sep 17 00:00:00 2001 From: Mumbi Francis Date: Tue, 26 Mar 2024 15:07:08 +0300 Subject: [PATCH] Address BE review changes --- server/compose/envoy/store_decode.go | 16 +-- server/compose/envoy/yaml_decode.go | 107 ++++++++----------- server/compose/envoy/yaml_encode.go | 66 ++++++------ server/compose/namespace.cue | 2 +- server/compose/types/namespace.go | 27 ++++- server/store/adapters/rdbms/aux_types.gen.go | 24 ++--- server/store/adapters/rdbms/upgrade_fixes.go | 2 +- 7 files changed, 124 insertions(+), 120 deletions(-) diff --git a/server/compose/envoy/store_decode.go b/server/compose/envoy/store_decode.go index 7251ab2e2f..39dc87db1f 100644 --- a/server/compose/envoy/store_decode.go +++ b/server/compose/envoy/store_decode.go @@ -163,28 +163,28 @@ func decodePageRefs(p *types.Page) (refs map[string]envoyx.Ref) { for index, b := range p.Blocks { switch b.Kind { case "RecordList": - refs = envoyx.MergeRefs(refs, getPageBlockRecordListRefs(b, index)) + refs = envoyx.MergeRefs(refs, getPageBlockRecordListRefs(b.Options, index)) case "Automation": - refs = envoyx.MergeRefs(refs, getPageBlockAutomationRefs(b, index)) + refs = envoyx.MergeRefs(refs, getPageBlockAutomationRefs(b.Options, index)) case "RecordOrganizer": - refs = envoyx.MergeRefs(refs, getPageBlockRecordOrganizerRefs(b, index)) + refs = envoyx.MergeRefs(refs, getPageBlockRecordOrganizerRefs(b.Options, index)) case "Chart": - refs = envoyx.MergeRefs(refs, getPageBlockChartRefs(b, index)) + refs = envoyx.MergeRefs(refs, getPageBlockChartRefs(b.Options, index)) case "Calendar": - refs = envoyx.MergeRefs(refs, getPageBlockCalendarRefs(b, index)) + refs = envoyx.MergeRefs(refs, getPageBlockCalendarRefs(b.Options, index)) case "Metric": - refs = envoyx.MergeRefs(refs, getPageBlockMetricRefs(b, index)) + refs = envoyx.MergeRefs(refs, getPageBlockMetricRefs(b.Options, index)) case "Comment": - refs = envoyx.MergeRefs(refs, getPageBlockCommentRefs(b, index)) + refs = envoyx.MergeRefs(refs, getPageBlockCommentRefs(b.Options, index)) case "Progress": - refs = envoyx.MergeRefs(refs, getPageBlockProgressRefs(b, index)) + refs = envoyx.MergeRefs(refs, getPageBlockProgressRefs(b.Options, index)) } } diff --git a/server/compose/envoy/yaml_decode.go b/server/compose/envoy/yaml_decode.go index 542ef8b9a9..ac5a3da8c1 100644 --- a/server/compose/envoy/yaml_decode.go +++ b/server/compose/envoy/yaml_decode.go @@ -73,74 +73,55 @@ func (d *auxYamlDoc) unmarshalPageBlocksNode(r *types.Page, n *yaml.Node) (refs refs = map[string]envoyx.Ref{} for index, b := range r.Blocks { - switch b.Kind { - case "RecordList": - refs = envoyx.MergeRefs(refs, getPageBlockRecordListRefs(b, index)) - - case "Automation": - refs = envoyx.MergeRefs(refs, getPageBlockAutomationRefs(b, index)) - - case "RecordOrganizer": - refs = envoyx.MergeRefs(refs, getPageBlockRecordOrganizerRefs(b, index)) - - case "Chart": - refs = envoyx.MergeRefs(refs, getPageBlockChartRefs(b, index)) - - case "Calendar": - refs = envoyx.MergeRefs(refs, getPageBlockCalendarRefs(b, index)) - - case "Metric": - refs = envoyx.MergeRefs(refs, getPageBlockMetricRefs(b, index)) - - case "Comment": - refs = envoyx.MergeRefs(refs, getPageBlockCommentRefs(b, index)) - - case "Progress": - refs = envoyx.MergeRefs(refs, getPageBlockProgressRefs(b, index)) - } + idents, err = unmarshalResourceBlocksNode(b.Kind, b.Options, index, refs) } - return } func (d *auxYamlDoc) unmarshalNamespaceBlocksNode(r *types.Namespace, n *yaml.Node) (refs map[string]envoyx.Ref, idents envoyx.Identifiers, err error) { refs = map[string]envoyx.Ref{} - for index, b := range r.Blocks { - switch b.Kind { - case "RecordList": - refs = envoyx.MergeRefs(refs, getPageBlockRecordListRefs(b, index)) + for index, gBlock := range r.Blocks { + idents, err = unmarshalResourceBlocksNode(gBlock.Kind, gBlock.Options, index, refs) + } + + return +} - case "Automation": - refs = envoyx.MergeRefs(refs, getPageBlockAutomationRefs(b, index)) +func unmarshalResourceBlocksNode(kind string, options map[string]interface{}, index int, refs map[string]envoyx.Ref) (idents envoyx.Identifiers, err error) { + switch kind { + case "RecordList": + refs = envoyx.MergeRefs(refs, getPageBlockRecordListRefs(options, index)) - case "RecordOrganizer": - refs = envoyx.MergeRefs(refs, getPageBlockRecordOrganizerRefs(b, index)) + case "Automation": + refs = envoyx.MergeRefs(refs, getPageBlockAutomationRefs(options, index)) - case "Chart": - refs = envoyx.MergeRefs(refs, getPageBlockChartRefs(b, index)) + case "RecordOrganizer": + refs = envoyx.MergeRefs(refs, getPageBlockRecordOrganizerRefs(options, index)) - case "Calendar": - refs = envoyx.MergeRefs(refs, getPageBlockCalendarRefs(b, index)) + case "Chart": + refs = envoyx.MergeRefs(refs, getPageBlockChartRefs(options, index)) - case "Metric": - refs = envoyx.MergeRefs(refs, getPageBlockMetricRefs(b, index)) + case "Calendar": + refs = envoyx.MergeRefs(refs, getPageBlockCalendarRefs(options, index)) - case "Comment": - refs = envoyx.MergeRefs(refs, getPageBlockCommentRefs(b, index)) + case "Metric": + refs = envoyx.MergeRefs(refs, getPageBlockMetricRefs(options, index)) - case "Progress": - refs = envoyx.MergeRefs(refs, getPageBlockProgressRefs(b, index)) - } + case "Comment": + refs = envoyx.MergeRefs(refs, getPageBlockCommentRefs(options, index)) + + case "Progress": + refs = envoyx.MergeRefs(refs, getPageBlockProgressRefs(options, index)) } return } -func getPageBlockRecordListRefs(b types.PageBlock, index int) (refs map[string]envoyx.Ref) { +func getPageBlockRecordListRefs(options map[string]interface{}, index int) (refs map[string]envoyx.Ref) { refs = make(map[string]envoyx.Ref) - id := optString(b.Options, "module", "moduleID") + id := optString(options, "module", "moduleID") if id == "" || id == "0" { return } @@ -153,10 +134,10 @@ func getPageBlockRecordListRefs(b types.PageBlock, index int) (refs map[string]e return } -func getPageBlockChartRefs(b types.PageBlock, index int) (refs map[string]envoyx.Ref) { +func getPageBlockChartRefs(options map[string]interface{}, index int) (refs map[string]envoyx.Ref) { refs = make(map[string]envoyx.Ref) - id := optString(b.Options, "chart", "chartID") + id := optString(options, "chart", "chartID") if id == "" || id == "0" { return } @@ -169,10 +150,10 @@ func getPageBlockChartRefs(b types.PageBlock, index int) (refs map[string]envoyx return } -func getPageBlockCalendarRefs(b types.PageBlock, index int) (refs map[string]envoyx.Ref) { +func getPageBlockCalendarRefs(options map[string]interface{}, index int) (refs map[string]envoyx.Ref) { refs = make(map[string]envoyx.Ref) - ff, _ := b.Options["feeds"].([]interface{}) + ff, _ := options["feeds"].([]interface{}) for j, f := range ff { feed, _ := f.(map[string]interface{}) opt, _ := (feed["options"]).(map[string]interface{}) @@ -191,10 +172,10 @@ func getPageBlockCalendarRefs(b types.PageBlock, index int) (refs map[string]env return } -func getPageBlockMetricRefs(b types.PageBlock, index int) (refs map[string]envoyx.Ref) { +func getPageBlockMetricRefs(options map[string]interface{}, index int) (refs map[string]envoyx.Ref) { refs = make(map[string]envoyx.Ref) - mm, _ := b.Options["metrics"].([]interface{}) + mm, _ := options["metrics"].([]interface{}) for j, m := range mm { mops, _ := m.(map[string]interface{}) @@ -212,26 +193,26 @@ func getPageBlockMetricRefs(b types.PageBlock, index int) (refs map[string]envoy return } -func getPageBlockCommentRefs(b types.PageBlock, index int) (refs map[string]envoyx.Ref) { +func getPageBlockCommentRefs(options map[string]interface{}, index int) (refs map[string]envoyx.Ref) { // Same difference - return getPageBlockRecordListRefs(b, index) + return getPageBlockRecordListRefs(options, index) } -func getPageBlockProgressRefs(b types.PageBlock, index int) (refs map[string]envoyx.Ref) { +func getPageBlockProgressRefs(options map[string]interface{}, index int) (refs map[string]envoyx.Ref) { refs = make(map[string]envoyx.Ref) var aux *envoyx.Ref - aux = getPageBlockProgressValueRefs(b.Options["minValue"]) + aux = getPageBlockProgressValueRefs(options["minValue"]) if aux != nil { refs[fmt.Sprintf("Blocks.%d.Options.minValue.ModuleID", index)] = *aux } - aux = getPageBlockProgressValueRefs(b.Options["maxValue"]) + aux = getPageBlockProgressValueRefs(options["maxValue"]) if aux != nil { refs[fmt.Sprintf("Blocks.%d.Options.maxValue.ModuleID", index)] = *aux } - aux = getPageBlockProgressValueRefs(b.Options["value"]) + aux = getPageBlockProgressValueRefs(options["value"]) if aux != nil { refs[fmt.Sprintf("Blocks.%d.Options.value.ModuleID", index)] = *aux } @@ -260,15 +241,15 @@ func getPageBlockProgressValueRefs(val any) (ref *envoyx.Ref) { } } -func getPageBlockRecordOrganizerRefs(b types.PageBlock, index int) (refs map[string]envoyx.Ref) { +func getPageBlockRecordOrganizerRefs(options map[string]interface{}, index int) (refs map[string]envoyx.Ref) { // Same difference - return getPageBlockRecordListRefs(b, index) + return getPageBlockRecordListRefs(options, index) } -func getPageBlockAutomationRefs(b types.PageBlock, index int) (refs map[string]envoyx.Ref) { +func getPageBlockAutomationRefs(options map[string]interface{}, index int) (refs map[string]envoyx.Ref) { refs = make(map[string]envoyx.Ref) - bb, _ := b.Options["buttons"].([]interface{}) + bb, _ := options["buttons"].([]interface{}) for buttonIx, b := range bb { button, _ := b.(map[string]interface{}) id := optString(button, "workflow", "workflowID") diff --git a/server/compose/envoy/yaml_encode.go b/server/compose/envoy/yaml_encode.go index 342aeca72e..d114d4de48 100644 --- a/server/compose/envoy/yaml_encode.go +++ b/server/compose/envoy/yaml_encode.go @@ -3,7 +3,6 @@ package envoy import ( "context" "fmt" - "github.com/cortezaproject/corteza/server/compose/types" "github.com/cortezaproject/corteza/server/pkg/envoyx" "github.com/cortezaproject/corteza/server/pkg/y7s" @@ -89,11 +88,11 @@ func (e YamlEncoder) encodeModuleFieldOptionsC(ctx context.Context, p envoyx.Enc } func (e YamlEncoder) encodePageBlocksC(ctx context.Context, p envoyx.EncodeParams, tt envoyx.Traverser, n *envoyx.Node, pg *types.Page, bb types.PageBlocks) (_ any, err error) { + var aux any out, _ := y7s.MakeSeq() - var aux any for i, b := range pg.Blocks { - aux, err = e.encodePageBlockC(ctx, p, tt, n, i, b) + aux, err = e.encodePageBlockC(ctx, p, tt, n, i, b.Kind, b.Options) if err != nil { return } @@ -107,12 +106,12 @@ func (e YamlEncoder) encodePageBlocksC(ctx context.Context, p envoyx.EncodeParam return out, nil } -func (e YamlEncoder) encodeNamespaceBlocksC(ctx context.Context, p envoyx.EncodeParams, tt envoyx.Traverser, n *envoyx.Node, pg *types.Namespace, bb types.PageBlocks) (_ any, err error) { +func (e YamlEncoder) encodeNamespaceBlocksC(ctx context.Context, p envoyx.EncodeParams, tt envoyx.Traverser, n *envoyx.Node, ns *types.Namespace, bb types.GlobalBlocks) (_ any, err error) { + var aux any out, _ := y7s.MakeSeq() - var aux any - for i, b := range pg.Blocks { - aux, err = e.encodePageBlockC(ctx, p, tt, n, i, b) + for i, gB := range ns.Blocks { + aux, err = e.encodePageBlockC(ctx, p, tt, n, i, gB.Kind, gB.Options) if err != nil { return } @@ -126,31 +125,30 @@ func (e YamlEncoder) encodeNamespaceBlocksC(ctx context.Context, p envoyx.Encode return out, nil } -func (e YamlEncoder) encodePageBlockC(ctx context.Context, p envoyx.EncodeParams, tt envoyx.Traverser, n *envoyx.Node, index int, b types.PageBlock) (_ any, err error) { - - switch b.Kind { +func (e YamlEncoder) encodePageBlockC(ctx context.Context, p envoyx.EncodeParams, tt envoyx.Traverser, n *envoyx.Node, index int, kind string, options map[string]interface{}) (_ any, err error) { + switch kind { case "RecordList": - b = e.cleanupPageblockRecordList(b) + options = e.cleanupPageblockRecordList(options) modRef := n.References[fmt.Sprintf("Blocks.%d.Options.ModuleID", index)] - b.Options["module"] = safeParentIdentifier(tt, n, modRef) - delete(b.Options, "moduleID") + options["module"] = safeParentIdentifier(tt, n, modRef) + delete(options, "moduleID") break case "RecordOrganizer": modRef := n.References[fmt.Sprintf("Blocks.%d.Options.ModuleID", index)] - b.Options["module"] = safeParentIdentifier(tt, n, modRef) - delete(b.Options, "moduleID") + options["module"] = safeParentIdentifier(tt, n, modRef) + delete(options, "moduleID") break case "Chart": chrRef := n.References[fmt.Sprintf("Blocks.%d.Options.ChartID", index)] - b.Options["chart"] = safeParentIdentifier(tt, n, chrRef) - delete(b.Options, "chartID") + options["chart"] = safeParentIdentifier(tt, n, chrRef) + delete(options, "chartID") break case "Calendar": - ff, _ := b.Options["feeds"].([]interface{}) + ff, _ := options["feeds"].([]interface{}) for i, f := range ff { feed, _ := f.(map[string]interface{}) fOpts, _ := (feed["options"]).(map[string]interface{}) @@ -162,7 +160,7 @@ func (e YamlEncoder) encodePageBlockC(ctx context.Context, p envoyx.EncodeParams break case "Automation": - bb, _ := b.Options["buttons"].([]interface{}) + bb, _ := options["buttons"].([]interface{}) for i, b := range bb { button, _ := b.(map[string]interface{}) if _, has := button["workflowID"]; !has { @@ -177,7 +175,7 @@ func (e YamlEncoder) encodePageBlockC(ctx context.Context, p envoyx.EncodeParams break case "Metric": - mm, _ := b.Options["metrics"].([]interface{}) + mm, _ := options["metrics"].([]interface{}) for i, m := range mm { modRef := n.References[fmt.Sprintf("Blocks.%d.Options.metrics.%d.ModuleID", index, i)] @@ -189,47 +187,47 @@ func (e YamlEncoder) encodePageBlockC(ctx context.Context, p envoyx.EncodeParams case "Comment": modRef := n.References[fmt.Sprintf("Blocks.%d.Options.ModuleID", index)] - b.Options["module"] = safeParentIdentifier(tt, n, modRef) - delete(b.Options, "moduleID") + options["module"] = safeParentIdentifier(tt, n, modRef) + delete(options, "moduleID") break case "Progress": - err = e.encodeProgressPageblockVal("minValue", index, n, tt, &b) + err = e.encodeProgressPageblockVal("minValue", index, n, tt, options) if err != nil { return } - err = e.encodeProgressPageblockVal("maxValue", index, n, tt, &b) + err = e.encodeProgressPageblockVal("maxValue", index, n, tt, options) if err != nil { return } - err = e.encodeProgressPageblockVal("value", index, n, tt, &b) + err = e.encodeProgressPageblockVal("value", index, n, tt, options) if err != nil { return } break } - return b, nil + return } -func (e YamlEncoder) encodeProgressPageblockVal(k string, index int, n *envoyx.Node, tt envoyx.Traverser, b *types.PageBlock) (err error) { - if reflect2.IsNil(b.Options[k]) { +func (e YamlEncoder) encodeProgressPageblockVal(k string, index int, n *envoyx.Node, tt envoyx.Traverser, options map[string]interface{}) (err error) { + if reflect2.IsNil(options[k]) { return } modRef := n.References[fmt.Sprintf("Blocks.%d.Options.%s.ModuleID", index, k)] - opt := b.Options[k].(map[string]any) + opt := options[k].(map[string]any) opt["moduleID"] = safeParentIdentifier(tt, n, modRef) delete(opt, "moduleID") return } -func (e YamlEncoder) cleanupPageblockRecordList(b types.PageBlock) (out types.PageBlock) { - out = b - rawFF, has := out.Options["fields"] +func (e YamlEncoder) cleanupPageblockRecordList(options map[string]interface{}) (out map[string]interface{}) { + out = options + rawFF, has := out["fields"] if !has { return } @@ -251,7 +249,7 @@ func (e YamlEncoder) cleanupPageblockRecordList(b types.PageBlock) (out types.Pa } } - out.Options["fields"] = retFF + out["fields"] = retFF - return b + return } diff --git a/server/compose/namespace.cue b/server/compose/namespace.cue index 9031da5819..f7f67517ba 100644 --- a/server/compose/namespace.cue +++ b/server/compose/namespace.cue @@ -28,7 +28,7 @@ namespace: { omitGetter: true } blocks: { - goType: "types.PageBlocks" + goType: "types.GlobalBlocks" dal: { type: "JSON", defaultEmptyObject: true } omitSetter: true omitGetter: true diff --git a/server/compose/types/namespace.go b/server/compose/types/namespace.go index 889098f072..7f8eb0bc92 100644 --- a/server/compose/types/namespace.go +++ b/server/compose/types/namespace.go @@ -15,7 +15,7 @@ type ( Slug string `json:"slug"` Enabled bool `json:"enabled"` Meta NamespaceMeta `json:"meta"` - Blocks PageBlocks `json:"blocks"` + Blocks GlobalBlocks `json:"blocks"` Labels map[string]string `json:"labels,omitempty"` @@ -29,6 +29,28 @@ type ( Name string `json:"name"` } + GlobalBlocks []GlobalBlock + + GlobalBlock struct { + BlockID uint64 `json:"blockID,string,omitempty"` + + Options map[string]interface{} `json:"options,omitempty"` + Style PageBlockStyle `json:"style,omitempty"` + Kind string `json:"kind"` + XYWH [4]int `json:"xywh"` // x,y,w,h + Meta map[string]any `json:"meta,omitempty"` + + // Warning: value of this field is now handled via resource-translation facility + // struct field is kept for the convenience for now since it allows us + // easy encoding/decoding of the outgoing/incoming values + Title string `json:"title,omitempty"` + + // Warning: value of this field is now handled via resource-translation facility + // struct field is kept for the convenience for now since it allows us + // easy encoding/decoding of the outgoing/incoming values + Description string `json:"description,omitempty"` + } + NamespaceFilter struct { NamespaceID []string `json:"namespaceID"` @@ -92,3 +114,6 @@ func (set NamespaceSet) FindByHandle(handle string) *Namespace { func (nm *NamespaceMeta) Scan(src any) error { return sql.ParseJSON(src, nm) } func (nm NamespaceMeta) Value() (driver.Value, error) { return json.Marshal(nm) } + +func (nm *GlobalBlocks) Scan(src any) error { return sql.ParseJSON(src, nm) } +func (nm GlobalBlocks) Value() (driver.Value, error) { return json.Marshal(nm) } diff --git a/server/store/adapters/rdbms/aux_types.gen.go b/server/store/adapters/rdbms/aux_types.gen.go index 9ec6335e9d..52ebaed538 100644 --- a/server/store/adapters/rdbms/aux_types.gen.go +++ b/server/store/adapters/rdbms/aux_types.gen.go @@ -7,17 +7,17 @@ package rdbms // import ( - automationType "github.com/cortezaproject/corteza/server/automation/types" - composeType "github.com/cortezaproject/corteza/server/compose/types" - discoveryType "github.com/cortezaproject/corteza/server/discovery/types" - federationType "github.com/cortezaproject/corteza/server/federation/types" - actionlogType "github.com/cortezaproject/corteza/server/pkg/actionlog" - "github.com/cortezaproject/corteza/server/pkg/expr" - flagType "github.com/cortezaproject/corteza/server/pkg/flag/types" - labelsType "github.com/cortezaproject/corteza/server/pkg/label/types" - rbacType "github.com/cortezaproject/corteza/server/pkg/rbac" - systemType "github.com/cortezaproject/corteza/server/system/types" - "time" + automationType "github.com/cortezaproject/corteza/server/automation/types" + composeType "github.com/cortezaproject/corteza/server/compose/types" + discoveryType "github.com/cortezaproject/corteza/server/discovery/types" + federationType "github.com/cortezaproject/corteza/server/federation/types" + actionlogType "github.com/cortezaproject/corteza/server/pkg/actionlog" + "github.com/cortezaproject/corteza/server/pkg/expr" + flagType "github.com/cortezaproject/corteza/server/pkg/flag/types" + labelsType "github.com/cortezaproject/corteza/server/pkg/label/types" + rbacType "github.com/cortezaproject/corteza/server/pkg/rbac" + systemType "github.com/cortezaproject/corteza/server/system/types" + "time" ) type ( @@ -279,7 +279,7 @@ type ( Slug string `db:"slug"` Enabled bool `db:"enabled"` Meta composeType.NamespaceMeta `db:"meta"` - Blocks composeType.PageBlocks `db:"blocks"` + Blocks composeType.GlobalBlocks `db:"blocks"` Name string `db:"name"` CreatedAt time.Time `db:"created_at"` UpdatedAt *time.Time `db:"updated_at"` diff --git a/server/store/adapters/rdbms/upgrade_fixes.go b/server/store/adapters/rdbms/upgrade_fixes.go index eedcdeeaf3..cc22319796 100644 --- a/server/store/adapters/rdbms/upgrade_fixes.go +++ b/server/store/adapters/rdbms/upgrade_fixes.go @@ -246,7 +246,7 @@ func fix_2023_03_00_migrateComposePageMeta(ctx context.Context, s *Store) (err e func fix_2024_03_00_migrateNamespacePageBlocks(ctx context.Context, s *Store) (err error) { return addColumn(ctx, s, "compose_namespace", - &dal.Attribute{Ident: "blocks", Type: &dal.TypeJSON{DefaultValue: "[{}]"}}, + &dal.Attribute{Ident: "blocks", Type: &dal.TypeJSON{DefaultValue: "[]"}}, ) }