Documentation ¶
Overview ¶
Package document defines types to manipulate and compare documents and values.
Index ¶
- func ArrayContains(a types.Array, v types.Value) (bool, error)
- func ArrayLength(a types.Array) (int, error)
- func CastAs(v types.Value, t types.ValueType) (types.Value, error)
- func CastAsArray(v types.Value) (types.Value, error)
- func CastAsBlob(v types.Value) (types.Value, error)
- func CastAsBool(v types.Value) (types.Value, error)
- func CastAsDocument(v types.Value) (types.Value, error)
- func CastAsDouble(v types.Value) (types.Value, error)
- func CastAsInteger(v types.Value) (types.Value, error)
- func CastAsText(v types.Value) (types.Value, error)
- func CloneValue(v types.Value) (types.Value, error)
- func Length(d types.Document) (int, error)
- func MapScan(d types.Document, t interface{}) error
- func MarshalJSON(d types.Document) ([]byte, error)
- func MarshalJSONArray(a types.Array) ([]byte, error)
- func MaskFields(d types.Document, fields ...string) types.Document
- func NewFromCSV(headers, columns []string) types.Document
- func NewFromJSON(data []byte) types.Document
- func NewFromMap[T any](m map[string]T) types.Document
- func NewFromStruct(s interface{}) (types.Document, error)
- func NewValue(x interface{}) (types.Value, error)
- func OnlyFields(d types.Document, fields ...string) types.Document
- func Scan(d types.Document, targets ...interface{}) error
- func ScanDocument(d types.Document, t interface{}) error
- func ScanField(d types.Document, field string, dest interface{}) error
- func ScanIterator(it Iterator, t interface{}) error
- func ScanPath(d types.Document, path Path, dest interface{}) error
- func ScanValue(v types.Value, t interface{}) error
- func SliceScan(a types.Array, t interface{}) error
- func StructScan(d types.Document, t interface{}) error
- func WithSortedFields(d types.Document) types.Document
- type ErrUnsupportedType
- type FieldBuffer
- func (fb *FieldBuffer) Add(field string, v types.Value) *FieldBuffer
- func (fb *FieldBuffer) Apply(fn func(p Path, v types.Value) (types.Value, error)) error
- func (fb *FieldBuffer) Copy(d types.Document) error
- func (fb *FieldBuffer) Delete(path Path) error
- func (fb FieldBuffer) GetByField(field string) (types.Value, error)
- func (fb FieldBuffer) Iterate(fn func(field string, value types.Value) error) error
- func (fb FieldBuffer) Len() int
- func (fb *FieldBuffer) MarshalJSON() ([]byte, error)
- func (fb *FieldBuffer) Replace(field string, v types.Value) error
- func (fb *FieldBuffer) Reset()
- func (fb *FieldBuffer) ScanDocument(d types.Document) error
- func (fb *FieldBuffer) Set(path Path, v types.Value) error
- func (fb *FieldBuffer) String() string
- func (fb *FieldBuffer) UnmarshalJSON(data []byte) error
- type Iterator
- type Op
- type Path
- func (p Path) Clone() Path
- func (p Path) Extend(f ...PathFragment) Path
- func (p Path) ExtendField(field string) Path
- func (p Path) ExtendIndex(index int) Path
- func (p Path) GetValueFromArray(a types.Array) (types.Value, error)
- func (p Path) GetValueFromDocument(d types.Document) (types.Value, error)
- func (p Path) IsEqual(other Path) bool
- func (p Path) String() string
- type PathFragment
- type Paths
- type Scanner
- type ValueBuffer
- func (vb *ValueBuffer) Append(v types.Value) *ValueBuffer
- func (vb *ValueBuffer) Apply(fn func(p Path, v types.Value) (types.Value, error)) error
- func (vb *ValueBuffer) Copy(a types.Array) error
- func (vb *ValueBuffer) GetByIndex(i int) (types.Value, error)
- func (vb *ValueBuffer) Iterate(fn func(i int, value types.Value) error) error
- func (vb *ValueBuffer) Len() int
- func (vb ValueBuffer) MarshalJSON() ([]byte, error)
- func (vb *ValueBuffer) Replace(index int, v types.Value) error
- func (vb *ValueBuffer) ScanArray(a types.Array) error
- func (vb *ValueBuffer) UnmarshalJSON(data []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArrayContains ¶
ArrayContains iterates over a and returns whether v is equal to one of its values.
func ArrayLength ¶
ArrayLength returns the length of an array.
func CastAsArray ¶ added in v0.13.0
CastAsArray casts according to the following rules: Text: decodes a JSON array, otherwise fails. Any other type is considered an invalid cast.
func CastAsBlob ¶ added in v0.13.0
CastAsBlob casts according to the following rules: Text: decodes a base64 string, otherwise fails. Any other type is considered an invalid cast.
func CastAsBool ¶ added in v0.13.0
CastAsBool casts according to the following rules: Integer: true if truthy, otherwise false. Text: uses strconv.Parsebool to determine the boolean value, it fails if the text doesn't contain a valid boolean. Any other type is considered an invalid cast.
func CastAsDocument ¶ added in v0.13.0
CastAsDocument casts according to the following rules: Text: decodes a JSON object, otherwise fails. Any other type is considered an invalid cast.
func CastAsDouble ¶ added in v0.13.0
CastAsDouble casts according to the following rules: Integer: returns a double version of the integer. Text: uses strconv.ParseFloat to determine the double value, it fails if the text doesn't contain a valid float value. Any other type is considered an invalid cast.
func CastAsInteger ¶ added in v0.13.0
CastAsInteger casts according to the following rules: Bool: returns 1 if true, 0 if false. Double: cuts off the decimal and remaining numbers. Text: uses strconv.ParseInt to determine the integer value, then casts it to an integer. If it fails uses strconv.ParseFloat to determine the double value, then casts it to an integer It fails if the text doesn't contain a valid float value. Any other type is considered an invalid cast.
func CastAsText ¶ added in v0.13.0
CastAsText returns a JSON representation of v. If the representation is a string, it gets unquoted.
func MarshalJSON ¶ added in v0.8.0
MarshalJSON encodes a document to json.
func MarshalJSONArray ¶ added in v0.8.0
MarshalJSONArray encodes an array to json.
func MaskFields ¶ added in v0.15.0
MaskFields returns a new document that masks the given fields.
func NewFromCSV ¶ added in v0.13.0
NewFromCSV takes a list of headers and columns and returns a document. Each header will be assigned as the key and each corresponding column as a text value. The length of headers and columns must be the same.
func NewFromJSON ¶
NewFromJSON creates a document from raw JSON data. The returned document will lazily decode the data. If data is not a valid json object, calls to Iterate or GetByField will return an error.
func NewFromMap ¶
NewFromMap creates a document from a map. Due to the way maps are designed, iteration order is not guaranteed.
func NewFromStruct ¶
NewFromStruct creates a document from a struct using reflection.
func OnlyFields ¶ added in v0.15.0
OnlyFields returns a new document that only contains the given fields.
func ScanDocument ¶ added in v0.13.0
ScanDocument scans a document into dest which must be either a struct pointer, a map or a map pointer.
func ScanIterator ¶ added in v0.13.0
ScanIterator scans a document iterator into a slice or fixed size array. t must be a pointer to a valid slice or array.
It t is a slice pointer and its capacity is too low, a new slice will be allocated. Otherwise, its length is set to 0 so that its content is overwritten.
If t is an array pointer, its capacity must be bigger than the length of a, otherwise an error is returned.
func SliceScan ¶
SliceScan scans a document array into a slice or fixed size array. t must be a pointer to a valid slice or array.
It t is a slice pointer and its capacity is too low, a new slice will be allocated. Otherwise, its length is set to 0 so that its content is overwritten.
If t is an array pointer, its capacity must be bigger than the length of a, otherwise an error is returned.
func StructScan ¶
StructScan scans d into t. t is expected to be a pointer to a struct.
By default, each struct field name is lowercased and the document's GetByField method is called with that name. If there is a match, the value is converted to the struct field type when possible, otherwise an error is returned. The decoding of each struct field can be customized by the format string stored under the "genji" key stored in the struct field's tag. The content of the format string is used instead of the struct field name and passed to the GetByField method.
Types ¶
type ErrUnsupportedType ¶
type ErrUnsupportedType struct { Value interface{} Msg string }
ErrUnsupportedType is used to skip struct or array fields that are not supported.
func (*ErrUnsupportedType) Error ¶
func (e *ErrUnsupportedType) Error() string
type FieldBuffer ¶
type FieldBuffer struct {
// contains filtered or unexported fields
}
FieldBuffer stores a group of fields in memory. It implements the Document interface.
func (*FieldBuffer) Add ¶
func (fb *FieldBuffer) Add(field string, v types.Value) *FieldBuffer
Add a field to the buffer.
func (*FieldBuffer) Copy ¶
func (fb *FieldBuffer) Copy(d types.Document) error
Copy deep copies every value of the document to the buffer. If a value is a document or an array, it will be stored as a FieldBuffer or ValueBuffer respectively.
func (*FieldBuffer) Delete ¶
func (fb *FieldBuffer) Delete(path Path) error
Delete a field from the buffer.
func (FieldBuffer) GetByField ¶
func (fb FieldBuffer) GetByField(field string) (types.Value, error)
GetByField returns a value by field. Returns an error if the field doesn't exists.
func (FieldBuffer) Iterate ¶
Iterate goes through all the fields of the document and calls the given function by passing each one of them. If the given function returns an error, the iteration stops.
func (*FieldBuffer) MarshalJSON ¶
func (fb *FieldBuffer) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
func (*FieldBuffer) Replace ¶
func (fb *FieldBuffer) Replace(field string, v types.Value) error
Replace the value of the field by v.
func (*FieldBuffer) ScanDocument ¶
func (fb *FieldBuffer) ScanDocument(d types.Document) error
ScanDocument copies all the fields of d to the buffer.
func (*FieldBuffer) Set ¶
func (fb *FieldBuffer) Set(path Path, v types.Value) error
Set replaces a field if it already exists or creates one if not. TODO(asdine): Set should always fail with types.ErrFieldNotFound if the path doesn't resolve to an existing field.
func (*FieldBuffer) String ¶ added in v0.12.0
func (fb *FieldBuffer) String() string
func (*FieldBuffer) UnmarshalJSON ¶
func (fb *FieldBuffer) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json.Unmarshaler interface.
type Iterator ¶
type Iterator interface { // Iterate goes through all the documents and calls the given function by passing each one of them. // If the given function returns an error, the iteration stops. Iterate(fn func(d types.Document) error) error }
An Iterator can iterate over documents.
type Op ¶ added in v0.15.0
Op represents a single operation on a document. It is returned by the Diff function.
func Diff ¶ added in v0.15.0
Diff returns the operations needed to transform the first document into the second.
func (*Op) MarshalBinary ¶ added in v0.15.0
type Path ¶ added in v0.9.0
type Path []PathFragment
A Path represents the path to a particular value within a document.
func NewPath ¶ added in v0.10.0
NewPath creates a path from a list of strings representing either a field name or an array index in string form.
func (Path) Extend ¶ added in v0.15.0
func (p Path) Extend(f ...PathFragment) Path
Extend clones the path and appends the fragment to it.
func (Path) ExtendField ¶ added in v0.15.0
Extend clones the path and appends the field to it.
func (Path) ExtendIndex ¶ added in v0.15.0
Extend clones the path and appends the array index to it.
func (Path) GetValueFromArray ¶ added in v0.10.0
GetValueFromArray returns the value at path p from a.
func (Path) GetValueFromDocument ¶ added in v0.10.0
GetValueFromDocument returns the value at path p from d.
type PathFragment ¶ added in v0.9.0
PathFragment is a fragment of a path representing either a field name or the index of an array.
type Paths ¶ added in v0.14.0
type Paths []Path
type ValueBuffer ¶
ValueBuffer is an array that holds values in memory.
func NewValueBuffer ¶
func NewValueBuffer(values ...types.Value) *ValueBuffer
NewValueBuffer creates a buffer of values.
func (*ValueBuffer) Append ¶
func (vb *ValueBuffer) Append(v types.Value) *ValueBuffer
Append a value to the buffer and return a new buffer.
func (*ValueBuffer) Copy ¶
func (vb *ValueBuffer) Copy(a types.Array) error
Copy deep copies all the values from the given array. If a value is a document or an array, it will be stored as a *FieldBuffer or *ValueBuffer respectively.
func (*ValueBuffer) GetByIndex ¶
func (vb *ValueBuffer) GetByIndex(i int) (types.Value, error)
GetByIndex returns a value set at the given index. If the index is out of range it returns an error.
func (*ValueBuffer) Iterate ¶
Iterate over all the values of the buffer. It implements the Array interface.
func (*ValueBuffer) Len ¶ added in v0.10.0
func (vb *ValueBuffer) Len() int
Len returns the length the of array
func (ValueBuffer) MarshalJSON ¶
func (vb ValueBuffer) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
func (*ValueBuffer) Replace ¶
func (vb *ValueBuffer) Replace(index int, v types.Value) error
Replace the value of the index by v.
func (*ValueBuffer) ScanArray ¶
func (vb *ValueBuffer) ScanArray(a types.Array) error
ScanArray copies all the values of a to the buffer.
func (*ValueBuffer) UnmarshalJSON ¶
func (vb *ValueBuffer) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json.Unmarshaler interface.