persist

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

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

Go to latest
Published: Feb 14, 2021 License: MIT Imports: 12 Imported by: 0

README

persist

persist contains functionality for storing persisted data and is used by Uplo.

Documentation

Index

Constants

View Source
const (
	// HashSize is the length of a Hash in bytes.
	HashSize = 32
)

Variables

View Source
var (
	// ErrBadFilenameSuffix indicates that SaveJSON or LoadJSON was called using
	// a filename that has a bad suffix. This prevents users from trying to use
	// this package to manage the temp files - this package will manage them
	// automatically.
	ErrBadFilenameSuffix = errors.New("filename suffix not allowed")

	// ErrFileInUse is returned if SaveJSON or LoadJSON is called on a file
	// that's already being manipulated in another thread by the persist
	// package.
	ErrFileInUse = errors.New("another thread is saving or loading this file")
)
View Source
var (
	// ErrBadHeader indicates that the file opened is not the file that was
	// expected.
	ErrBadHeader = errors.New("wrong header")

	// ErrBadVersion indicates that the version number of the file is not
	// compatible with the current codebase.
	ErrBadVersion = errors.New("incompatible version")
)
View Source
var (
	// ErrHashWrongLen is the error when encoded value has the wrong
	// length to be a hash.
	ErrHashWrongLen = errors.New("encoded value has the wrong length to be a hash")
)

Functions

func LoadJSON

func LoadJSON(meta Metadata, object interface{}, filename string) error

LoadJSON will load a persisted json object from disk.

func SaveJSON

func SaveJSON(meta Metadata, object interface{}, filename string) error

SaveJSON will save a json object to disk in a durable, atomic way. The resulting file will have a checksum of the data as the third line. If manually editing files, the checksum line can be replaced with the 8 characters "manual". This will cause the reader to accept the checksum even though the file has been changed.

Types

type Hash

type Hash [HashSize]byte

Hash is a BLAKE2b 256-bit digest.

func HashBytes

func HashBytes(data []byte) Hash

HashBytes takes a byte slice and returns the result.

func (Hash) MarshalJSON

func (h Hash) MarshalJSON() ([]byte, error)

MarshalJSON marshales a hash as a hex string.

func (Hash) String

func (h Hash) String() string

String prints the hash in hex.

func (*Hash) UnmarshalJSON

func (h *Hash) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes the json hex string of the hash.

type Metadata

type Metadata struct {
	Header  string
	Version string
}

Metadata contains the header and version of the data being stored.

Jump to

Keyboard shortcuts

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