Skip to content

Commit

Permalink
wip(bolt): initial fixes for uint64 platform.IDs
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
Co-Authored-by: Lorenzo Fontana <lo@linux.com>
  • Loading branch information
2 people authored and goller committed Oct 11, 2018
1 parent 9fa032d commit e534efc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
7 changes: 6 additions & 1 deletion bolt/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,5 +341,10 @@ func (c *Client) updateAuthorization(ctx context.Context, tx *bolt.Tx, id platfo
return err
}

return tx.Bucket(authorizationBucket).Put(a.ID, b)
encodedID, err := id.Encode()
if err != nil {
return err
}

return tx.Bucket(authorizationBucket).Put(encodedID, b)
}
13 changes: 6 additions & 7 deletions bolt/dashboard.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bolt

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -172,7 +171,7 @@ func (c *Client) CreateDashboard(ctx context.Context, d *platform.Dashboard) err
}

func (c *Client) createViewIfNotExists(ctx context.Context, tx *bolt.Tx, cell *platform.Cell, opts platform.AddDashboardCellOptions) error {
if len(opts.UsingView) != 0 {
if opts.UsingView.Valid() {
// Creates a hard copy of a view
v, err := c.findViewByID(ctx, tx, opts.UsingView)
if err != nil {
Expand All @@ -184,7 +183,7 @@ func (c *Client) createViewIfNotExists(ctx context.Context, tx *bolt.Tx, cell *p
}
cell.ViewID = view.ID
return nil
} else if len(cell.ViewID) != 0 {
} else if cell.ViewID.Valid() {
// Creates a soft copy of a view
_, err := c.findViewByID(ctx, tx, cell.ViewID)
if err != nil {
Expand Down Expand Up @@ -217,7 +216,7 @@ func (c *Client) ReplaceDashboardCells(ctx context.Context, id platform.ID, cs [
}

for _, cell := range cs {
if len(cell.ID) == 0 {
if !cell.ID.Valid() {
return fmt.Errorf("cannot provide empty cell id")
}

Expand All @@ -226,7 +225,7 @@ func (c *Client) ReplaceDashboardCells(ctx context.Context, id platform.ID, cs [
return fmt.Errorf("cannot replace cells that were not already present")
}

if !bytes.Equal(cl.ViewID, cell.ViewID) {
if cl.ViewID == cell.ViewID {
return fmt.Errorf("cannot update view id in replace")
}
}
Expand Down Expand Up @@ -264,7 +263,7 @@ func (c *Client) RemoveDashboardCell(ctx context.Context, dashboardID, cellID pl

idx := -1
for i, cell := range d.Cells {
if bytes.Equal(cell.ID, cellID) {
if cell.ID == cellID {
idx = i
break
}
Expand Down Expand Up @@ -293,7 +292,7 @@ func (c *Client) UpdateDashboardCell(ctx context.Context, dashboardID, cellID pl

idx := -1
for i, cell := range d.Cells {
if bytes.Equal(cell.ID, cellID) {
if cell.ID == cellID {
idx = i
break
}
Expand Down
31 changes: 25 additions & 6 deletions bolt/source.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bolt

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -26,10 +25,14 @@ func init() {
if err := DefaultSource.ID.DecodeFromString("020f755c3c082000"); err != nil {
panic(fmt.Sprintf("failed to decode default source id: %v", err))
}

if err := DefaultSource.OrganizationID.DecodeFromString("50616e67652c206c"); err != nil {
panic(fmt.Sprintf("failed to decode default source organization id: %v", err))
}
}

func (c *Client) initializeSources(ctx context.Context, tx *bolt.Tx) error {
if _, err := tx.CreateBucketIfNotExists([]byte(sourceBucket)); err != nil {
if _, err := tx.CreateBucketIfNotExists(sourceBucket); err != nil {
return err
}

Expand Down Expand Up @@ -94,7 +97,12 @@ func (c *Client) FindSourceByID(ctx context.Context, id platform.ID) (*platform.
}

func (c *Client) findSourceByID(ctx context.Context, tx *bolt.Tx, id platform.ID) (*platform.Source, error) {
v := tx.Bucket(sourceBucket).Get(id)
encodedID, err := id.Encode()
if err != nil {
return nil, err
}

v := tx.Bucket(sourceBucket).Get(encodedID)

if len(v) == 0 {
return nil, platform.ErrSourceNotFound
Expand Down Expand Up @@ -166,7 +174,12 @@ func (c *Client) putSource(ctx context.Context, tx *bolt.Tx, s *platform.Source)
return err
}

if err := tx.Bucket(sourceBucket).Put(s.ID, v); err != nil {
encodedID, err := s.ID.Encode()
if err != nil {
return err
}

if err := tx.Bucket(sourceBucket).Put(encodedID, v); err != nil {
return err
}

Expand Down Expand Up @@ -229,12 +242,18 @@ func (c *Client) DeleteSource(ctx context.Context, id platform.ID) error {
}

func (c *Client) deleteSource(ctx context.Context, tx *bolt.Tx, id platform.ID) error {
if bytes.Equal(id, DefaultSource.ID) {
if id == DefaultSource.ID {
return fmt.Errorf("cannot delete autogen source")
}
_, err := c.findSourceByID(ctx, tx, id)
if err != nil {
return err
}
return tx.Bucket(sourceBucket).Delete(id)

encodedID, err := id.Encode()
if err != nil {
return err
}

return tx.Bucket(sourceBucket).Delete(encodedID)
}
22 changes: 17 additions & 5 deletions bolt/view.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bolt

import (
"bytes"
"context"
"encoding/json"

Expand Down Expand Up @@ -43,7 +42,12 @@ func (c *Client) FindViewByID(ctx context.Context, id platform.ID) (*platform.Vi
func (c *Client) findViewByID(ctx context.Context, tx *bolt.Tx, id platform.ID) (*platform.View, error) {
var d platform.View

v := tx.Bucket(viewBucket).Get([]byte(id))
encodedID, err := id.Encode()
if err != nil {
return nil, err
}

v := tx.Bucket(viewBucket).Get(encodedID)

if len(v) == 0 {
return nil, platform.ErrViewNotFound
Expand Down Expand Up @@ -108,7 +112,7 @@ func (c *Client) FindView(ctx context.Context, filter platform.ViewFilter) (*pla
func filterViewsFn(filter platform.ViewFilter) func(d *platform.View) bool {
if filter.ID != nil {
return func(d *platform.View) bool {
return bytes.Equal(d.ID, *filter.ID)
return d.ID == *filter.ID
}
}

Expand Down Expand Up @@ -185,7 +189,11 @@ func (c *Client) putView(ctx context.Context, tx *bolt.Tx, d *platform.View) err
if err != nil {
return err
}
if err := tx.Bucket(viewBucket).Put([]byte(d.ID), v); err != nil {
encodedID, err := d.ID.Encode()
if err != nil {
return err
}
if err := tx.Bucket(viewBucket).Put(encodedID, v); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -255,5 +263,9 @@ func (c *Client) deleteView(ctx context.Context, tx *bolt.Tx, id platform.ID) er
if err != nil {
return err
}
return tx.Bucket(viewBucket).Delete([]byte(id))
encodedID, err := id.Encode()
if err != nil {
return err
}
return tx.Bucket(viewBucket).Delete(encodedID)
}

0 comments on commit e534efc

Please sign in to comment.