Documentation ¶
Overview ¶
Package data contains primitives for working with an object that stores data used in other parts of the library.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidType = errors.New("invalid type") ErrObjectNotFound = errors.New("object not found") )
Standard data package errors.
Functions ¶
func ToSlice ¶
ToSlice is an helper function with which you can wrap a call to Storage Search() and get a slice of the identical objects, without additional type conversion. The function will not compile in go versions smaller than 1.18. An example is in the file convert_test.go.
Types ¶
type Iterator ¶
type Iterator interface { // HasNext returns true if it is possible to get the next object from the // collection. HasNext() bool // Next returns the next object from the iterator. This object is already // unmarshaled and can be converted using type casting. Next() (Object, error) // Close should be called every time the collection is finished. This method // frees the iterator data in memory. Close() error }
Iterator is an interface that allows you to cycle through the objects in the repository one by one. To prevent memory leaks, you should close the iterator every time you finish working with the data.
type Key ¶
type Key string
Key is a unique name that is used to save and load an Object. At the same time as Key, the object type also takes part in the saving.
type Object ¶
type Object interface { encoding.BinaryMarshaler encoding.BinaryUnmarshaler // Clone should create an exact copy of the object, located in a different // memory location from the original. This is necessary in case of cache // optimization to avoid marshalling the object in some cases. Clone is also // used as a template for finding an object in the repository. Clone() Object // Instance should return a unique object type to share namespace between // the stored data. In the simplest case, you can return the type name via // InstanceOf, but keep in mind that you need to preserve compatibility or // provide for migration when refactoring. Instance() Type }
Object is an interface which is designed to store and load data. This interface must be implemented by all objects that interact and store their state in the HAL. You can see an example in object_test.go file.
type Prefix ¶
type Prefix string
Prefix is the part of Key by which objects in the repository can be searched and iterated.
type SliceIterator ¶
type SliceIterator struct {
// contains filtered or unexported fields
}
SliceIterator implements a simple iterator based on slice iteration. It can be used in various storage implementations and provides simple iteration mechanism.
func NewSliceIterator ¶
func NewSliceIterator(obj ...Object) *SliceIterator
NewSliceIterator creates a new object iterator based on slice iteration.
func (*SliceIterator) Close ¶
func (i *SliceIterator) Close() error
Close should be called every time the collection is finished. This method frees the iterator data in memory.
func (*SliceIterator) HasNext ¶
func (i *SliceIterator) HasNext() (out bool)
HasNext returns true if it is possible to get the next object from the collection.
func (*SliceIterator) Next ¶
func (i *SliceIterator) Next() (Object, error)
Next returns the next object from the iterator. This object is already unmarshaled and can be converted using type casting.
type Storage ¶
type Storage interface { // Save stores the object in a storage, using Key as the unique name (path) // to save the object and Type (by calling Instance method) to share the // namespace between objects. Key can be repeated for different Type values. // The MarshalBinary method can be called before the object is saved. Save(context.Context, Object, Key) error // Load loads an object from the repository, using Key as a unique name // (path) to find the object and Type (by calling the Instance method). The // UnmarshalBinary method can be called for the object. To restore an // object, you must allocate memory for it to avoid panic when working with // a nil address. // If the object is not found, an ErrObjectNotFound error will be returned. Load(context.Context, Object, Key) error // Search searches for objects in the repository of a certain Type. It // returns a list of found objects or ErrObjectNotFound if no objects // matching the criteria were found. Prefix is used for additional filtering // of objects according to their Key with partial match at the beginning of // the key. If you want to get all objects Prefix can be empty. The original // object is only needed as a template, other instances will be created // based on it (by method Copy), but the object itself will not be changed. Search(context.Context, Object, Prefix) ([]Object, error) // Iterate does what Search does, but in place of the finished collection of // objects an iterator will be returned that can be used to list the objects // one by one. The original object is only needed as a template, other // instances will be created based on it (by method Copy), but the object // itself will not be changed. Iterate(context.Context, Object, Prefix) (Iterator, error) }
Storage is a base interface that provides operations for storing, loading and retrieving objects of type Object in an abstract object repository. This storage should be implemented for testing, real-world application and further use in tokens.
type Type ¶
type Type string
Type is used as a unique name for a set of objects of the same type. It is intended to divide the namespace between the stored data. In general, a structure name obtained through the reflect package can be used, but when renaming a structure or refactoring code, the uniqueness of this value must be taken into account.
func InstanceOf ¶
func InstanceOf(v interface{}) Type
InstanceOf is a simple implementation of getting a unique type for the stored data. It works using the reflect package and returns the data type used as namespace.