object

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package object defines types to manipulate and compare objects and values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayContains

func ArrayContains(a types.Array, v types.Value) (bool, error)

ArrayContains iterates over a and returns whether v is equal to one of its values.

func ArrayLength

func ArrayLength(a types.Array) (int, error)

ArrayLength returns the length of an array.

func CastAs

func CastAs(v types.Value, t types.ValueType) (types.Value, error)

CastAs casts v as the selected type when possible.

func CastAsArray

func CastAsArray(v types.Value) (types.Value, error)

CastAsArray casts according to the following rules: Text: decodes a JSON array, otherwise fails. Any other type is considered an invalid cast.

func CastAsBlob

func CastAsBlob(v types.Value) (types.Value, error)

CastAsBlob casts according to the following rules: Text: decodes a base64 string, otherwise fails. Any other type is considered an invalid cast.

func CastAsBool

func CastAsBool(v types.Value) (types.Value, error)

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 CastAsDouble

func CastAsDouble(v types.Value) (types.Value, error)

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

func CastAsInteger(v types.Value) (types.Value, error)

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 CastAsObject

func CastAsObject(v types.Value) (types.Value, error)

CastAsObject casts according to the following rules: Text: decodes a JSON object, otherwise fails. Any other type is considered an invalid cast.

func CastAsText

func CastAsText(v types.Value) (types.Value, error)

CastAsText returns a JSON representation of v. If the representation is a string, it gets unquoted.

func CastAsTimestamp

func CastAsTimestamp(v types.Value) (types.Value, error)

CastAsTimestamp casts according to the following rules: Text: uses carbon.Parse to determine the timestamp value it fails if the text doesn't contain a valid timestamp. Any other type is considered an invalid cast.

func CloneValue

func CloneValue(v types.Value) (types.Value, error)

func Length

func Length(d types.Object) (int, error)

Length returns the length of an object.

func MapScan

func MapScan(d types.Object, t any) error

MapScan decodes the object into a map.

func MarshalJSON

func MarshalJSON(d types.Object) ([]byte, error)

MarshalJSON encodes an object to json.

func MarshalJSONArray

func MarshalJSONArray(a types.Array) ([]byte, error)

MarshalJSONArray encodes an array to json.

func MaskFields

func MaskFields(d types.Object, fields ...string) types.Object

MaskFields returns a new object that masks the given fields.

func NewArrayFromSlice

func NewArrayFromSlice[T any](l []T) types.Array

func NewFromCSV

func NewFromCSV(headers, columns []string) types.Object

NewFromCSV takes a list of headers and columns and returns an object. 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

func NewFromJSON(data []byte) types.Object

NewFromJSON creates an object from raw JSON data. The returned object will lazily decode the data. If data is not a valid json object, calls to Iterate or GetByField will return an error.

func NewFromMap

func NewFromMap[T any](m map[string]T) types.Object

NewFromMap creates an object from a map. Due to the way maps are designed, iteration order is not guaranteed.

func NewFromStruct

func NewFromStruct(s interface{}) (types.Object, error)

NewFromStruct creates an object from a struct using reflection.

func NewValue

func NewValue(x any) (types.Value, error)

NewValue creates a value whose type is infered from x.

func OnlyFields

func OnlyFields(d types.Object, fields ...string) types.Object

OnlyFields returns a new object that only contains the given fields.

func Scan

func Scan(d types.Object, targets ...interface{}) error

Scan each field of the object into the given variables.

func ScanField

func ScanField(d types.Object, field string, dest interface{}) error

ScanField scans a single field into dest.

func ScanIterator

func ScanIterator(it Iterator, t interface{}) error

ScanIterator scans a row 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 ScanPath

func ScanPath(d types.Object, path Path, dest interface{}) error

ScanPath scans a single path into dest.

func ScanRow

func ScanRow(d types.Object, t interface{}) error

ScanRow scans a row into dest which must be either a struct pointer, a map or a map pointer.

func ScanValue

func ScanValue(v types.Value, t any) error

ScanValue scans v into t.

func SliceScan

func SliceScan(a types.Array, t interface{}) error

SliceScan scans an 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

func StructScan(d types.Object, t interface{}) error

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 object'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 "chai" 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.

