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