state

package
v0.0.0-...-8b058bf Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CurrentFileInfo

func CurrentFileInfo(v *viper.Viper, afs *afero.Afero) (os.FileInfo, error)

CurrentFileInfo returns information about a state file, including its name, size, permissions, and when it was last modified.

func Discover

func Discover(stateFolderName string, folderPath string, afs *afero.Afero) (names []string, err error)

Discover looks for .yaml files in a folder and returns the name of each file without the .yaml suffix. If the folder does not exist or cannot be read, Discover returns an error.

Types

type Handle

type Handle struct {
	FilePath string
	FileInfo os.FileInfo
	Viper    *viper.Viper
}

A state Handle is used to save and load a stateful object to/from disk. It consists of the path to the state file, the latest information about that file, and a Viper instance to handle reading/writing/unmarshalling the file data.

func (*Handle) Initialize

func (handle *Handle) Initialize(name string, folderPath string, afs *afero.Afero) error

Initialize makes sure a handle can be used to save and load a state file with viper, determining the file path if needed and creating the containing folder if it does not exist. It expects that the parent struct of the handle has already been initialized. It errors if any step fails.

func (*Handle) InitializeViper

func (handle *Handle) InitializeViper(afs *afero.Afero) bool

Initialize Viper makes sure the Handle has a viper and returns true if it had to create one or false if not.

func (*Handle) Load

func (handle *Handle) Load(state Stateful, afs *afero.Afero) (os.FileInfo, error)

Load overwrites the in-memory representation of a state from the data in a statefile. The Handle must be initialized before calling Load. It updates the FileInfo for the handle if successful. It returns an error if any step fails.

func (*Handle) Save

func (handle *Handle) Save(afs *afero.Afero) (os.FileInfo, error)

Save writes a given state's in-memory data to a statefile, creating or overwriting it as needed. The Handle must be initialized before calling Save. It returns an error if any step fails.

func (*Handle) SetStruct

func (handle *Handle) SetStruct(data any, parent string) error

SetStruct is used to automatically map a struct's fields to viper configuration settings and is mapstructure-aware; if any of the fields in your struct have a defined mapstructure tag, SetStruct will figure out the right way to add them for you. It respects the `tympanconfig` tag, using any behavior specified by it. It handles nested structs and uses viper's dot notation to write them correctly nested, prepending the children with their parent path during recursion. It returns an error if the passed data is not a struct.

type Initializable

type Initializable[I any, T any] interface {
	// The Initializable struct should return a pointer to a valid instance of itself with defaults set.
	Initialize() T
}

Initializable structs must be able to load their own defaults. Initializable structs are often included in Stateful structs. When specified in a new type of stateful struct, you must pass the type twice: once as itself, once as a pointer to itself. For Example:

type Instance[D state.Initializable[D, *D]] struct {}

type Kind

type Kind struct {
	Name       string
	FolderName string
}

The Kind of a stateful struct is metadata about the struct and is specified by the application using it. The Name field is used in log messaging/displays about the struct while the FolderName field is used to determine where to save the struct or look for it when loading. If Kind is not specified, the appropriate defaults should be set by the implementation of the stateful struct's Initialize() method.

type MetaConfig

type MetaConfig struct {
	ConfigKey string
	Ignore    bool
}

MetaConfig structs are used when parsing tags on configuration structs; they help turn a mapstructure tag into the name of a viper configuration key and change the behavior of a configuration item via the tympanconfig directive; right now the only supported directive is `tympanconfig:"ignore"` which ensures a struct key is not written to the configuration.

func ParseStructTags

func ParseStructTags(tagEntry reflect.StructTag) (metaConfig MetaConfig)

ParseStructTags() is used to introspect on a struct which is to be saved to disk via viper; it returns the MetaConfig for a given struct field which SetStruct uses to determine behavior.

type Stateful

type Stateful interface {
	// A stateful struct must be initializeable. It is expected that initialize should set the default values for the
	// struct if they are not set, call Initialize on their state handle, load the state from disk if the file already
	// exists or write the default state to disk if it does not. If initalization fails, the method should return an error
	Initialize(name string, rootFolderPath string, afs *afero.Afero) error
	Load(afs *afero.Afero) error
	Save(afs *afero.Afero) error
}

Stateful structs can be initialized, loaded, and saved. All three methods update the struct itself, Initialize and Save may write to disk while Load will only ever read from disk.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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