Implement ID bases in packages

v2
Nikita Tokarchuk 5 years ago
parent 7f46008227
commit 2c49854aee
  1. 50
      mongox/base/base.go
  2. 83
      mongox/base/base_test.go
  3. 8
      mongox/base/getid.go
  4. 19
      mongox/base/getid_test.go
  5. 24
      mongox/base/jsonbased/id.go
  6. 80
      mongox/base/jsonbased/id_test.go
  7. 24
      mongox/base/oidbased/id.go
  8. 80
      mongox/base/oidbased/id_test.go
  9. 22
      mongox/base/stringbased/id.go
  10. 78
      mongox/base/stringbased/id_test.go
  11. 8
      mongox/mongox.go

@ -1,50 +0,0 @@
package base
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox"
)
var _ mongox.ObjectBased = (*Object)(nil)
var _ mongox.ObjectIDBased = (*ObjectID)(nil)
var _ mongox.StringBased = (*String)(nil)
// Object is a structure with object as an _id field
type Object primitive.D
// GetID returns an _id
func (db *Object) GetID() primitive.D {
return primitive.D(*db)
}
// SetID sets an _id
func (db *Object) SetID(id primitive.D) {
*db = Object(id)
}
// ObjectID is a structure with objectId as an _id field
type ObjectID primitive.ObjectID
// GetID returns an _id
func (db *ObjectID) GetID() primitive.ObjectID {
return primitive.ObjectID(*db)
}
// SetID sets an _id
func (db *ObjectID) SetID(id primitive.ObjectID) {
*db = ObjectID(id)
}
// String is a structure with string as an _id field
type String string
// GetID returns an _id
func (db *String) GetID() string {
return string(*db)
}
// SetID sets an _id
func (db *String) SetID(id string) {
*db = String(id)
}

@ -1,83 +0,0 @@
package base
import (
"testing"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func TestObject_GetID(t *testing.T) {
type DocWithObject struct {
Object `bson:"_id" json:"_id" collection:"2"`
}
doc := &DocWithObject{Object: Object(primitive.D{{"1", "one"}, {"2", "two"}})}
assert.Equal(t, primitive.D{{"1", "one"}, {"2", "two"}}, doc.GetID())
}
func TestObject_SetID(t *testing.T) {
type DocWithObject struct {
Object `bson:"_id" json:"_id" collection:"2"`
}
doc := &DocWithObject{Object: Object(primitive.D{{"1", "one"}, {"2", "two"}})}
doc.SetID(primitive.D{{"3", "three"}, {"4", "you"}})
assert.Equal(t, primitive.D{{"3", "three"}, {"4", "you"}}, primitive.D(doc.Object))
assert.Equal(t, primitive.D{{"3", "three"}, {"4", "you"}}, primitive.D(doc.GetID()))
}
func TestString_GetID(t *testing.T) {
type DocWithString struct {
String `bson:"_id" json:"_id" collection:"3"`
}
doc := &DocWithString{String: String("foobar")}
assert.Equal(t, "foobar", doc.GetID())
}
func TestString_SetID(t *testing.T) {
type DocWithString struct {
String `bson:"_id" json:"_id" collection:"3"`
}
doc := &DocWithString{String: String("foobar")}
doc.SetID("rockrockrock")
assert.Equal(t, "rockrockrock", string(doc.String))
assert.Equal(t, "rockrockrock", doc.GetID())
}
func TestObjectID_GetID(t *testing.T) {
type DocWithObjectID struct {
ObjectID `bson:"_id" json:"_id" collection:"1"`
}
doc := &DocWithObjectID{ObjectID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}}
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.GetID())
}
func TestObjectID_SetID(t *testing.T) {
type DocWithObjectID struct {
ObjectID `bson:"_id" json:"_id" collection:"1"`
}
doc := &DocWithObjectID{}
doc.SetID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2})
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), primitive.ObjectID(doc.ObjectID))
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.GetID())
}

