podman

Форк
0
217 строк · 5.6 Кб
1
// Copyright (C) MongoDB, Inc. 2017-present.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4
// not use this file except in compliance with the License. You may obtain
5
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6

7
// Package primitive contains types similar to Go primitives for BSON types that do not have direct
8
// Go primitive representations.
9
package primitive // import "go.mongodb.org/mongo-driver/bson/primitive"
10

11
import (
12
	"bytes"
13
	"encoding/json"
14
	"fmt"
15
	"time"
16
)
17

18
// Binary represents a BSON binary value.
19
type Binary struct {
20
	Subtype byte
21
	Data    []byte
22
}
23

24
// Equal compares bp to bp2 and returns true if they are equal.
25
func (bp Binary) Equal(bp2 Binary) bool {
26
	if bp.Subtype != bp2.Subtype {
27
		return false
28
	}
29
	return bytes.Equal(bp.Data, bp2.Data)
30
}
31

32
// IsZero returns if bp is the empty Binary.
33
func (bp Binary) IsZero() bool {
34
	return bp.Subtype == 0 && len(bp.Data) == 0
35
}
36

37
// Undefined represents the BSON undefined value type.
38
type Undefined struct{}
39

40
// DateTime represents the BSON datetime value.
41
type DateTime int64
42

43
var _ json.Marshaler = DateTime(0)
44
var _ json.Unmarshaler = (*DateTime)(nil)
45

46
// MarshalJSON marshal to time type.
47
func (d DateTime) MarshalJSON() ([]byte, error) {
48
	return json.Marshal(d.Time())
49
}
50

51
// UnmarshalJSON creates a primitive.DateTime from a JSON string.
52
func (d *DateTime) UnmarshalJSON(data []byte) error {
53
	// Ignore "null" to keep parity with the time.Time type and the standard library. Decoding "null" into a non-pointer
54
	// DateTime field will leave the field unchanged. For pointer values, the encoding/json will set the pointer to nil
55
	// and will not defer to the UnmarshalJSON hook.
56
	if string(data) == "null" {
57
		return nil
58
	}
59

60
	var tempTime time.Time
61
	if err := json.Unmarshal(data, &tempTime); err != nil {
62
		return err
63
	}
64

65
	*d = NewDateTimeFromTime(tempTime)
66
	return nil
67
}
68

69
// Time returns the date as a time type.
70
func (d DateTime) Time() time.Time {
71
	return time.Unix(int64(d)/1000, int64(d)%1000*1000000)
72
}
73

74
// NewDateTimeFromTime creates a new DateTime from a Time.
75
func NewDateTimeFromTime(t time.Time) DateTime {
76
	return DateTime(t.Unix()*1e3 + int64(t.Nanosecond())/1e6)
77
}
78

79
// Null represents the BSON null value.
80
type Null struct{}
81

82
// Regex represents a BSON regex value.
83
type Regex struct {
84
	Pattern string
85
	Options string
86
}
87

88
func (rp Regex) String() string {
89
	return fmt.Sprintf(`{"pattern": "%s", "options": "%s"}`, rp.Pattern, rp.Options)
90
}
91

92
// Equal compares rp to rp2 and returns true if they are equal.
93
func (rp Regex) Equal(rp2 Regex) bool {
94
	return rp.Pattern == rp2.Pattern && rp.Options == rp2.Options
95
}
96

97
// IsZero returns if rp is the empty Regex.
98
func (rp Regex) IsZero() bool {
99
	return rp.Pattern == "" && rp.Options == ""
100
}
101

102
// DBPointer represents a BSON dbpointer value.
103
type DBPointer struct {
104
	DB      string
105
	Pointer ObjectID
106
}
107

108
func (d DBPointer) String() string {
109
	return fmt.Sprintf(`{"db": "%s", "pointer": "%s"}`, d.DB, d.Pointer)
110
}
111

112
// Equal compares d to d2 and returns true if they are equal.
113
func (d DBPointer) Equal(d2 DBPointer) bool {
114
	return d == d2
115
}
116

117
// IsZero returns if d is the empty DBPointer.
118
func (d DBPointer) IsZero() bool {
119
	return d.DB == "" && d.Pointer.IsZero()
120
}
121

122
// JavaScript represents a BSON JavaScript code value.
123
type JavaScript string
124

125
// Symbol represents a BSON symbol value.
126
type Symbol string
127

128
// CodeWithScope represents a BSON JavaScript code with scope value.
129
type CodeWithScope struct {
130
	Code  JavaScript
131
	Scope interface{}
132
}
133

134
func (cws CodeWithScope) String() string {
135
	return fmt.Sprintf(`{"code": "%s", "scope": %v}`, cws.Code, cws.Scope)
136
}
137

138
// Timestamp represents a BSON timestamp value.
139
type Timestamp struct {
140
	T uint32
141
	I uint32
142
}
143

144
// Equal compares tp to tp2 and returns true if they are equal.
145
func (tp Timestamp) Equal(tp2 Timestamp) bool {
146
	return tp.T == tp2.T && tp.I == tp2.I
147
}
148

149
// IsZero returns if tp is the zero Timestamp.
150
func (tp Timestamp) IsZero() bool {
151
	return tp.T == 0 && tp.I == 0
152
}
153

154
// CompareTimestamp returns an integer comparing two Timestamps, where T is compared first, followed by I.
155
// Returns 0 if tp = tp2, 1 if tp > tp2, -1 if tp < tp2.
156
func CompareTimestamp(tp, tp2 Timestamp) int {
157
	if tp.Equal(tp2) {
158
		return 0
159
	}
160

161
	if tp.T > tp2.T {
162
		return 1
163
	}
164
	if tp.T < tp2.T {
165
		return -1
166
	}
167
	// Compare I values because T values are equal
168
	if tp.I > tp2.I {
169
		return 1
170
	}
171
	return -1
172
}
173

174
// MinKey represents the BSON minkey value.
175
type MinKey struct{}
176

177
// MaxKey represents the BSON maxkey value.
178
type MaxKey struct{}
179

180
// D is an ordered representation of a BSON document. This type should be used when the order of the elements matters,
181
// such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead.
182
//
183
// Example usage:
184
//
185
//	bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
186
type D []E
187

188
// Map creates a map from the elements of the D.
189
func (d D) Map() M {
190
	m := make(M, len(d))
191
	for _, e := range d {
192
		m[e.Key] = e.Value
193
	}
194
	return m
195
}
196

197
// E represents a BSON element for a D. It is usually used inside a D.
198
type E struct {
199
	Key   string
200
	Value interface{}
201
}
202

203
// M is an unordered representation of a BSON document. This type should be used when the order of the elements does not
204
// matter. This type is handled as a regular map[string]interface{} when encoding and decoding. Elements will be
205
// serialized in an undefined, random order. If the order of the elements matters, a D should be used instead.
206
//
207
// Example usage:
208
//
209
//	bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}
210
type M map[string]interface{}
211

212
// An A is an ordered representation of a BSON array.
213
//
214
// Example usage:
215
//
216
//	bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}}
217
type A []interface{}
218

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

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

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

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