sgob

package module
v0.0.0-...-c78eb94 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2025 License: MIT Imports: 8 Imported by: 0

README

Documentation

SGOB | GOB based serialisation

Extends MinData serialisation (from github.com/gford1000-go/serialise) to support pre-registered types using gob.

This is an example extension of the Approach interface.

Supports optional encryption of the byte slice using aes-gcm.

func main() {
    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)
}

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

Examples

Constants

This section is empty.

Variables

View Source
var ErrCannotRegisterNilType = errors.New("variable must not be nil in call to RegisterType")

ErrCannotRegisterNilType raised if nil passed to RegisterType

View Source
var ErrNoDeserialisableData = errors.New("no data found to deserialise")

ErrNoDeserialisableData raised when GOB serialisation approach has value data to deserialise

View Source
var ErrNoGobData = errors.New("no data provided to deserialise")

ErrNoGobData raised when GOB serialisation approach has no data to deserialise

View Source
var ErrNoRegistry = errors.New("no registry provided into which to register type")

ErrNoRegistry raised when there is no registry provided to operate on

View Source
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

func (*TypeRegistry) GetType

func (r *TypeRegistry) GetType(name string) (reflect.Type, error)

GetType returns the registered type for the specified name

type TypeRegistryOptions

type TypeRegistryOptions struct {
	Registry *TypeRegistry
}

TypeRegistryOptions allows overrides to the type registration behaviour

Jump to

Keyboard shortcuts

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