@ -12,11 +12,11 @@ import (
func GetID(source interface{}) (id interface{}) { func GetID(source interface{}) (id interface{}) {
switch doc := source.(type) { switch doc := source.(type) {
case mongox.ObjectIDBased: case mongox.OIDBased:
return getObjectIDOrGenerate(doc) return getObjectIDOrGenerate(doc)
case mongox.StringBased: case mongox.StringBased:
return getStringIDOrPanic(doc) return getStringIDOrPanic(doc)
case mongox.ObjectBased: case mongox.JSONBased:
return getObjectOrPanic(doc) return getObjectOrPanic(doc)
case mongox.InterfaceBased: case mongox.InterfaceBased:
return getInterfaceOrPanic(doc) return getInterfaceOrPanic(doc)
@ -26,7 +26,7 @@ func GetID(source interface{}) (id interface{}) {
} }
} }
func getObjectIDOrGenerate(source mongox.ObjectIDBased) (id primitive.ObjectID) { func getObjectIDOrGenerate(source mongox.OIDBased) (id primitive.ObjectID) {
id = source.GetID() id = source.GetID()
if id != primitive.NilObjectID { if id != primitive.NilObjectID {
@ -49,7 +49,7 @@ func getStringIDOrPanic(source mongox.StringBased) (id string) {
panic(fmt.Errorf("source contains malformed document, %v", source)) panic(fmt.Errorf("source contains malformed document, %v", source))
} }
func getObjectOrPanic(source mongox.ObjectBased) (id primitive.D) { func getObjectOrPanic(source mongox.JSONBased) (id primitive.D) {
id = source.GetID() id = source.GetID()
if id != nil { if id != nil {

@ -3,7 +3,12 @@ package base
import ( import (
"testing" "testing"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox/base/jsonbased"
"github.com/mainnika/mongox-go-driver/v2/mongox/base/oidbased"
"github.com/mainnika/mongox-go-driver/v2/mongox/base/stringbased"
) )
type DocWithCustomInterface struct { type DocWithCustomInterface struct {
@ -21,17 +26,17 @@ func (d *DocWithCustomInterface) SetID(id interface{}) {
func TestGetID(t *testing.T) { func TestGetID(t *testing.T) {
type DocWithObjectID struct { type DocWithObjectID struct {
ObjectID `bson:"_id" json:"_id" collection:"1"` oidbased.Primary `bson:",inline" json:",inline" collection:"1"`
} }
type DocWithObject struct { type DocWithObject struct {
Object `bson:"_id" json:"_id" collection:"2"` jsonbased.Primary `bson:",inline" json:",inline" collection:"2"`
} }
type DocWithString struct { type DocWithString struct {
String `bson:"_id" json:"_id" collection:"3"` stringbased.Primary `bson:",inline" json:",inline" collection:"3"`
} }
GetID(&DocWithObjectID{ObjectID: ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2})}) assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), GetID(&DocWithObjectID{oidbased.Primary{[12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}}}))
GetID(&DocWithObject{Object: Object(primitive.D{{"1", "2"}})}) assert.Equal(t, primitive.D{{"1", "2"}}, GetID(&DocWithObject{jsonbased.Primary{primitive.D{{"1", "2"}}}}))
GetID(&DocWithString{String: String("foobar")}) assert.Equal(t, "foobar", GetID(&DocWithString{stringbased.Primary{"foobar"}}))
GetID(&DocWithCustomInterface{ID: 420}) assert.Equal(t, 420, GetID(&DocWithCustomInterface{ID: 420}))
} }

@ -0,0 +1,24 @@
package jsonbased
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox"
)
var _ mongox.JSONBased = (*Primary)(nil)
// Primary is a structure with object as an _id field
type Primary struct {
ID primitive.D `bson:"_id" json:"_id"`
}
// GetID returns an _id
func (p *Primary) GetID() primitive.D {
return p.ID
}
// SetID sets an _id
func (p *Primary) SetID(id primitive.D) {
p.ID = id
}

@ -0,0 +1,80 @@
package jsonbased
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox/tempdb"
)
func Test_GetID(t *testing.T) {
type DocWithObject struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
doc := &DocWithObject{Primary{primitive.D{{"1", "one"}, {"2", "two"}}}}
assert.Equal(t, primitive.D{{"1", "one"}, {"2", "two"}}, doc.GetID())
}
func Test_SetID(t *testing.T) {
type DocWithObject struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
doc := &DocWithObject{Primary{primitive.D{{"1", "one"}, {"2", "two"}}}}
doc.SetID(primitive.D{{"3", "three"}, {"4", "you"}})
assert.Equal(t, primitive.D{{"3", "three"}, {"4", "you"}}, doc.Primary.ID)
assert.Equal(t, primitive.D{{"3", "three"}, {"4", "you"}}, doc.GetID())
}
func Test_SaveLoad(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
db, err := tempdb.NewTempDB("mongodb://localhost")
if err != nil {
t.Fatal(err)
}
defer db.Close()
doc1 := &DocWithObjectID{Primary{primitive.D{{"1", "one"}, {"2", "two"}}}}
doc2 := &DocWithObjectID{}
err = db.SaveOne(doc1)
assert.NoError(t, err)
err = db.LoadOne(doc2)
assert.NoError(t, err)
assert.Equal(t, doc1, doc2)
bytes1, _ := json.Marshal(doc1)
bytes2, _ := json.Marshal(doc2)
assert.Equal(t, bytes1, bytes2)
}
func Test_Marshal(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
id := primitive.D{{"1", "one"}, {"2", "two"}}
doc := &DocWithObjectID{Primary{id}}
bytes, err := json.Marshal(doc)
assert.NoError(t, err)
assert.Equal(t, `{"_id":[{"Key":"1","Value":"one"},{"Key":"2","Value":"two"}]}`, string(bytes))
}

@ -0,0 +1,24 @@
package oidbased
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox"
)
var _ mongox.OIDBased = (*Primary)(nil)
// Primary is a structure with objectId as the primary key
type Primary struct {
ID primitive.ObjectID `bson:"_id" json:"_id"`
}
// GetID returns an _id
func (p *Primary) GetID() primitive.ObjectID {
return p.ID
}
// SetID sets an _id
func (p *Primary) SetID(id primitive.ObjectID) {
p.ID = id
}

@ -0,0 +1,80 @@
package oidbased
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox/tempdb"
)
func Test_GetID(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
doc := &DocWithObjectID{Primary{[12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}}}
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.GetID())
}
func Test_SetID(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
doc := &DocWithObjectID{}
doc.SetID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2})
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.Primary.ID)
assert.Equal(t, primitive.ObjectID([12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}), doc.GetID())
}
func Test_SaveLoad(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
db, err := tempdb.NewTempDB("mongodb://localhost")
if err != nil {
t.Fatal(err)
}
defer db.Close()
doc1 := &DocWithObjectID{}
doc2 := &DocWithObjectID{}
err = db.SaveOne(doc1)
assert.NoError(t, err)
err = db.LoadOne(doc2)
assert.NoError(t, err)
assert.Equal(t, doc1, doc2)
bytes1, _ := json.Marshal(doc1)
bytes2, _ := json.Marshal(doc2)
assert.Equal(t, bytes1, bytes2)
}
func Test_Marshal(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
id, _ := primitive.ObjectIDFromHex("feadbeeffeadbeeffeadbeef")
doc := &DocWithObjectID{Primary{id}}
bytes, err := json.Marshal(doc)
assert.NoError(t, err)
assert.Equal(t, `{"_id":"feadbeeffeadbeeffeadbeef"}`, string(bytes))
}

