Documentation ¶
Overview ¶
Package state provides functionality related to saving and loading object graphs. For most types, it provides a set of default saving / loading logic that will be invoked automatically if custom logic is not defined.
Kind Support ---- ------- Bool default Int default Int8 default Int16 default Int32 default Int64 default Uint default Uint8 default Uint16 default Uint32 default Uint64 default Float32 default Float64 default Complex64 custom Complex128 custom Array default Chan custom Func custom Interface custom Map default (*) Ptr default Slice default String default Struct custom UnsafePointer custom
(*) Maps are treated as value types by this package, even if they are pointers internally. If you want to save two independent references to the same map value, you must explicitly use a pointer to a map.
Index ¶
- func IsZeroValue(val interface{}) bool
- func Load(r io.Reader, rootPtr interface{}, stats *Stats) error
- func PrettyPrint(w io.Writer, r io.Reader, html bool) error
- func ReadHeader(r io.Reader) (length uint64, object bool, err error)
- func Register(name string, instance interface{}, fns Fns)
- func Save(w io.Writer, rootPtr interface{}, stats *Stats) error
- func WriteHeader(w io.Writer, length uint64, object bool) error
- type ErrState
- type Fns
- type Map
- func (m Map) AfterLoad(fn func())
- func (m Map) Failf(format string, args ...interface{})
- func (m Map) Load(name string, objPtr interface{})
- func (m Map) LoadValue(name string, objPtr interface{}, fn func(interface{}))
- func (m Map) LoadWait(name string, objPtr interface{})
- func (m Map) Save(name string, objPtr interface{})
- func (m Map) SaveValue(name string, obj interface{})
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsZeroValue ¶
func IsZeroValue(val interface{}) bool
IsZeroValue checks if the given value is the zero value.
This function is used by the stateify tool.
func PrettyPrint ¶
PrettyPrint reads the state stream from r, and pretty prints to w.
func ReadHeader ¶
ReadHeader reads an object header.
Each object written to the statefile is prefixed with a header. See WriteHeader for more information; these functions are exported to allow non-state writes to the file to play nice with debugging tools.
func Register ¶
Register must be called for any interface implementation types that implements Loader.
Register should be called either immediately after startup or via init() methods. Double registration of either names or types will result in a panic.
No synchronization is provided; this should only be called in init.
Example usage:
state.Register("Foo", (*Foo)(nil), state.Fns{ Save: (*Foo).Save, Load: (*Foo).Load, })
func WriteHeader ¶
WriteHeader writes a header.
Each object written to the statefile should be prefixed with a header. In order to generate statefiles that play nicely with debugging tools, raw writes should be prefixed with a header with object set to false and the appropriate length. This will allow tools to skip these regions.
Types ¶
type ErrState ¶
type ErrState struct { // Err is the underlying error. Err error // contains filtered or unexported fields }
ErrState is returned when an error is encountered during encode/decode.
type Fns ¶
type Fns struct { // Save is a function like Save(concreteType, Map). Save interface{} // Load is a function like Load(concreteType, Map). Load interface{} }
Fns are the state dispatch functions.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is a generic state container.
This is the object passed to Save and Load in order to store their state.
Detailed documentation is available in individual methods.
func (Map) AfterLoad ¶
func (m Map) AfterLoad(fn func())
AfterLoad schedules a function execution when all objects have been allocated and their automated loading and customized load logic have been executed. fn will not be executed until all of current object's dependencies' AfterLoad() logic, if exist, have been executed.
func (Map) Failf ¶
Failf fails the save or restore with the provided message. Processing will stop after calling Failf, as the state package uses a panic & recover mechanism for state errors. You should defer any cleanup required.
func (Map) LoadValue ¶
LoadValue loads the given object value from the map.
See SaveValue for an example.
func (Map) LoadWait ¶
LoadWait loads the given objects from the map, and marks it as requiring all AfterLoad executions to complete prior to running this object's AfterLoad.
See Save for an example.
func (Map) Save ¶
Save adds the given object to the map.
You should pass always pointers to the object you are saving. For example:
type X struct { A int B *int }
func (x *X) Save(m Map) { m.Save("A", &x.A) m.Save("B", &x.B) }
func (x *X) Load(m Map) { m.Load("A", &x.A) m.Load("B", &x.B) }
func (Map) SaveValue ¶
SaveValue adds the given object value to the map.
This should be used for values where pointers are not available, or casts are required during Save/Load.
For example, if we want to cast external package type P.Foo to int64:
type X struct { A P.Foo }
func (x *X) Save(m Map) { m.SaveValue("A", int64(x.A)) }
func (x *X) Load(m Map) { m.LoadValue("A", new(int64), func(x interface{}) { x.A = P.Foo(x.(int64)) }) }