Support primary key ID and unique key Name where api uses name

This commit is contained in:
breel
2020-08-02 09:59:47 -06:00
parent 37fe9415e7
commit 8e6e86955e
14 changed files with 68 additions and 49 deletions

View File

@@ -124,9 +124,9 @@ func (bdb *BoltDB) Insert(ctx context.Context, namespace string, doc interface{}
if err := bson.Unmarshal(b, &m); err != nil {
return err
}
if _, ok := m[entity.Name]; !ok {
return errors.New("primary key required to insert: did not find " + entity.Name)
} else if _, ok := m[entity.Name].(string); !ok {
if _, ok := m[entity.ID]; !ok {
return errors.New("primary key required to insert: did not find " + entity.ID)
} else if _, ok := m[entity.ID].(string); !ok {
return errors.New("primary key must be a string")
}
return bdb.db.Update(func(tx *bolt.Tx) error {
@@ -134,7 +134,7 @@ func (bdb *BoltDB) Insert(ctx context.Context, namespace string, doc interface{}
if err != nil {
return err
}
k := []byte(m[entity.Name].(string))
k := []byte(m[entity.ID].(string))
v := bucket.Get(k)
if len(v) > 0 {
return errors.New("cannot insert: collision on primary key")
@@ -272,7 +272,7 @@ func apply(doc bson.M, operator interface{}) (bson.M, error) {
func applyUnset(doc, operator bson.M) (bson.M, error) {
for k := range operator {
if k == entity.Name {
if k == entity.ID {
continue
}
nesting := strings.Split(k, ".")
@@ -305,7 +305,7 @@ func applyUnset(doc, operator bson.M) (bson.M, error) {
func applySet(doc, operator bson.M) (bson.M, error) {
for k, v := range operator {
if k == entity.Name {
if k == entity.ID {
continue
}
nesting := strings.Split(k, ".")

View File

@@ -283,15 +283,16 @@ func TestBoltDBInsert(t *testing.T) {
t.Fatal("could insert colliding object:", err)
}
ones[0].Name = "NEWNAME"
ones[0].ID = "NEWID"
ones[0].Name = "NEWID"
if err := driver.Insert(context.TODO(), testNS, ones[0]); err != nil {
t.Fatal("could not insert object with new Name:", err)
t.Fatal("could not insert object with new ID:", err)
}
if n, err := driver.Count(context.TODO(), testNS, ones[0].Query()); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(err)
t.Fatal(n, err)
}
ch, err = driver.Find(context.TODO(), testNS, ones[0].Query())
@@ -400,25 +401,27 @@ func fillBoltDB(t *testing.T, bdb *BoltDB) {
}
for i := 0; i < testN; i++ {
p := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5],
Relationship: "rshp-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5],
}
o := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5],
Text: "text-" + uuid.New().String()[:5],
Modified: time.Now().UnixNano(),
Connections: map[string]entity.One{p.Name: p},
Connections: map[string]entity.One{p.ID: p},
Attachments: map[string]string{"filename": "/path/to/file"},
}
b, err := bson.Marshal(o)
if err != nil {
return err
}
if err := bucket.Put([]byte(o.Name), b); err != nil {
if err := bucket.Put([]byte(o.ID), b); err != nil {
return err
}
}

View File

@@ -106,18 +106,18 @@ func (mp *Map) Insert(_ context.Context, namespace string, doc interface{}) erro
if err := bson.Unmarshal(b, &m); err != nil {
return err
}
if _, ok := m[entity.Name]; !ok {
return errors.New("primary key required to insert: did not find " + entity.Name)
} else if _, ok := m[entity.Name].(string); !ok {
if _, ok := m[entity.ID]; !ok {
return errors.New("primary key required to insert: did not find " + entity.ID)
} else if _, ok := m[entity.ID].(string); !ok {
return errors.New("primary key must be a string")
}
if _, ok := mp.db[namespace][m[entity.Name].(string)]; ok {
if _, ok := mp.db[namespace][m[entity.ID].(string)]; ok {
return errors.New("cannot insert: collision on primary key")
}
if _, ok := mp.db[namespace]; !ok {
mp.db[namespace] = make(map[string][]byte)
}
mp.db[namespace][m[entity.Name].(string)] = b
mp.db[namespace][m[entity.ID].(string)] = b
return nil
}

View File

@@ -14,25 +14,27 @@ func tempMap(t *testing.T) *Map {
mp.db[testNS] = map[string][]byte{}
for i := 0; i < testN; i++ {
p := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5],
Relationship: "rshp-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5],
}
o := entity.One{
ID: "iddd-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "type-" + uuid.New().String()[:5],
Title: "titl-" + uuid.New().String()[:5],
Text: "text-" + uuid.New().String()[:5],
Modified: time.Now().UnixNano(),
Connections: map[string]entity.One{p.Name: p},
Connections: map[string]entity.One{p.ID: p},
Attachments: map[string]string{"filename": "/path/to/file"},
}
b, err := bson.Marshal(o)
if err != nil {
t.Fatal(err)
}
mp.db[testNS][o.Name] = b
mp.db[testNS][o.ID] = b
}
return mp
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"errors"
"local/dndex/config"
"local/dndex/storage/entity"
"log"
"time"
@@ -77,9 +78,9 @@ func (m Mongo) Insert(ctx context.Context, namespace string, apply interface{})
if err := bson.Unmarshal(b, &mapp); err != nil {
return err
}
if _, ok := mapp["_id"]; !ok {
if _, ok := mapp[entity.ID]; !ok {
return errors.New("no _id in new object")
} else if _, ok := mapp["_id"].(string); !ok {
} else if _, ok := mapp[entity.ID].(string); !ok {
return errors.New("non-string _id in new object")
}
c := m.client.Database(m.db).Collection(namespace)

View File

@@ -10,8 +10,8 @@ import (
)
const (
Name = "_id"
JSONName = "name"
ID = "_id"
Name = "name"
Relationship = "relationship"
Type = "type"
Title = "title"
@@ -22,14 +22,15 @@ const (
)
type One struct {
Name string `bson:"_id,omitempty" json:"name,omitempty"`
ID string `bson:"_id,omitempty" json:"_id,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
Type string `bson:"type,omitempty" json:"type,omitempty"`
Title string `bson:"title,omitempty" json:"title,omitempty"`
Text string `bson:"text,omitempty" json:"text,omitempty"`
Relationship string `bson:"relationship,omitempty" json:"relationship,omitempty"`
Modified int64 `bson:"modified,omitempty" json:"modified,omitempty"`
Connections map[string]One `bson:"connections" json:"connections,omitempty"`
Attachments map[string]string `bson:"attachments,omitempty" json:"attachments,omitempty"`
Attachments map[string]string `bson:"attachments" json:"attachments,omitempty"`
}
func (o One) Query() One {
@@ -75,10 +76,6 @@ func (o One) MarshalBSON() ([]byte, error) {
m[k] = strings.TrimSpace(v.(string))
}
}
if name, ok := m[JSONName]; ok {
m[Name] = name
delete(m, JSONName)
}
if !isMin {
connections := map[string]interface{}{}
switch m[Connections].(type) {

View File

@@ -2,6 +2,7 @@ package storage
import (
"context"
"errors"
"fmt"
"local/dndex/storage/driver"
"local/dndex/storage/entity"
@@ -56,6 +57,9 @@ func (g Graph) gatherOnes(ctx context.Context, ch <-chan bson.Raw) ([]entity.One
}
func (g Graph) Insert(ctx context.Context, namespace string, one entity.One) error {
if one.Name == "" || one.ID == "" {
return errors.New("cannot create document without both name and id")
}
if ones, err := g.ListCaseInsensitive(ctx, namespace, one.Name); err != nil {
return err
} else if len(ones) > 0 {

View File

@@ -366,6 +366,7 @@ func TestIntegration(t *testing.T) {
func randomOne() entity.One {
return entity.One{
ID: "id-" + uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "Humman",
Title: "Biggus",