From 8ad87c8824697cee944a87a0283a498865ec8a37 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Fri, 20 Sep 2019 07:33:32 +0200 Subject: [PATCH] Move to typed props for ns, fields, charts... --- compose/rest/namespace.go | 34 +++++++++++++++++++------------ compose/service/namespace.go | 3 --- compose/service/page.go | 10 +-------- compose/types/chart.go | 2 -- compose/types/module_field.go | 25 +++++++++++++++++++++-- compose/types/namespace.go | 38 +++++++++++++++++++++++++++++------ compose/types/page.go | 33 ++++++++++++++++++++++++++---- 7 files changed, 106 insertions(+), 39 deletions(-) diff --git a/compose/rest/namespace.go b/compose/rest/namespace.go index 857282973..c27ec7b8a 100644 --- a/compose/rest/namespace.go +++ b/compose/rest/namespace.go @@ -68,12 +68,17 @@ func (ctrl Namespace) List(ctx context.Context, r *request.NamespaceList) (inter } func (ctrl Namespace) Create(ctx context.Context, r *request.NamespaceCreate) (interface{}, error) { - var err error - ns := &types.Namespace{ - Name: r.Name, - Slug: r.Slug, - Meta: r.Meta, - Enabled: r.Enabled, + var ( + err error + ns = &types.Namespace{ + Name: r.Name, + Slug: r.Slug, + Enabled: r.Enabled, + } + ) + + if err = r.Meta.Unmarshal(&ns.Meta); err != nil { + return nil, err } ns, err = ctrl.namespace.With(ctx).Create(ns) @@ -87,16 +92,19 @@ func (ctrl Namespace) Read(ctx context.Context, r *request.NamespaceRead) (inter func (ctrl Namespace) Update(ctx context.Context, r *request.NamespaceUpdate) (interface{}, error) { var ( - ns = &types.Namespace{} err error + ns = &types.Namespace{ + ID: r.NamespaceID, + Name: r.Name, + Slug: r.Slug, + Enabled: r.Enabled, + UpdatedAt: r.UpdatedAt, + } ) - ns.ID = r.NamespaceID - ns.Name = r.Name - ns.Slug = r.Slug - ns.Meta = r.Meta - ns.Enabled = r.Enabled - ns.UpdatedAt = r.UpdatedAt + if err = r.Meta.Unmarshal(&ns.Meta); err != nil { + return nil, err + } ns, err = ctrl.namespace.With(ctx).Update(ns) return ctrl.makePayload(ctx, ns, err) diff --git a/compose/service/namespace.go b/compose/service/namespace.go index 0c8d286b9..c13acf4ce 100644 --- a/compose/service/namespace.go +++ b/compose/service/namespace.go @@ -3,7 +3,6 @@ package service import ( "context" - "github.com/davecgh/go-spew/spew" "github.com/titpetric/factory" "go.uber.org/zap" @@ -109,7 +108,6 @@ func (svc namespace) Find(filter types.NamespaceFilter) (set types.NamespaceSet, // Create adds namespace and presets access rules for role everyone func (svc namespace) Create(mod *types.Namespace) (*types.Namespace, error) { if !handle.IsValid(mod.Slug) { - spew.Dump(mod.Slug) return nil, ErrInvalidHandle } @@ -126,7 +124,6 @@ func (svc namespace) Update(mod *types.Namespace) (ns *types.Namespace, err erro } if !handle.IsValid(mod.Slug) { - spew.Dump(mod.Slug) return nil, ErrInvalidHandle } diff --git a/compose/service/page.go b/compose/service/page.go index 61011a6c6..054f513e0 100644 --- a/compose/service/page.go +++ b/compose/service/page.go @@ -3,7 +3,6 @@ package service import ( "context" - "github.com/davecgh/go-spew/spew" "github.com/titpetric/factory" "go.uber.org/zap" @@ -260,7 +259,6 @@ func (svc page) Update(mod *types.Page) (p *types.Page, err error) { p.Visible = mod.Visible p.Weight = mod.Weight - spew.Dump(p.Handle) if err = svc.UniqueCheck(p); err != nil { return } @@ -272,18 +270,12 @@ func (svc page) Update(mod *types.Page) (p *types.Page, err error) { func (svc page) UniqueCheck(p *types.Page) (err error) { if p.Handle != "" { if e, _ := svc.pageRepo.FindByHandle(p.NamespaceID, p.Handle); e != nil && e.ID != p.ID { - spew.Dump( - e.ID, - e.Handle, - p.ID, - p.Handle, - ) return repository.ErrPageHandleNotUnique } } if p.ModuleID > 0 { - if p, _ := svc.pageRepo.FindByModuleID(p.NamespaceID, p.ModuleID); p.ID > 0 && p.ID != p.ID { + if e, _ := svc.pageRepo.FindByModuleID(p.NamespaceID, p.ModuleID); e != nil && e.ID != e.ID { return ErrModulePageExists } } diff --git a/compose/types/chart.go b/compose/types/chart.go index aae60e46c..8a1ebfce6 100644 --- a/compose/types/chart.go +++ b/compose/types/chart.go @@ -5,7 +5,6 @@ import ( "encoding/json" "time" - "github.com/davecgh/go-spew/spew" "github.com/pkg/errors" "github.com/cortezaproject/corteza-server/internal/permissions" @@ -73,7 +72,6 @@ func (cc *ChartConfig) Scan(value interface{}) error { *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)) } diff --git a/compose/types/module_field.go b/compose/types/module_field.go index 10daccd5c..abb859233 100644 --- a/compose/types/module_field.go +++ b/compose/types/module_field.go @@ -6,7 +6,7 @@ import ( "sort" "time" - "github.com/jmoiron/sqlx/types" + "github.com/pkg/errors" "github.com/cortezaproject/corteza-server/internal/permissions" ) @@ -22,7 +22,7 @@ type ( Name string `json:"name" db:"name"` Label string `json:"label" db:"label"` - Options types.JSONText `json:"options" db:"options"` + Options ModuleFieldOptions `json:"options" db:"options"` Private bool `json:"isPrivate" db:"is_private"` Required bool `json:"isRequired" db:"is_required"` @@ -34,12 +34,33 @@ type ( UpdatedAt *time.Time `db:"updated_at" json:"updatedAt,omitempty"` DeletedAt *time.Time `db:"deleted_at" json:"deletedAt,omitempty"` } + + ModuleFieldOptions map[string]interface{} ) var ( _ sort.Interface = &ModuleFieldSet{} ) +func (mfo *ModuleFieldOptions) 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: + *mfo = ModuleFieldOptions{} + case []uint8: + b := value.([]byte) + if err := json.Unmarshal(b, mfo); err != nil { + return errors.Wrapf(err, "Can not scan '%v' into ModuleFieldOptions", string(b)) + } + } + + return nil +} + +func (mfo ModuleFieldOptions) Value() (driver.Value, error) { + return json.Marshal(mfo) +} + // Resource returns a system resource ID for this type func (m ModuleField) PermissionResource() permissions.Resource { return ModuleFieldPermissionResource.AppendID(m.ID) diff --git a/compose/types/namespace.go b/compose/types/namespace.go index 7a8a25aa0..1bdc2ead5 100644 --- a/compose/types/namespace.go +++ b/compose/types/namespace.go @@ -1,20 +1,22 @@ package types import ( + "database/sql/driver" + "encoding/json" "time" - "github.com/jmoiron/sqlx/types" + "github.com/pkg/errors" "github.com/cortezaproject/corteza-server/internal/permissions" ) type ( Namespace struct { - ID uint64 `db:"id" json:"namespaceID,string"` - Name string `db:"name" json:"name"` - Slug string `db:"slug" json:"slug"` - Enabled bool `db:"enabled" json:"enabled"` - Meta types.JSONText `db:"meta" json:"meta"` + ID uint64 `db:"id" json:"namespaceID,string"` + Name string `db:"name" json:"name"` + Slug string `db:"slug" json:"slug"` + Enabled bool `db:"enabled" json:"enabled"` + Meta NamespaceMeta `db:"meta" json:"meta"` CreatedAt time.Time `db:"created_at" json:"createdAt,omitempty"` UpdatedAt *time.Time `db:"updated_at" json:"updatedAt,omitempty"` @@ -29,6 +31,11 @@ type ( Sort string `json:"sort"` Count uint `json:"count"` } + + NamespaceMeta struct { + Subtitle string `json:"subtitle,omitempty"` + Description string `json:"description,omitempty"` + } ) const ( @@ -50,3 +57,22 @@ func (set NamespaceSet) FindByHandle(handle string) *Namespace { return nil } + +func (nm *NamespaceMeta) 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: + *nm = NamespaceMeta{} + case []uint8: + b := value.([]byte) + if err := json.Unmarshal(b, nm); err != nil { + return errors.Wrapf(err, "Can not scan '%v' into NamespaceMeta", string(b)) + } + } + + return nil +} + +func (nm NamespaceMeta) Value() (driver.Value, error) { + return json.Marshal(nm) +} diff --git a/compose/types/page.go b/compose/types/page.go index 0f08b4e13..2a876b767 100644 --- a/compose/types/page.go +++ b/compose/types/page.go @@ -44,10 +44,7 @@ type ( 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"` + XYWH [4]int `json:"xywh" yaml:"xywh,flow"` // x,y,w,h } PageBlockStyle struct { @@ -101,6 +98,34 @@ func (bb PageBlocks) Value() (driver.Value, error) { return json.Marshal(bb) } +// Helper to extract old encoding to new one +func (b *PageBlock) UnmarshalJSON(data []byte) (err error) { + type internalPageBlock PageBlock + i := struct { + internalPageBlock + X int `json:"x,omitempty" yaml:"-"` + Y int `json:"y,omitempty" yaml:"-"` + Width int `json:"width,omitempty" yaml:"-"` + Height int `json:"height,omitempty" yaml:"-"` + }{} + + if err = json.Unmarshal(data, &i); err != nil { + return + } + + *b = PageBlock(i.internalPageBlock) + if i.XYWH[0]+i.XYWH[1]+i.XYWH[2]+i.XYWH[3] > 0 { + return nil + } + + if i.X+i.Y+i.Width+i.Height > 0 { + // Cast old x,y,w,h structure to this: + b.XYWH = [4]int{i.X, i.Y, i.Width, i.Height} + } + + return nil +} + func (set PageSet) FindByParent(parentID uint64) (out PageSet) { out = PageSet{} for i := range set {