From 8e6e86955e3643beaa7f6ecfb2730ace62c4a717 Mon Sep 17 00:00:00 2001 From: breel Date: Sun, 2 Aug 2020 09:59:47 -0600 Subject: [PATCH] Support primary key ID and unique key Name where api uses name --- storage/driver/boltdb.go | 12 ++++++------ storage/driver/boltdb_test.go | 13 ++++++++----- storage/driver/map.go | 10 +++++----- storage/driver/map_test.go | 6 ++++-- storage/driver/mon.go | 5 +++-- storage/entity/one.go | 13 +++++-------- storage/graph.go | 4 ++++ storage/graph_test.go | 1 + view/auth.go | 5 ++++- view/auth_test.go | 8 ++++---- view/port_test.go | 2 +- view/register.go | 3 +++ view/who.go | 1 - view/who_test.go | 34 ++++++++++++++++++++-------------- 14 files changed, 68 insertions(+), 49 deletions(-) diff --git a/storage/driver/boltdb.go b/storage/driver/boltdb.go index ff6331e..cb75741 100644 --- a/storage/driver/boltdb.go +++ b/storage/driver/boltdb.go @@ -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, ".") diff --git a/storage/driver/boltdb_test.go b/storage/driver/boltdb_test.go index 0b7eb9d..bc53ebe 100644 --- a/storage/driver/boltdb_test.go +++ b/storage/driver/boltdb_test.go @@ -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 } } diff --git a/storage/driver/map.go b/storage/driver/map.go index b2d545a..e114d4f 100644 --- a/storage/driver/map.go +++ b/storage/driver/map.go @@ -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 } diff --git a/storage/driver/map_test.go b/storage/driver/map_test.go index 94c5a1c..3eea872 100644 --- a/storage/driver/map_test.go +++ b/storage/driver/map_test.go @@ -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 } diff --git a/storage/driver/mon.go b/storage/driver/mon.go index 4559494..6b09742 100644 --- a/storage/driver/mon.go +++ b/storage/driver/mon.go @@ -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) diff --git a/storage/entity/one.go b/storage/entity/one.go index 875f7f5..e948aa0 100644 --- a/storage/entity/one.go +++ b/storage/entity/one.go @@ -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) { diff --git a/storage/graph.go b/storage/graph.go index 10ae8a7..491e840 100644 --- a/storage/graph.go +++ b/storage/graph.go @@ -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 { diff --git a/storage/graph_test.go b/storage/graph_test.go index b469d44..e5ccf2f 100644 --- a/storage/graph_test.go +++ b/storage/graph_test.go @@ -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", diff --git a/view/auth.go b/view/auth.go index e4f3136..e442b67 100644 --- a/view/auth.go +++ b/view/auth.go @@ -47,6 +47,7 @@ func auth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) er func isPublic(g storage.RateLimitedGraph, r *http.Request) bool { namespace, err := getAuthNamespace(r) + if err != nil { return false } @@ -103,8 +104,10 @@ func requestAuth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Requ } userKey := ones[0] + id := uuid.New().String() token := entity.One{ - Name: uuid.New().String(), + ID: id, + Name: id, Title: namespace, } if err := g.Insert(r.Context(), namespace, token); err != nil { diff --git a/view/auth_test.go b/view/auth_test.go index 86bead7..c288f72 100644 --- a/view/auth_test.go +++ b/view/auth_test.go @@ -22,7 +22,7 @@ func TestAuth(t *testing.T) { g := storage.NewRateLimitedGraph() handler := jsonHandler(g) - if err := g.Insert(context.TODO(), "col."+AuthKey, entity.One{Name: UserKey, Title: "password"}); err != nil { + if err := g.Insert(context.TODO(), "col."+AuthKey, entity.One{ID: UserKey, Name: UserKey, Title: "password"}); err != nil { t.Fatal(err) } @@ -48,13 +48,13 @@ func TestAuth(t *testing.T) { t.Run("auth: expired provided", func(t *testing.T) { os.Setenv("AUTHLIFETIME", "1ms") defer os.Setenv("AUTHLIFETIME", "1h") - one := entity.One{Name: uuid.New().String(), Title: "title"} + one := entity.One{ID: uuid.New().String(), Name: uuid.New().String(), Title: "title"} if err := g.Insert(context.TODO(), "col", one); err != nil { t.Fatal(err) } time.Sleep(time.Millisecond * 50) r := httptest.NewRequest(http.MethodGet, "/who?namespace=col", nil) - r.Header.Set("Cookie", fmt.Sprintf("%s=%s", AuthKey, one.Name)) + r.Header.Set("Cookie", fmt.Sprintf("%s=%s", AuthKey, one.ID)) w := httptest.NewRecorder() handler.ServeHTTP(w, r) if w.Code != http.StatusSeeOther { @@ -82,7 +82,7 @@ func TestAuth(t *testing.T) { t.Run("auth: provided", func(t *testing.T) { os.Setenv("AUTHLIFETIME", "1h") - one := entity.One{Name: uuid.New().String(), Title: "title"} + one := entity.One{ID: uuid.New().String(), Name: uuid.New().String(), Title: "title"} if err := g.Insert(context.TODO(), "col."+AuthKey, one); err != nil { t.Fatal(err) } diff --git a/view/port_test.go b/view/port_test.go index b11978c..875dba7 100644 --- a/view/port_test.go +++ b/view/port_test.go @@ -24,7 +24,7 @@ func TestPort(t *testing.T) { t.Fatal(err) } for _, name := range []string{"A", "B", "C"} { - if err := g.Insert(context.TODO(), "col", entity.One{Name: name}); err != nil { + if err := g.Insert(context.TODO(), "col", entity.One{ID: "id-" + name, Name: name}); err != nil { t.Fatal(err) } } diff --git a/view/register.go b/view/register.go index aef1f48..35537fd 100644 --- a/view/register.go +++ b/view/register.go @@ -5,6 +5,8 @@ import ( "local/dndex/storage" "local/dndex/storage/entity" "net/http" + + "github.com/google/uuid" ) func register(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error { @@ -33,6 +35,7 @@ func registerPost(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Req return nil } one := entity.One{ + ID: uuid.New().String(), Name: UserKey, Title: password, } diff --git a/view/who.go b/view/who.go index df3fe39..c714f07 100644 --- a/view/who.go +++ b/view/who.go @@ -170,7 +170,6 @@ func whoPut(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter, return nil } - body = bytes.ReplaceAll(body, []byte(`"`+entity.JSONName+`"`), []byte(`"`+entity.Name+`"`)) op := bson.M{} if err := json.Unmarshal(body, &op); err != nil { return err diff --git a/view/who_test.go b/view/who_test.go index 4c03d92..fbb5261 100644 --- a/view/who_test.go +++ b/view/who_test.go @@ -58,7 +58,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - o := entity.One{} + var o entity.One if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } @@ -105,7 +105,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - o := entity.One{} + var o entity.One if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } @@ -140,7 +140,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - o := entity.One{} + var o entity.One if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } @@ -175,7 +175,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - o := entity.One{} + var o entity.One if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } @@ -207,7 +207,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - o := entity.One{} + var o entity.One if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } @@ -260,7 +260,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - o := entity.One{} + var o entity.One if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } @@ -278,7 +278,7 @@ func TestWho(t *testing.T) { handler, _, iwant, _, can := fresh(t) defer can() want := iwant - r := httptest.NewRequest(http.MethodPost, "/who?namespace=col&id="+want.Name, strings.NewReader(`{"title":"this should fail to insert"}`)) + r := httptest.NewRequest(http.MethodPost, "/who?namespace=col&id="+want.Name, strings.NewReader(`{"title":"this should fail to insert", "_id":"trash"}`)) w := httptest.NewRecorder() handler.ServeHTTP(w, r) if w.Code != http.StatusConflict { @@ -291,6 +291,7 @@ func TestWho(t *testing.T) { defer can() iwant := want iwant.Name = "" + iwant.ID = "NEWBIE" + iwant.ID b, err := json.Marshal(iwant) if err != nil { t.Fatal(err) @@ -301,7 +302,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - o := entity.One{} + var o entity.One if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } @@ -319,6 +320,7 @@ func TestWho(t *testing.T) { handler, _, want, _, can := fresh(t) defer can() want.Name = "hello world #1 e ę" + want.ID = "hello world #1 e ę" + want.ID want.Connections = nil b, err := json.Marshal(want) if err != nil { @@ -336,7 +338,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - o := entity.One{} + var o entity.One if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } @@ -369,6 +371,7 @@ func TestWho(t *testing.T) { defer can() want.Name = "hello world #4" + want.ID = "hello world #4" + want.ID want.Connections = nil b, err := json.Marshal(want) if err != nil { @@ -410,6 +413,7 @@ func TestWho(t *testing.T) { defer can() want.Name = "hello world #1 e ę" + want.ID = "hello world #1 e ę" + want.ID want.Connections = nil b, err := json.Marshal(want) if err != nil { @@ -499,6 +503,7 @@ func TestWho(t *testing.T) { from := ones[4] push := ones[10].Peer() push.Relationship = "spawn" + push.ID = uuid.New().String() b, err := json.Marshal(push) if err != nil { t.Fatal(err) @@ -509,7 +514,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - got := entity.One{} + var got entity.One if err := json.NewDecoder(w.Body).Decode(&got); err != nil { t.Fatal(err) } @@ -558,7 +563,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - got := entity.One{} + var got entity.One if err := json.NewDecoder(w.Body).Decode(&got); err != nil { t.Fatal(err) } @@ -675,7 +680,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - o := entity.One{} + var o entity.One if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } @@ -702,7 +707,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - o := entity.One{} + var o entity.One if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } @@ -744,7 +749,7 @@ func TestWho(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("%d: %s", w.Code, w.Body.Bytes()) } - o := entity.One{} + var o entity.One if err := json.NewDecoder(w.Body).Decode(&o); err != nil { t.Fatal(err) } @@ -943,6 +948,7 @@ func fillDB(t *testing.T, g storage.RateLimitedGraph) []entity.One { func randomOne() entity.One { return 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],