2013-02-28 13:28:48 +08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"labix.org/v2/mgo/bson"
|
|
|
|
"github.com/mikespook/golib/autoinc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IdGenerator interface {
|
|
|
|
Id() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectId
|
2013-08-07 15:30:41 +08:00
|
|
|
type objectId struct {}
|
2013-02-28 13:28:48 +08:00
|
|
|
|
|
|
|
func (id *objectId) Id() string {
|
2013-08-07 15:30:41 +08:00
|
|
|
return bson.NewObjectId().Hex()
|
2013-02-28 13:28:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewObjectId() IdGenerator {
|
2013-08-07 15:30:41 +08:00
|
|
|
return &objectId{}
|
2013-02-28 13:28:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AutoIncId
|
|
|
|
type autoincId struct {
|
|
|
|
*autoinc.AutoInc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id *autoincId) Id() string {
|
|
|
|
return strconv.Itoa(id.AutoInc.Id())
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAutoIncId() IdGenerator {
|
|
|
|
return &autoincId{autoinc.New(1, 1)}
|
|
|
|
}
|