123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package strfmt
- import (
- "database/sql/driver"
- "errors"
- "fmt"
- "github.com/globalsign/mgo/bson"
- "github.com/mailru/easyjson/jlexer"
- "github.com/mailru/easyjson/jwriter"
- )
- func init() {
- var id ObjectId
-
- Default.Add("bsonobjectid", &id, IsBSONObjectID)
- }
- func IsBSONObjectID(str string) bool {
- return bson.IsObjectIdHex(str)
- }
- type ObjectId bson.ObjectId
- func NewObjectId(hex string) ObjectId {
- return ObjectId(bson.ObjectIdHex(hex))
- }
- func (id *ObjectId) MarshalText() ([]byte, error) {
- return []byte(bson.ObjectId(*id).Hex()), nil
- }
- func (id *ObjectId) UnmarshalText(data []byte) error {
- *id = ObjectId(bson.ObjectIdHex(string(data)))
- return nil
- }
- func (id *ObjectId) Scan(raw interface{}) error {
- var data []byte
- switch v := raw.(type) {
- case []byte:
- data = v
- case string:
- data = []byte(v)
- default:
- return fmt.Errorf("cannot sql.Scan() strfmt.URI from: %#v", v)
- }
- return id.UnmarshalText(data)
- }
- func (id *ObjectId) Value() (driver.Value, error) {
- return driver.Value(string(*id)), nil
- }
- func (id *ObjectId) String() string {
- return string(*id)
- }
- func (id *ObjectId) MarshalJSON() ([]byte, error) {
- var w jwriter.Writer
- id.MarshalEasyJSON(&w)
- return w.BuildBytes()
- }
- func (id *ObjectId) MarshalEasyJSON(w *jwriter.Writer) {
- w.String(bson.ObjectId(*id).Hex())
- }
- func (id *ObjectId) UnmarshalJSON(data []byte) error {
- l := jlexer.Lexer{Data: data}
- id.UnmarshalEasyJSON(&l)
- return l.Error()
- }
- func (id *ObjectId) UnmarshalEasyJSON(in *jlexer.Lexer) {
- if data := in.String(); in.Ok() {
- *id = NewObjectId(data)
- }
- }
- func (id *ObjectId) GetBSON() (interface{}, error) {
- return bson.M{"data": bson.ObjectId(*id).Hex()}, nil
- }
- func (id *ObjectId) SetBSON(raw bson.Raw) error {
- var m bson.M
- if err := raw.Unmarshal(&m); err != nil {
- return err
- }
- if data, ok := m["data"].(string); ok {
- *id = NewObjectId(data)
- return nil
- }
- return errors.New("couldn't unmarshal bson raw value as ObjectId")
- }
|