persist

package
v0.0.0-...-ffc4fba Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: Apache-2.0, Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package persist provides codec-based file persistence for arbitrary state types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadState

func LoadState(dir, basename string, codec Codec, state any) error

LoadState loads state from a file in the specified directory. The filename is constructed from the basename and the codec's extension. The state parameter must be a pointer to the target struct.

func SaveState

func SaveState(dir, basename string, codec Codec, state any) error

SaveState saves the given state to a file in the specified directory. The filename is constructed from the basename and the codec's extension.

Types

type Codec

type Codec interface {
	// Encode writes the state to the writer.
	Encode(w io.Writer, state any) error
	// Decode reads the state from the reader.
	Decode(r io.Reader, state any) error
	// Extension returns the file extension for this codec (e.g., ".json", ".gob").
	Extension() string
}

Codec defines how state is serialized and deserialized.

type GobCodec

type GobCodec struct{}

GobCodec implements Codec using gob encoding.

func NewGobCodec

func NewGobCodec() *GobCodec

NewGobCodec creates a gob codec.

func (*GobCodec) Decode

func (c *GobCodec) Decode(r io.Reader, state any) error

Decode implements Codec.Decode using gob decoding.

func (*GobCodec) Encode

func (c *GobCodec) Encode(w io.Writer, state any) error

Encode implements Codec.Encode using gob encoding.

func (*GobCodec) Extension

func (c *GobCodec) Extension() string

Extension implements Codec.Extension for gob files.

type JSONCodec

type JSONCodec struct {
	// Indent specifies the indentation string. Empty string means compact JSON.
	Indent string
}

JSONCodec implements Codec using JSON encoding with optional indentation.

func NewJSONCodec

func NewJSONCodec() *JSONCodec

NewJSONCodec creates a JSON codec with pretty-printing (2-space indent).

func (*JSONCodec) Decode

func (c *JSONCodec) Decode(r io.Reader, state any) error

Decode implements Codec.Decode using JSON decoding.

func (*JSONCodec) Encode

func (c *JSONCodec) Encode(w io.Writer, state any) error

Encode implements Codec.Encode using JSON encoding.

func (*JSONCodec) Extension

func (c *JSONCodec) Extension() string

Extension implements Codec.Extension for JSON files.

type Persister

type Persister[T any] struct {
	// contains filtered or unexported fields
}

Persister handles I/O for a specific state type using a Codec.

func NewPersister

func NewPersister[T any](basename string, codec Codec) *Persister[T]

NewPersister creates a persister with the given basename and codec.

func (*Persister[T]) Load

func (p *Persister[T]) Load(dir string, restoreState func(*T)) error

Load restores state from the given directory using the provided restore function.

func (*Persister[T]) Save

func (p *Persister[T]) Save(dir string, buildState func() *T) error

Save writes state to the given directory using the provided build function.

Jump to

Keyboard shortcuts

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