podman
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.
9package primitive // import "go.mongodb.org/mongo-driver/bson/primitive"
10
11import (
12"bytes"
13"encoding/json"
14"fmt"
15"time"
16)
17
18// Binary represents a BSON binary value.
19type Binary struct {
20Subtype byte
21Data []byte
22}
23
24// Equal compares bp to bp2 and returns true if they are equal.
25func (bp Binary) Equal(bp2 Binary) bool {
26if bp.Subtype != bp2.Subtype {
27return false
28}
29return bytes.Equal(bp.Data, bp2.Data)
30}
31
32// IsZero returns if bp is the empty Binary.
33func (bp Binary) IsZero() bool {
34return bp.Subtype == 0 && len(bp.Data) == 0
35}
36
37// Undefined represents the BSON undefined value type.
38type Undefined struct{}
39
40// DateTime represents the BSON datetime value.
41type DateTime int64
42
43var _ json.Marshaler = DateTime(0)
44var _ json.Unmarshaler = (*DateTime)(nil)
45
46// MarshalJSON marshal to time type.
47func (d DateTime) MarshalJSON() ([]byte, error) {
48return json.Marshal(d.Time())
49}
50
51// UnmarshalJSON creates a primitive.DateTime from a JSON string.
52func (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.
56if string(data) == "null" {
57return nil
58}
59
60var tempTime time.Time
61if err := json.Unmarshal(data, &tempTime); err != nil {
62return err
63}
64
65*d = NewDateTimeFromTime(tempTime)
66return nil
67}
68
69// Time returns the date as a time type.
70func (d DateTime) Time() time.Time {
71return time.Unix(int64(d)/1000, int64(d)%1000*1000000)
72}
73
74// NewDateTimeFromTime creates a new DateTime from a Time.
75func NewDateTimeFromTime(t time.Time) DateTime {
76return DateTime(t.Unix()*1e3 + int64(t.Nanosecond())/1e6)
77}
78
79// Null represents the BSON null value.
80type Null struct{}
81
82// Regex represents a BSON regex value.
83type Regex struct {
84Pattern string
85Options string
86}
87
88func (rp Regex) String() string {
89return 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.
93func (rp Regex) Equal(rp2 Regex) bool {
94return rp.Pattern == rp2.Pattern && rp.Options == rp2.Options
95}
96
97// IsZero returns if rp is the empty Regex.
98func (rp Regex) IsZero() bool {
99return rp.Pattern == "" && rp.Options == ""
100}
101
102// DBPointer represents a BSON dbpointer value.
103type DBPointer struct {
104DB string
105Pointer ObjectID
106}
107
108func (d DBPointer) String() string {
109return 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.
113func (d DBPointer) Equal(d2 DBPointer) bool {
114return d == d2
115}
116
117// IsZero returns if d is the empty DBPointer.
118func (d DBPointer) IsZero() bool {
119return d.DB == "" && d.Pointer.IsZero()
120}
121
122// JavaScript represents a BSON JavaScript code value.
123type JavaScript string
124
125// Symbol represents a BSON symbol value.
126type Symbol string
127
128// CodeWithScope represents a BSON JavaScript code with scope value.
129type CodeWithScope struct {
130Code JavaScript
131Scope interface{}
132}
133
134func (cws CodeWithScope) String() string {
135return fmt.Sprintf(`{"code": "%s", "scope": %v}`, cws.Code, cws.Scope)
136}
137
138// Timestamp represents a BSON timestamp value.
139type Timestamp struct {
140T uint32
141I uint32
142}
143
144// Equal compares tp to tp2 and returns true if they are equal.
145func (tp Timestamp) Equal(tp2 Timestamp) bool {
146return tp.T == tp2.T && tp.I == tp2.I
147}
148
149// IsZero returns if tp is the zero Timestamp.
150func (tp Timestamp) IsZero() bool {
151return 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.
156func CompareTimestamp(tp, tp2 Timestamp) int {
157if tp.Equal(tp2) {
158return 0
159}
160
161if tp.T > tp2.T {
162return 1
163}
164if tp.T < tp2.T {
165return -1
166}
167// Compare I values because T values are equal
168if tp.I > tp2.I {
169return 1
170}
171return -1
172}
173
174// MinKey represents the BSON minkey value.
175type MinKey struct{}
176
177// MaxKey represents the BSON maxkey value.
178type 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}}
186type D []E
187
188// Map creates a map from the elements of the D.
189func (d D) Map() M {
190m := make(M, len(d))
191for _, e := range d {
192m[e.Key] = e.Value
193}
194return m
195}
196
197// E represents a BSON element for a D. It is usually used inside a D.
198type E struct {
199Key string
200Value 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}
210type 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}}}
217type A []interface{}
218