func WithSortedFields

func WithSortedFields(d types.Object) types.Object

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 object interface.

func NewFieldBuffer

func NewFieldBuffer() *FieldBuffer

NewFieldBuffer creates a FieldBuffer.

func (*FieldBuffer) Add

func (fb *FieldBuffer) Add(field string, v types.Value) *FieldBuffer

Add a field to the buffer.

func (*FieldBuffer) Apply

func (fb *FieldBuffer) Apply(fn func(p Path, v types.Value) (types.Value, error)) error

Apply a function to all the values of the buffer.

func (*FieldBuffer) Copy

func (fb *FieldBuffer) Copy(d types.Object) error

Copy deep copies every value of the object to the buffer. If a value is an object 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

func (fb FieldBuffer) Iterate(fn func(field string, value types.Value) error) error

Iterate goes through all the fields of the object and calls the given function by passing each one of them. If the given function returns an error, the iteration stops.

func (FieldBuffer) Len

func (fb FieldBuffer) Len() int

Len of the buffer.

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) Reset

func (fb *FieldBuffer) Reset()

Reset the buffer.

func (*FieldBuffer) ScanCSV

func (fb *FieldBuffer) ScanCSV(headers, columns []string)

func (*FieldBuffer) ScanObject

func (fb *FieldBuffer) ScanObject(d types.Object) error

ScanObject 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

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 objects 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.Object) error) error
}

An Iterator can iterate over object keys.

type Op

type Op struct {
	Type  string
	Path  Path
	Value types.Value
}

Op represents a single operation on an object. It is returned by the Diff function.

func Diff

func Diff(d1, d2 types.Object) ([]Op, error)

Diff returns the operations needed to transform the first object into the second.

func NewDeleteOp

func NewDeleteOp(path Path, v types.Value) Op

func NewSetOp

func NewSetOp(path Path, v types.Value) Op

func (*Op) MarshalBinary

func (o *Op) MarshalBinary() ([]byte, error)

type Path

type Path []PathFragment

A Path represents the path to a particular value within an object.

func NewPath

func NewPath(fragments ...string) Path

NewPath creates a path from a list of strings representing either a field name or an array index in string form.

func (Path) Clone

func (p Path) Clone() Path

func (Path) Extend

func (p Path) Extend(f ...PathFragment) Path

Extend clones the path and appends the fragment to it.

func (Path) ExtendField

func (p Path) ExtendField(field string) Path

Extend clones the path and appends the field to it.

func (Path) ExtendIndex

func (p Path) ExtendIndex(index int) Path

Extend clones the path and appends the array index to it.

func (Path) GetValueFromArray

func (p Path) GetValueFromArray(a types.Array) (types.Value, error)

GetValueFromArray returns the value at path p from a.

func (Path) GetValueFromObject

func (p Path) GetValueFromObject(d types.Object) (types.Value, error)

GetValueFromObject returns the value at path p from d.

func (Path) IsEqual

func (p Path) IsEqual(other Path) bool

IsEqual returns whether other is equal to p.

func (Path) String

func (p Path) String() string

String representation of all the fragments of the path. It implements the Stringer interface.

type PathFragment

type PathFragment struct {
	FieldName  string
	ArrayIndex int
}

PathFragment is a fragment of a path representing either a field name or the index of an array.

type Paths

type Paths []Path

func (Paths) IsEqual

func (p Paths) IsEqual(other Paths) bool

IsEqual returns whether other is equal to p.

func (Paths) String

func (p Paths) String() string

type Scanner

type Scanner interface {
	ScanObject(types.Object) error
}

A Scanner can iterate over an object and scan all the fields.

type ValueBuffer

type ValueBuffer struct {
	Values []types.Value
}

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) Apply

func (vb *ValueBuffer) Apply(fn func(p Path, v types.Value) (types.Value, error)) error

Apply a function to all the values of the 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 an object 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

func (vb *ValueBuffer) Iterate(fn func(i int, value types.Value) error) error

Iterate over all the values of the buffer. It implements the Array interface.

func (*ValueBuffer) Len

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) Reset

func (vb *ValueBuffer) Reset()

Reset the buffer.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL