bson.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package strfmt
  15. import (
  16. "database/sql/driver"
  17. "errors"
  18. "fmt"
  19. "github.com/globalsign/mgo/bson"
  20. "github.com/mailru/easyjson/jlexer"
  21. "github.com/mailru/easyjson/jwriter"
  22. )
  23. func init() {
  24. var id ObjectId
  25. // register this format in the default registry
  26. Default.Add("bsonobjectid", &id, IsBSONObjectID)
  27. }
  28. // IsBSONObjectID returns true when the string is a valid BSON.ObjectId
  29. func IsBSONObjectID(str string) bool {
  30. return bson.IsObjectIdHex(str)
  31. }
  32. // ObjectId represents a BSON object ID (alias to github.com/globalsign/mgo/bson.ObjectId)
  33. //
  34. // swagger:strfmt bsonobjectid
  35. type ObjectId bson.ObjectId
  36. // NewObjectId creates a ObjectId from a Hex String
  37. func NewObjectId(hex string) ObjectId {
  38. return ObjectId(bson.ObjectIdHex(hex))
  39. }
  40. // MarshalText turns this instance into text
  41. func (id *ObjectId) MarshalText() ([]byte, error) {
  42. return []byte(bson.ObjectId(*id).Hex()), nil
  43. }
  44. // UnmarshalText hydrates this instance from text
  45. func (id *ObjectId) UnmarshalText(data []byte) error { // validation is performed later on
  46. *id = ObjectId(bson.ObjectIdHex(string(data)))
  47. return nil
  48. }
  49. // Scan read a value from a database driver
  50. func (id *ObjectId) Scan(raw interface{}) error {
  51. var data []byte
  52. switch v := raw.(type) {
  53. case []byte:
  54. data = v
  55. case string:
  56. data = []byte(v)
  57. default:
  58. return fmt.Errorf("cannot sql.Scan() strfmt.URI from: %#v", v)
  59. }
  60. return id.UnmarshalText(data)
  61. }
  62. // Value converts a value to a database driver value
  63. func (id *ObjectId) Value() (driver.Value, error) {
  64. return driver.Value(string(*id)), nil
  65. }
  66. func (id *ObjectId) String() string {
  67. return string(*id)
  68. }
  69. // MarshalJSON returns the ObjectId as JSON
  70. func (id *ObjectId) MarshalJSON() ([]byte, error) {
  71. var w jwriter.Writer
  72. id.MarshalEasyJSON(&w)
  73. return w.BuildBytes()
  74. }
  75. // MarshalEasyJSON writes the ObjectId to a easyjson.Writer
  76. func (id *ObjectId) MarshalEasyJSON(w *jwriter.Writer) {
  77. w.String(bson.ObjectId(*id).Hex())
  78. }
  79. // UnmarshalJSON sets the ObjectId from JSON
  80. func (id *ObjectId) UnmarshalJSON(data []byte) error {
  81. l := jlexer.Lexer{Data: data}
  82. id.UnmarshalEasyJSON(&l)
  83. return l.Error()
  84. }
  85. // UnmarshalEasyJSON sets the ObjectId from a easyjson.Lexer
  86. func (id *ObjectId) UnmarshalEasyJSON(in *jlexer.Lexer) {
  87. if data := in.String(); in.Ok() {
  88. *id = NewObjectId(data)
  89. }
  90. }
  91. // GetBSON returns the hex representation of the ObjectId as a bson.M{} map.
  92. func (id *ObjectId) GetBSON() (interface{}, error) {
  93. return bson.M{"data": bson.ObjectId(*id).Hex()}, nil
  94. }
  95. // SetBSON sets the ObjectId from raw bson data
  96. func (id *ObjectId) SetBSON(raw bson.Raw) error {
  97. var m bson.M
  98. if err := raw.Unmarshal(&m); err != nil {
  99. return err
  100. }
  101. if data, ok := m["data"].(string); ok {
  102. *id = NewObjectId(data)
  103. return nil
  104. }
  105. return errors.New("couldn't unmarshal bson raw value as ObjectId")
  106. }