parent
7f46008227
commit
2c49854aee
@ -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()) |
||||
} |
@ -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)) |
||||
} |
Loading…
Reference in new issue