Documentation
¶
Overview ¶
Example ¶
type myType struct {
Data []string
}
data := &myType{Data: []string{"Hello", "World!"}}
// Create the GOB serialisation Approach, including its own TypeRegistry
testRegistry := NewTypeRegistry()
testRegistry.AddTypeOf(data)
approach := NewGOBApproach(func(o *TypeRegistryOptions) { o.Registry = testRegistry })
// Register the Approach so that it can be retrieved later
serialise.RegisterApproach(approach)
// Serialise the data using the default version of MinData serialisation
b, name, _ := serialise.ToBytes(data, serialise.WithSerialisationApproach(approach))
// Retrieve the Approach used for serialisation, from the returned name
approach, _ = serialise.GetApproach(name)
// Deserialise
v, _ := serialise.FromBytes(b, approach)
fmt.Println(strings.Join(data.Data, " ") == strings.Join(v.(*myType).Data, " "))
Output: true
Index ¶
- Variables
- func CreateInstance(name string, opts ...func(*TypeRegistryOptions)) (any, error)
- func CreateInstancePtr(name string, opts ...func(*TypeRegistryOptions)) (any, error)
- func GetRegisteredType(name string, opts ...func(*TypeRegistryOptions)) (reflect.Type, error)
- func NewGOBApproach(opts ...func(opt *TypeRegistryOptions)) serialise.Approach
- func NewGOBApproachWithVersion(version GOBVersion, opts ...func(opt *TypeRegistryOptions)) serialise.Approach
- func RegisterType(v any, opts ...func(*TypeRegistryOptions)) error
- type GOBVersion
- type TypeRegistry
- type TypeRegistryOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrCannotRegisterNilType = errors.New("variable must not be nil in call to RegisterType")
ErrCannotRegisterNilType raised if nil passed to RegisterType
var ErrNoDeserialisableData = errors.New("no data found to deserialise")
ErrNoDeserialisableData raised when GOB serialisation approach has value data to deserialise
var ErrNoGobData = errors.New("no data provided to deserialise")
ErrNoGobData raised when GOB serialisation approach has no data to deserialise
var ErrNoRegistry = errors.New("no registry provided into which to register type")
ErrNoRegistry raised when there is no registry provided to operate on
var ErrUnknownTypeName = errors.New("requested type has not been registered")
ErrUnknownTypeName is raised if the requested type is not found in the registry
Functions ¶
func CreateInstance ¶
func CreateInstance(name string, opts ...func(*TypeRegistryOptions)) (any, error)
CreateInstance returns an instance of the type specified by the name
func CreateInstancePtr ¶
func CreateInstancePtr(name string, opts ...func(*TypeRegistryOptions)) (any, error)
CreateInstancePtr returns a pointer to an instance of the type specified by the name
func GetRegisteredType ¶
func GetRegisteredType(name string, opts ...func(*TypeRegistryOptions)) (reflect.Type, error)
GetRegisteredType returns an instance of the type specified by the name
func NewGOBApproach ¶
func NewGOBApproach(opts ...func(opt *TypeRegistryOptions)) serialise.Approach
NewGOBApproach creates an instance of the current default version of the GOB serialisation
func NewGOBApproachWithVersion ¶
func NewGOBApproachWithVersion(version GOBVersion, opts ...func(opt *TypeRegistryOptions)) serialise.Approach
NewGOBApproachWithVersion creates an Approach instance of the specified version, that uses gob serialisation.
func RegisterType ¶
func RegisterType(v any, opts ...func(*TypeRegistryOptions)) error
RegisterType allows registry of the type specified by the supplied value
Types ¶
type GOBVersion ¶
type GOBVersion int8
GOBVersion describes a version of a GOB serialisation implementation All breaking changes to serialisation will trigger an increment, to ensure backwards compatibility to any consumers of existing versions.
const ( UnknownVersion GOBVersion = iota V1 OutOfRange )
type TypeRegistry ¶
type TypeRegistry struct {
// contains filtered or unexported fields
}
TypeRegistry manages a map of types to their type name
func NewTypeRegistry ¶
func NewTypeRegistry() *TypeRegistry
NewTypeRegistry creates an instance of TypeRegistry
func (*TypeRegistry) AddTypeOf ¶
func (r *TypeRegistry) AddTypeOf(v any)
AddTypeOf adds the type of the value to the registry
type TypeRegistryOptions ¶
type TypeRegistryOptions struct {
Registry *TypeRegistry
}
TypeRegistryOptions allows overrides to the type registration behaviour