From 40ab1e22ea6ddfb29279939e8a56add7f9c1eacb Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Wed, 18 Sep 2019 15:19:25 +0200 Subject: [PATCH] Make structs for page block & chart config --- compose/rest/chart.go | 20 +++++++++----- compose/rest/page.go | 10 +++++-- compose/types/chart.go | 48 ++++++++++++++++++++++++++++---- compose/types/page.go | 63 ++++++++++++++++++++++++++++++++++-------- 4 files changed, 115 insertions(+), 26 deletions(-) diff --git a/compose/rest/chart.go b/compose/rest/chart.go index 66026bdec..d2b7ebd05 100644 --- a/compose/rest/chart.go +++ b/compose/rest/chart.go @@ -66,7 +66,10 @@ func (ctrl Chart) Create(ctx context.Context, r *request.ChartCreate) (interface mod := &types.Chart{ NamespaceID: r.NamespaceID, Name: r.Name, - Config: r.Config, + } + + if err = r.Config.Unmarshal(&mod.Config); err != nil { + return nil, err } mod, err = ctrl.chart.With(ctx).Create(mod) @@ -81,15 +84,18 @@ func (ctrl Chart) Read(ctx context.Context, r *request.ChartRead) (interface{}, func (ctrl Chart) Update(ctx context.Context, r *request.ChartUpdate) (interface{}, error) { var ( - mod = &types.Chart{} err error + mod = &types.Chart{ + ID: r.ChartID, + Name: r.Name, + NamespaceID: r.NamespaceID, + UpdatedAt: r.UpdatedAt, + } ) - mod.ID = r.ChartID - mod.Name = r.Name - mod.Config = r.Config - mod.NamespaceID = r.NamespaceID - mod.UpdatedAt = r.UpdatedAt + if err = r.Config.Unmarshal(&mod.Config); err != nil { + return nil, err + } mod, err = ctrl.chart.With(ctx).Update(mod) return ctrl.makePayload(ctx, mod, err) diff --git a/compose/rest/page.go b/compose/rest/page.go index 8aa732014..e4a9bad83 100644 --- a/compose/rest/page.go +++ b/compose/rest/page.go @@ -77,11 +77,14 @@ func (ctrl *Page) Create(ctx context.Context, r *request.PageCreate) (interface{ ModuleID: r.ModuleID, Title: r.Title, Description: r.Description, - Blocks: r.Blocks, Visible: r.Visible, } ) + if err = r.Blocks.Unmarshal(&mod.Blocks); err != nil { + return nil, err + } + mod, err = ctrl.page.With(ctx).Create(mod) return ctrl.makePayload(ctx, mod, err) } @@ -106,11 +109,14 @@ func (ctrl *Page) Update(ctx context.Context, r *request.PageUpdate) (interface{ ModuleID: r.ModuleID, Title: r.Title, Description: r.Description, - Blocks: r.Blocks, Visible: r.Visible, } ) + if err = r.Blocks.Unmarshal(&mod.Blocks); err != nil { + return nil, err + } + mod, err = ctrl.page.With(ctx).Update(mod) return ctrl.makePayload(ctx, mod, err) } diff --git a/compose/types/chart.go b/compose/types/chart.go index 3638aaf55..582625ce2 100644 --- a/compose/types/chart.go +++ b/compose/types/chart.go @@ -1,18 +1,22 @@ package types import ( + "database/sql/driver" + "encoding/json" "time" + "github.com/davecgh/go-spew/spew" + "github.com/pkg/errors" + "github.com/cortezaproject/corteza-server/internal/permissions" - "github.com/jmoiron/sqlx/types" ) type ( Chart struct { - ID uint64 `json:"chartID,string" db:"id"` - Handle string `json:"handle" db:"handle"` - Name string `json:"name" db:"name"` - Config types.JSONText `json:"config" db:"config"` + ID uint64 `json:"chartID,string" db:"id"` + Handle string `json:"handle" db:"handle"` + Name string `json:"name" db:"name"` + Config ChartConfig `json:"config" db:"config"` NamespaceID uint64 `json:"namespaceID,string" db:"rel_namespace,string"` @@ -21,6 +25,20 @@ type ( DeletedAt *time.Time `db:"deleted_at" json:"deletedAt,omitempty"` } + ChartConfig struct { + Reports []*ChartConfigReport `json:"reports,omitempty" yaml:",omitempty"` + } + + ChartConfigReport struct { + Filter string `json:"filter" yaml:",omitempty"` + ModuleID uint64 `json:"moduleID,string" yaml:"moduleID"` + Metrics []map[string]interface{} `json:"metrics,omitempty" yaml:",omitempty"` + Dimensions []map[string]interface{} `json:"dimensions,omitempty" yaml:",omitempty"` + Renderer struct { + Version string `json:"version,omitempty" yaml:",omitempty"` + } `json:"renderer,omitempty" yaml:",omitempty"` + } + ChartFilter struct { NamespaceID uint64 `json:"namespaceID,string"` Query string `json:"query"` @@ -46,3 +64,23 @@ func (set ChartSet) FindByHandle(handle string) *Chart { return nil } + +func (cc *ChartConfig) Scan(value interface{}) error { + //lint:ignore S1034 This typecast is intentional, we need to get []byte out of a []uint8 + switch value.(type) { + case nil: + *cc = ChartConfig{} + case []uint8: + b := value.([]byte) + spew.Dump(string(b)) + if err := json.Unmarshal(b, cc); err != nil { + return errors.Wrapf(err, "Can not scan '%v' into ChartConfig", string(b)) + } + } + + return nil +} + +func (cc ChartConfig) Value() (driver.Value, error) { + return json.Marshal(cc) +} diff --git a/compose/types/page.go b/compose/types/page.go index cfc44f3c4..5f00b26d1 100644 --- a/compose/types/page.go +++ b/compose/types/page.go @@ -1,10 +1,13 @@ package types import ( + "database/sql/driver" + "encoding/json" "time" + "github.com/pkg/errors" + "github.com/cortezaproject/corteza-server/internal/permissions" - "github.com/jmoiron/sqlx/types" ) type ( @@ -21,7 +24,7 @@ type ( Title string `json:"title" db:"title"` Description string `json:"description" db:"description"` - Blocks types.JSONText `json:"blocks" db:"blocks"` + Blocks PageBlocks `json:"blocks" db:"blocks"` Children PageSet `json:"children,omitempty" db:"-"` @@ -33,16 +36,22 @@ type ( DeletedAt *time.Time `db:"deleted_at" json:"deletedAt,omitempty"` } - // Block - value of Page.Blocks ([]Block) - Block struct { - Title string `json:"title"` - Description string `json:"description"` - Options types.JSONText `json:"options"` - Kind string `json:"kind"` - X int `json:"x"` - Y int `json:"y"` - Width int `json:"width"` - Height int `json:"height"` + PageBlocks []PageBlock + + PageBlock struct { + Title string `json:"title,omitempty" yaml:",omitempty"` + Description string `json:"description,omitempty" yaml:",omitempty"` + Options map[string]interface{} `json:"options,omitempty" yaml:",omitempty"` + Style PageBlockStyle `json:"style,omitempty" yaml:",omitempty"` + Kind string `json:"kind"` + X int `json:"x"` + Y int `json:"y"` + Width int `json:"width"` + Height int `json:"height"` + } + + PageBlockStyle struct { + Variants map[string]string `json:"variants,omitempty" yaml:",omitempty,flow"` } PageFilter struct { @@ -71,3 +80,33 @@ func (set PageSet) FindByHandle(handle string) *Page { return nil } + +func (bb *PageBlocks) Scan(value interface{}) error { + //lint:ignore S1034 This typecast is intentional, we need to get []byte out of a []uint8 + switch value.(type) { + case nil: + *bb = PageBlocks{} + case []uint8: + b := value.([]byte) + if err := json.Unmarshal(b, bb); err != nil { + return errors.Wrapf(err, "Can not scan '%v' into PageBlocks", string(b)) + } + } + + return nil +} + +func (bb PageBlocks) Value() (driver.Value, error) { + return json.Marshal(bb) +} + +func (set PageSet) FindByParent(parentID uint64) (out PageSet) { + out = PageSet{} + for i := range set { + if set[i].SelfID == parentID { + out = append(out, set[i]) + } + } + + return +}