3
0

Move to typed props for ns, fields, charts...

This commit is contained in:
Denis Arh
2019-09-20 07:33:32 +02:00
parent 55da25788f
commit 8ad87c8824
7 changed files with 106 additions and 39 deletions
+21 -13
View File
@@ -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)
-3
View File
@@ -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
}
+1 -9
View File
@@ -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
}
}
-2
View File
@@ -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))
}
+23 -2
View File
@@ -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)
+32 -6
View File
@@ -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)
}
+29 -4
View File
@@ -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 {