podman

Форк
0
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

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
	// register this format in the default registry
30
	Default.Add("bsonobjectid", &id, IsBSONObjectID)
31
}
32

33
// IsBSONObjectID returns true when the string is a valid BSON.ObjectId
34
func IsBSONObjectID(str string) bool {
35
	_, err := bsonprim.ObjectIDFromHex(str)
36
	return 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
42
type ObjectId bsonprim.ObjectID //nolint:revive
43

44
// NewObjectId creates a ObjectId from a Hex String
45
func NewObjectId(hex string) ObjectId { //nolint:revive
46
	oid, err := bsonprim.ObjectIDFromHex(hex)
47
	if err != nil {
48
		panic(err)
49
	}
50
	return ObjectId(oid)
51
}
52

53
// MarshalText turns this instance into text
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
// UnmarshalText hydrates this instance from text
63
func (id *ObjectId) UnmarshalText(data []byte) error { // validation is performed later on
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
// Scan read a value from a database driver
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
// Value converts a value to a database driver value
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
// MarshalJSON returns the ObjectId as JSON
102
func (id ObjectId) MarshalJSON() ([]byte, error) {
103
	return bsonprim.ObjectID(id).MarshalJSON()
104
}
105

106
// UnmarshalJSON sets the ObjectId from JSON
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
// MarshalBSON renders the object id as a BSON document
117
func (id ObjectId) MarshalBSON() ([]byte, error) {
118
	return bson.Marshal(bson.M{"data": bsonprim.ObjectID(id)})
119
}
120

121
// UnmarshalBSON reads the objectId from a BSON document
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
// 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.
136
func (id ObjectId) MarshalBSONValue() (bsontype.Type, []byte, error) {
137
	oid := bsonprim.ObjectID(id)
138
	return 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.
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
// DeepCopyInto copies the receiver and writes its value into out.
153
func (id *ObjectId) DeepCopyInto(out *ObjectId) {
154
	*out = *id
155
}
156

157
// DeepCopy copies the receiver into a new ObjectId.
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

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.