Documentation
¶
Overview ¶
Package types provides Go types matching BSON types that don't have built-in Go equivalents.
All BSON types have three representations in FerretDB:
- As they are used in "business logic" / handlers - `types` package.
- As they are used in the wire protocol implementation - `bson` package.
- As they are used to store data in PostgreSQL - `fjson` package.
The reason for that is a separation of concerns: to avoid method names clashes, to simplify type asserts, to make refactorings and optimizations easier, etc.
Mapping ¶
Composite types (passed by pointers)
*types.Document *bson.Document *fjson.documentType Document *types.Array *bson.arrayType *fjson.arrayType Array
Scalar types (passed by values)
float64 *bson.doubleType *fjson.doubleType 64-bit binary floating point string *bson.stringType *fjson.stringType UTF-8 string types.Binary *bson.binaryType *fjson.binaryType Binary data types.ObjectID *bson.objectIDType *fjson.objectIDType ObjectId bool *bson.boolType *fjson.boolType Boolean time.Time *bson.dateTimeType *fjson.dateTimeType UTC datetime types.NullType *bson.nullType *fjson.nullType Null types.Regex *bson.regexType *fjson.regexType Regular expression int32 *bson.int32Type *fjson.int32Type 32-bit integer types.Timestamp *bson.timestampType *fjson.timestampType Timestamp int64 *bson.int64Type *fjson.int64Type 64-bit integer
Index ¶
- Constants
- Variables
- type Array
- type Binary
- type BinarySubtype
- type CompareResult
- type CompositeType
- type CompositeTypeInterface
- type Document
- func (d *Document) Command() string
- func (d *Document) DeepCopy() *Document
- func (d *Document) Get(key string) (any, error)
- func (d *Document) GetByPath(path ...string) (any, error)
- func (d *Document) Has(key string) bool
- func (d *Document) Keys() []string
- func (d *Document) Len() int
- func (d *Document) Map() map[string]any
- func (d *Document) Remove(key string)
- func (d *Document) RemoveByPath(keys ...string)
- func (d *Document) Set(key string, value any) error
- type NullType
- type ObjectID
- type Regex
- type ScalarType
- type Timestamp
- type Type
Constants ¶
const ( BinaryGeneric = BinarySubtype(0x00) // generic BinaryFunction = BinarySubtype(0x01) // function BinaryGenericOld = BinarySubtype(0x02) // generic-old BinaryUUIDOld = BinarySubtype(0x03) // uuid-old BinaryUUID = BinarySubtype(0x04) // uuid BinaryMD5 = BinarySubtype(0x05) // md5 BinaryEncrypted = BinarySubtype(0x06) // encrypted BinaryUser = BinarySubtype(0x80) // user )
const MaxDocumentLen = 16777216
Variables ¶
var Null = NullType{}
Null represents BSON value Null.
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array struct {
// contains filtered or unexported fields
}
Array represents BSON array.
Zero value is a valid empty array.
func MustNewArray
deprecated
added in
v0.0.5
func (*Array) GetByPath ¶ added in v0.0.5
GetByPath returns a value by path - a sequence of indexes and keys.
type Binary ¶
type Binary struct { Subtype BinarySubtype B []byte }
Binary represents BSON type Binary.
type BinarySubtype ¶
type BinarySubtype byte
BinarySubtype represents BSON Binary's subtype.
func (BinarySubtype) String ¶
func (i BinarySubtype) String() string
type CompareResult ¶ added in v0.2.0
type CompareResult int8
CompareResult represents the result of a comparison.
const ( Equal CompareResult = 0 // == Less CompareResult = -1 // < Greater CompareResult = 1 // > NotEqual CompareResult = 127 // != )
func (CompareResult) String ¶ added in v0.2.0
func (i CompareResult) String() string
type CompositeType ¶ added in v0.0.5
CompositeType represents composite type - *Document or *Array.
type CompositeTypeInterface ¶ added in v0.0.6
type CompositeTypeInterface interface { CompositeType GetByPath(path ...string) (any, error) // contains filtered or unexported methods }
TODO remove once we have go-sumtype equivalent?
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document represents BSON document.
Duplicate field names are not supported.
func ConvertDocument ¶
ConvertDocument converts bson.Document to *types.Document and validates it. It references the same data without copying it.
TODO Remove this function.
func MustConvertDocument
deprecated
func MustConvertDocument(d document) *Document
MustConvertDocument is a ConvertDocument that panics in case of error.
Deprecated: use `must.NotFail(ConvertDocument(...))` instead.
func MustNewDocument
deprecated
added in
v0.0.6
func NewDocument ¶ added in v0.0.6
NewDocument creates a document with the given key/value pairs.
func (*Document) Command ¶
Command returns the first document's key. This is often used as a command name. It returns an empty string if document is nil or empty.
func (*Document) GetByPath ¶ added in v0.0.5
GetByPath returns a value by path - a sequence of indexes and keys.
func (*Document) Has ¶ added in v0.1.1
Has returns true if the given key is present in the document.
func (*Document) Keys ¶
Keys returns document's keys. Do not modify it.
It returns nil for nil Document.
func (*Document) Len ¶ added in v0.0.6
Len returns the number of elements in the document.
It returns 0 for nil Document.
func (*Document) Map ¶
Map returns this document as a map. Do not modify it.
It returns nil for nil Document.
func (*Document) RemoveByPath ¶ added in v0.1.0
RemoveByPath removes document by path, doing nothing if the key does not exist.
type NullType ¶ added in v0.0.6
type NullType struct{}
NullType represents BSON type Null.
Most callers should use types.Null value instead.
type ObjectID ¶
type ObjectID [12]byte
ObjectID represents BSON type ObjectID.
Normally, it is generated by the driver, but in some cases (like upserts) FerretDB has to do itself.
type ScalarType ¶ added in v0.0.6
type ScalarType interface { float64 | string | Binary | ObjectID | bool | time.Time | NullType | Regex | int32 | Timestamp | int64 }
ScalarType represents scalar type.
type Type ¶ added in v0.0.6
type Type interface { ScalarType | CompositeType }
Type represents any BSON type (scalar or composite).