...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package strfmt
16
17 import (
18 "database/sql/driver"
19 "fmt"
20
21 "go.mongodb.org/mongo-driver/bson"
22
23 "go.mongodb.org/mongo-driver/bson/bsontype"
24 bsonprim "go.mongodb.org/mongo-driver/bson/primitive"
25 )
26
27 func init() {
28 var id ObjectId
29
30 Default.Add("bsonobjectid", &id, IsBSONObjectID)
31 }
32
33
34 func IsBSONObjectID(str string) bool {
35 _, err := bsonprim.ObjectIDFromHex(str)
36 return err == nil
37 }
38
39
40
41
42 type ObjectId bsonprim.ObjectID
43
44
45 func NewObjectId(hex string) ObjectId {
46 oid, err := bsonprim.ObjectIDFromHex(hex)
47 if err != nil {
48 panic(err)
49 }
50 return ObjectId(oid)
51 }
52
53
54 func (id ObjectId) MarshalText() ([]byte, error) {
55 oid := bsonprim.ObjectID(id)
56 if oid == bsonprim.NilObjectID {
57 return nil, nil
58 }
59 return []byte(oid.Hex()), nil
60 }
61
62
63 func (id *ObjectId) UnmarshalText(data []byte) error {
64 if len(data) == 0 {
65 *id = ObjectId(bsonprim.NilObjectID)
66 return nil
67 }
68 oidstr := string(data)
69 oid, err := bsonprim.ObjectIDFromHex(oidstr)
70 if err != nil {
71 return err
72 }
73 *id = ObjectId(oid)
74 return nil
75 }
76
77
78 func (id *ObjectId) Scan(raw interface{}) error {
79 var data []byte
80 switch v := raw.(type) {
81 case []byte:
82 data = v
83 case string:
84 data = []byte(v)
85 default:
86 return fmt.Errorf("cannot sql.Scan() strfmt.URI from: %#v", v)
87 }
88
89 return id.UnmarshalText(data)
90 }
91
92
93 func (id ObjectId) Value() (driver.Value, error) {
94 return driver.Value(bsonprim.ObjectID(id).Hex()), nil
95 }
96
97 func (id ObjectId) String() string {
98 return bsonprim.ObjectID(id).Hex()
99 }
100
101
102 func (id ObjectId) MarshalJSON() ([]byte, error) {
103 return bsonprim.ObjectID(id).MarshalJSON()
104 }
105
106
107 func (id *ObjectId) UnmarshalJSON(data []byte) error {
108 var obj bsonprim.ObjectID
109 if err := obj.UnmarshalJSON(data); err != nil {
110 return err
111 }
112 *id = ObjectId(obj)
113 return nil
114 }
115
116
117 func (id ObjectId) MarshalBSON() ([]byte, error) {
118 return bson.Marshal(bson.M{"data": bsonprim.ObjectID(id)})
119 }
120
121
122 func (id *ObjectId) UnmarshalBSON(data []byte) error {
123 var obj struct {
124 Data bsonprim.ObjectID
125 }
126 if err := bson.Unmarshal(data, &obj); err != nil {
127 return err
128 }
129 *id = ObjectId(obj.Data)
130 return nil
131 }
132
133
134
135
136 func (id ObjectId) MarshalBSONValue() (bsontype.Type, []byte, error) {
137 oid := bsonprim.ObjectID(id)
138 return bson.TypeObjectID, oid[:], nil
139 }
140
141
142
143
144
145 func (id *ObjectId) UnmarshalBSONValue(_ bsontype.Type, data []byte) error {
146 var oid bsonprim.ObjectID
147 copy(oid[:], data)
148 *id = ObjectId(oid)
149 return nil
150 }
151
152
153 func (id *ObjectId) DeepCopyInto(out *ObjectId) {
154 *out = *id
155 }
156
157
158 func (id *ObjectId) DeepCopy() *ObjectId {
159 if id == nil {
160 return nil
161 }
162 out := new(ObjectId)
163 id.DeepCopyInto(out)
164 return out
165 }
166
View as plain text