@ -0,0 +1,22 @@
package stringbased
import (
"github.com/mainnika/mongox-go-driver/v2/mongox"
)
var _ mongox.StringBased = (*Primary)(nil)
// Primary is a structure with string as an _id field
type Primary struct {
ID string `bson:"_id" json:"_id"`
}
// GetID returns an _id
func (p *Primary) GetID() string {
return p.ID
}
// SetID sets an _id
func (p *Primary) SetID(id string) {
p.ID = id
}

@ -0,0 +1,78 @@
package stringbased
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/mainnika/mongox-go-driver/v2/mongox/tempdb"
)
func Test_GetID(t *testing.T) {
type DocWithString struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
doc := &DocWithString{Primary{"foobar"}}
assert.Equal(t, "foobar", doc.GetID())
}
func Test_SetID(t *testing.T) {
type DocWithString struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
doc := &DocWithString{Primary{"foobar"}}
doc.SetID("rockrockrock")
assert.Equal(t, "rockrockrock", doc.Primary.ID)
assert.Equal(t, "rockrockrock", doc.GetID())
}
func Test_SaveLoad(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
db, err := tempdb.NewTempDB("mongodb://localhost")
if err != nil {
t.Fatal(err)
}
defer db.Close()
doc1 := &DocWithObjectID{Primary{"foobar"}}
doc2 := &DocWithObjectID{}
err = db.SaveOne(doc1)
assert.NoError(t, err)
err = db.LoadOne(doc2)
assert.NoError(t, err)
assert.Equal(t, doc1, doc2)
bytes1, _ := json.Marshal(doc1)
bytes2, _ := json.Marshal(doc2)
assert.Equal(t, bytes1, bytes2)
}
func Test_Marshal(t *testing.T) {
type DocWithObjectID struct {
Primary `bson:",inline" json:",inline" collection:"1"`
}
doc := &DocWithObjectID{Primary{"foobar"}}
bytes, err := json.Marshal(doc)
assert.NoError(t, err)
assert.Equal(t, `{"_id":"foobar"}`, string(bytes))
}

@ -96,8 +96,8 @@ type Resetter interface {
Reset() Reset()
} }
// ObjectIDBased is an interface for documents that have objectId type for the _id field // OIDBased is an interface for documents that have objectId type for the _id field
type ObjectIDBased interface { type OIDBased interface {
GetID() primitive.ObjectID GetID() primitive.ObjectID
SetID(id primitive.ObjectID) SetID(id primitive.ObjectID)
} }
@ -108,8 +108,8 @@ type StringBased interface {
SetID(id string) SetID(id string)
} }
// ObjectBased is an interface for documents that have object type for the _id field // JSONBased is an interface for documents that have object type for the _id field
type ObjectBased interface { type JSONBased interface {
GetID() primitive.D GetID() primitive.D
SetID(id primitive.D) SetID(id primitive.D)
} }

Loading…
Cancel
Save