本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

166 lines
4.4KB

  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. "fmt"
  18. "go.mongodb.org/mongo-driver/bson"
  19. "go.mongodb.org/mongo-driver/bson/bsontype"
  20. bsonprim "go.mongodb.org/mongo-driver/bson/primitive"
  21. )
  22. func init() {
  23. var id ObjectId
  24. // register this format in the default registry
  25. Default.Add("bsonobjectid", &id, IsBSONObjectID)
  26. }
  27. // IsBSONObjectID returns true when the string is a valid BSON.ObjectId
  28. func IsBSONObjectID(str string) bool {
  29. _, err := bsonprim.ObjectIDFromHex(str)
  30. return err == nil
  31. }
  32. // ObjectId represents a BSON object ID (alias to go.mongodb.org/mongo-driver/bson/primitive.ObjectID)
  33. //
  34. // swagger:strfmt bsonobjectid
  35. type ObjectId bsonprim.ObjectID
  36. // NewObjectId creates a ObjectId from a Hex String
  37. func NewObjectId(hex string) ObjectId {
  38. oid, err := bsonprim.ObjectIDFromHex(hex)
  39. if err != nil {
  40. panic(err)
  41. }
  42. return ObjectId(oid)
  43. }
  44. // MarshalText turns this instance into text
  45. func (id ObjectId) MarshalText() ([]byte, error) {
  46. oid := bsonprim.ObjectID(id)
  47. if oid == bsonprim.NilObjectID {
  48. return nil, nil
  49. }
  50. return []byte(oid.Hex()), nil
  51. }
  52. // UnmarshalText hydrates this instance from text
  53. func (id *ObjectId) UnmarshalText(data []byte) error { // validation is performed later on
  54. if len(data) == 0 {
  55. *id = ObjectId(bsonprim.NilObjectID)
  56. return nil
  57. }
  58. oidstr := string(data)
  59. oid, err := bsonprim.ObjectIDFromHex(oidstr)
  60. if err != nil {
  61. return err
  62. }
  63. *id = ObjectId(oid)
  64. return nil
  65. }
  66. // Scan read a value from a database driver
  67. func (id *ObjectId) Scan(raw interface{}) error {
  68. var data []byte
  69. switch v := raw.(type) {
  70. case []byte:
  71. data = v
  72. case string:
  73. data = []byte(v)
  74. default:
  75. return fmt.Errorf("cannot sql.Scan() strfmt.URI from: %#v", v)
  76. }
  77. return id.UnmarshalText(data)
  78. }
  79. // Value converts a value to a database driver value
  80. func (id ObjectId) Value() (driver.Value, error) {
  81. return driver.Value(bsonprim.ObjectID(id).Hex()), nil
  82. }
  83. func (id ObjectId) String() string {
  84. return bsonprim.ObjectID(id).String()
  85. }
  86. // MarshalJSON returns the ObjectId as JSON
  87. func (id ObjectId) MarshalJSON() ([]byte, error) {
  88. return bsonprim.ObjectID(id).MarshalJSON()
  89. }
  90. // UnmarshalJSON sets the ObjectId from JSON
  91. func (id *ObjectId) UnmarshalJSON(data []byte) error {
  92. var obj bsonprim.ObjectID
  93. if err := obj.UnmarshalJSON(data); err != nil {
  94. return err
  95. }
  96. *id = ObjectId(obj)
  97. return nil
  98. }
  99. // MarshalBSON renders the object id as a BSON document
  100. func (id ObjectId) MarshalBSON() ([]byte, error) {
  101. return bson.Marshal(bson.M{"data": bsonprim.ObjectID(id)})
  102. }
  103. // UnmarshalBSON reads the objectId from a BSON document
  104. func (id *ObjectId) UnmarshalBSON(data []byte) error {
  105. var obj struct {
  106. Data bsonprim.ObjectID
  107. }
  108. if err := bson.Unmarshal(data, &obj); err != nil {
  109. return err
  110. }
  111. *id = ObjectId(obj.Data)
  112. return nil
  113. }
  114. // MarshalBSONValue is an interface implemented by types that can marshal themselves
  115. // into a BSON document represented as bytes. The bytes returned must be a valid
  116. // BSON document if the error is nil.
  117. func (id ObjectId) MarshalBSONValue() (bsontype.Type, []byte, error) {
  118. oid := bsonprim.ObjectID(id)
  119. return bsontype.ObjectID, oid[:], nil
  120. }
  121. // UnmarshalBSONValue is an interface implemented by types that can unmarshal a
  122. // BSON value representation of themselves. The BSON bytes and type can be
  123. // assumed to be valid. UnmarshalBSONValue must copy the BSON value bytes if it
  124. // wishes to retain the data after returning.
  125. func (id *ObjectId) UnmarshalBSONValue(tpe bsontype.Type, data []byte) error {
  126. var oid bsonprim.ObjectID
  127. copy(oid[:], data)
  128. *id = ObjectId(oid)
  129. return nil
  130. }
  131. // DeepCopyInto copies the receiver and writes its value into out.
  132. func (id *ObjectId) DeepCopyInto(out *ObjectId) {
  133. *out = *id
  134. }
  135. // DeepCopy copies the receiver into a new ObjectId.
  136. func (id *ObjectId) DeepCopy() *ObjectId {
  137. if id == nil {
  138. return nil
  139. }
  140. out := new(ObjectId)
  141. id.DeepCopyInto(out)
  142. return out
  143. }
上海开阖软件有限公司 沪ICP备12045867号-1