diff --git a/mongox/base/base.go b/mongox/base/base.go deleted file mode 100644 index 42fd735..0000000 --- a/mongox/base/base.go +++ /dev/null @@ -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) -} diff --git a/mongox/base/base_test.go b/mongox/base/base_test.go deleted file mode 100644 index b9243e5..0000000 --- a/mongox/base/base_test.go +++ /dev/null @@ -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()) -} diff --git a/mongox/base/getid.go b/mongox/base/getid.go index 1e31767..6209db6 100644 --- a/mongox/base/getid.go +++ b/mongox/base/getid.go @@ -12,11 +12,11 @@ import ( func GetID(source interface{}) (id interface{}) { switch doc := source.(type) { - case mongox.ObjectIDBased: + case mongox.OIDBased: return getObjectIDOrGenerate(doc) case mongox.StringBased: return getStringIDOrPanic(doc) - case mongox.ObjectBased: + case mongox.JSONBased: return getObjectOrPanic(doc) case mongox.InterfaceBased: 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() if id != primitive.NilObjectID { @@ -49,7 +49,7 @@ func getStringIDOrPanic(source mongox.StringBased) (id string) { 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() if id != nil { diff --git a/mongox/base/getid_test.go b/mongox/base/getid_test.go index bf62fad..ab0d4e1 100644 --- a/mongox/base/getid_test.go +++ b/mongox/base/getid_test.go @@ -3,7 +3,12 @@ package base import ( "testing" + "github.com/stretchr/testify/assert" "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 { @@ -21,17 +26,17 @@ func (d *DocWithCustomInterface) SetID(id interface{}) { func TestGetID(t *testing.T) { type DocWithObjectID struct { - ObjectID `bson:"_id" json:"_id" collection:"1"` + oidbased.Primary `bson:",inline" json:",inline" collection:"1"` } type DocWithObject struct { - Object `bson:"_id" json:"_id" collection:"2"` + jsonbased.Primary `bson:",inline" json:",inline" collection:"2"` } 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})}) - GetID(&DocWithObject{Object: Object(primitive.D{{"1", "2"}})}) - GetID(&DocWithString{String: String("foobar")}) - GetID(&DocWithCustomInterface{ID: 420}) + 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}}})) + assert.Equal(t, primitive.D{{"1", "2"}}, GetID(&DocWithObject{jsonbased.Primary{primitive.D{{"1", "2"}}}})) + assert.Equal(t, "foobar", GetID(&DocWithString{stringbased.Primary{"foobar"}})) + assert.Equal(t, 420, GetID(&DocWithCustomInterface{ID: 420})) } diff --git a/mongox/base/jsonbased/id.go b/mongox/base/jsonbased/id.go new file mode 100644 index 0000000..9d8c306 --- /dev/null +++ b/mongox/base/jsonbased/id.go @@ -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 +} diff --git a/mongox/base/jsonbased/id_test.go b/mongox/base/jsonbased/id_test.go new file mode 100644 index 0000000..cb9a241 --- /dev/null +++ b/mongox/base/jsonbased/id_test.go @@ -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)) +} diff --git a/mongox/base/oidbased/id.go b/mongox/base/oidbased/id.go new file mode 100644 index 0000000..2543778 --- /dev/null +++ b/mongox/base/oidbased/id.go @@ -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 +} diff --git a/mongox/base/oidbased/id_test.go b/mongox/base/oidbased/id_test.go new file mode 100644 index 0000000..276d0d9 --- /dev/null +++ b/mongox/base/oidbased/id_test.go @@ -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)) +} diff --git a/mongox/base/stringbased/id.go b/mongox/base/stringbased/id.go new file mode 100644 index 0000000..8e9ad63 --- /dev/null +++ b/mongox/base/stringbased/id.go @@ -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 +} diff --git a/mongox/base/stringbased/id_test.go b/mongox/base/stringbased/id_test.go new file mode 100644 index 0000000..791812f --- /dev/null +++ b/mongox/base/stringbased/id_test.go @@ -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)) +} diff --git a/mongox/mongox.go b/mongox/mongox.go index 04ce522..ec6b220 100644 --- a/mongox/mongox.go +++ b/mongox/mongox.go @@ -96,8 +96,8 @@ type Resetter interface { Reset() } -// ObjectIDBased is an interface for documents that have objectId type for the _id field -type ObjectIDBased interface { +// OIDBased is an interface for documents that have objectId type for the _id field +type OIDBased interface { GetID() primitive.ObjectID SetID(id primitive.ObjectID) } @@ -108,8 +108,8 @@ type StringBased interface { SetID(id string) } -// ObjectBased is an interface for documents that have object type for the _id field -type ObjectBased interface { +// JSONBased is an interface for documents that have object type for the _id field +type JSONBased interface { GetID() primitive.D SetID(id primitive.D) }