Improve base types

v2
Nikita Tokarchuk 5 years ago
parent db6c11f455
commit 45a41c6c6c
  1. 1
      go.mod
  2. 3
      go.sum
  3. 50
      mongox/base/base.go
  4. 83
      mongox/base/base_test.go
  5. 12
      mongox/base/getid.go
  6. 24
      mongox/base/getid_test.go
  7. 24
      mongox/base/object.go
  8. 24
      mongox/base/objectid.go
  9. 22
      mongox/base/string.go
  10. 12
      mongox/mongox.go

@ -4,6 +4,7 @@ require (
github.com/google/go-cmp v0.3.0 // indirect github.com/google/go-cmp v0.3.0 // indirect
github.com/klauspost/compress v1.10.1 // indirect github.com/klauspost/compress v1.10.1 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.5.1
github.com/xdg/stringprep v1.0.0 // indirect github.com/xdg/stringprep v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.3.0 go.mongodb.org/mongo-driver v1.3.0
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d // indirect golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d // indirect

@ -71,6 +71,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk=
@ -113,4 +115,5 @@ golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgw
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

@ -0,0 +1,50 @@
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)
}

@ -0,0 +1,83 @@
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,18 +12,18 @@ import (
func GetID(source interface{}) (id interface{}) { func GetID(source interface{}) (id interface{}) {
switch doc := source.(type) { switch doc := source.(type) {
case mongox.BaseObjectID: case mongox.ObjectIDBased:
return getObjectIDOrGenerate(doc) return getObjectIDOrGenerate(doc)
case mongox.BaseString: case mongox.StringBased:
return getStringIDOrPanic(doc) return getStringIDOrPanic(doc)
case mongox.BaseObject: case mongox.ObjectBased:
return getObjectOrPanic(doc) return getObjectOrPanic(doc)
default: default:
panic(fmt.Errorf("source contains malformed document, %v", source)) panic(fmt.Errorf("source contains malformed document, %v", source))
} }
} }
func getObjectIDOrGenerate(source mongox.BaseObjectID) (id primitive.ObjectID) { func getObjectIDOrGenerate(source mongox.ObjectIDBased) (id primitive.ObjectID) {
id = source.GetID() id = source.GetID()
if id != primitive.NilObjectID { if id != primitive.NilObjectID {
@ -36,7 +36,7 @@ func getObjectIDOrGenerate(source mongox.BaseObjectID) (id primitive.ObjectID) {
return return
} }
func getStringIDOrPanic(source mongox.BaseString) (id string) { func getStringIDOrPanic(source mongox.StringBased) (id string) {
id = source.GetID() id = source.GetID()
if id != "" { if id != "" {
@ -46,7 +46,7 @@ func getStringIDOrPanic(source mongox.BaseString) (id string) {
panic(fmt.Errorf("source contains malformed document, %v", source)) panic(fmt.Errorf("source contains malformed document, %v", source))
} }
func getObjectOrPanic(source mongox.BaseObject) (id primitive.D) { func getObjectOrPanic(source mongox.ObjectBased) (id primitive.D) {
id = source.GetID() id = source.GetID()
if id != nil { if id != nil {

@ -0,0 +1,24 @@
package base
import (
"testing"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func TestGetID(t *testing.T) {
type DocWithObjectID struct {
ObjectID `bson:"_id" json:"_id" collection:"1"`
}
type DocWithObject struct {
Object `bson:"_id" json:"_id" collection:"2"`
}
type DocWithString struct {
String `bson:"_id" json:"_id" 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")})
}

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

@ -1,24 +0,0 @@
package base
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/mainnika/mongox-go-driver/v2/mongox"
)
var _ mongox.BaseObjectID = &ObjectID{}
// ObjectID is a structure with objectId as an _id field
type ObjectID struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"`
}
// GetID returns an _id
func (db *ObjectID) GetID() primitive.ObjectID {
return db.ID
}
// SetID sets an _id
func (db *ObjectID) SetID(id primitive.ObjectID) {
db.ID = id
}

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

@ -80,20 +80,20 @@ type Resetter interface {
Reset() Reset()
} }
// BaseObjectID is an interface for documents that have objectId type for the _id field // ObjectIDBased is an interface for documents that have objectId type for the _id field
type BaseObjectID interface { type ObjectIDBased interface {
GetID() primitive.ObjectID GetID() primitive.ObjectID
SetID(id primitive.ObjectID) SetID(id primitive.ObjectID)
} }
// BaseString is an interface for documents that have string type for the _id field // StringBased is an interface for documents that have string type for the _id field
type BaseString interface { type StringBased interface {
GetID() string GetID() string
SetID(id string) SetID(id string)
} }
// BaseObject is an interface for documents that have object type for the _id field // ObjectBased is an interface for documents that have object type for the _id field
type BaseObject interface { type ObjectBased interface {
GetID() primitive.D GetID() primitive.D
SetID(id primitive.D) SetID(id primitive.D)
} }

Loading…
Cancel
Save