Support primary key ID and unique key Name where api uses name
This commit is contained in:
@@ -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, ".")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user