appsettings

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 6 Imported by: 1

README

AppSettings

CI GoDoc

A hierarchical key value store for persisting simple runtime options in Go applications.

Example

s := appsettings.NewAppSettings("settings.json")

t := s.GetTree("user-settings")

//set
t.SetString("pizza", "pie")
t.SetInt("how-many-pugs", 349)

//read
if v, err := t.GetInt("how-many-pugs"); err == nil {
	log.Println(v)
}

if v, err = t.GetString("pizza"); err == nil {
	log.Println(v)
}

s.Persist()

Installation

$ go get -u -v github.com/donatj/appsettings

Migration from v0.0.1

The JSON format for the early Alpha changed. To migrate your existing data compatibly to the more modern format, you can use jq and execute the following command, first replacing {your-file} with the path to your actual database file.

jq '.Tree |= with_entries(.value = {Leaves: .value} ) | . + {Branches: .Tree} | del(.Tree)' < {your-file} > tmp && mv tmp {your-fie}

Documentation

Documentation can be found a godoc:

https://godoc.org/github.com/donatj/appsettings

Documentation

Overview

Package appsettings provides simple key/value store functionality designed to be used for easily storing and recalling application runtime settings

Index

Constants

This section is empty.

Variables

View Source
var ErrUndefinedKey = errors.New("undefined key")

ErrUndefinedKey is returned when the key requested from get is undefined.

View Source
var ErrorEmptyFetch = errors.New("empty fetch result")

ErrorEmptyFetch is returned when the fetch result is empty.

Functions

func OptionPrettyPrint added in v0.3.0

func OptionPrettyPrint(app *AppSettings)

OptionPrettyPrint configures AppSettings to pretty print the saved json

Types

type AppSettings

type AppSettings struct {
	// contains filtered or unexported fields
}

AppSettings is the root most DataTree

func NewAppSettings

func NewAppSettings(dbFilename string, options ...Option) (*AppSettings, error)

NewAppSettings gets a new AppSettings struct

func (AppSettings) DecrInt64 added in v0.4.0

func (a AppSettings) DecrInt64(key string) int64

DecrInt64 decrements an int or int64 leaf and returns the new value as int64.

If the key is undefined or non-integer, this will initialize it to 0 and decrements it to -1

func (AppSettings) Delete added in v0.1.0

func (a AppSettings) Delete(key string)

Delete removes the given leaf from the branch.

func (AppSettings) DeleteTree added in v0.2.0

func (a AppSettings) DeleteTree(key string)

DeleteTree removes the given tree from the branch.

func (AppSettings) GetInt added in v0.1.0

func (a AppSettings) GetInt(key string) (int, error)

GetInt gets the give keys leaf as an int.

Returns an ErrUndefinedKey if the key is not defined.

Will return an error if ParseInt of the value of the leaf fails.

func (AppSettings) GetInt64 added in v0.1.0

func (a AppSettings) GetInt64(key string) (int64, error)

GetInt64 gets the give keys leaf as an int64.

Returns an ErrUndefinedKey if the key is not defined.

Will return an error if ParseInt(...,10, 64) of the value of the leaf fails.

func (AppSettings) GetLeaves added in v0.1.0

func (a AppSettings) GetLeaves() map[string]string

func (AppSettings) GetString added in v0.1.0

func (a AppSettings) GetString(key string) (string, error)

GetString gets the give keys leaf as a string.

Returns an ErrUndefinedKey if the key is not defined

func (AppSettings) GetTree

func (a AppSettings) GetTree(key string) DataTree

GetTree fetches a tree for app setting storage

func (AppSettings) GetTrees added in v0.2.0

func (a AppSettings) GetTrees() map[string]DataTree

func (AppSettings) HasLeaf added in v0.3.0

func (a AppSettings) HasLeaf(key string) bool

HasLeaf checks if the given key is defined as a leaf value

func (AppSettings) HasTree added in v0.3.0

func (a AppSettings) HasTree(key string) bool

HasTree checks if the given key is defined as a tree

func (AppSettings) IncrInt64 added in v0.4.0

func (a AppSettings) IncrInt64(key string) int64

IncrInt64 increments an int or int64 leaf and returns the new value as int64.

If the key is undefined or non-integer, this will initialize it to 0 and increment it to 1

func (*AppSettings) Persist

func (a *AppSettings) Persist() error

Persist causes the current state of the app settings to be persisted.

func (AppSettings) SetInt added in v0.1.0

func (a AppSettings) SetInt(key string, val int)

SetInt sets the give key leaf as a int.

func (AppSettings) SetInt64 added in v0.1.0

func (a AppSettings) SetInt64(key string, val int64)

SetInt64 sets the give key leaf as a int64.

func (AppSettings) SetString added in v0.1.0

func (a AppSettings) SetString(key string, val string)

SetString sets the give key leaf as a string.

type DataTree

type DataTree interface {
	GetString(key string) (string, error)
	SetString(key string, val string)

	GetInt(key string) (int, error)
	SetInt(key string, val int)

	GetInt64(key string) (int64, error)
	SetInt64(key string, val int64)
	IncrInt64(key string) int64
	DecrInt64(key string) int64

	Delete(key string)
	DeleteTree(key string)
	GetTree(key string) DataTree

	HasTree(key string) bool
	HasLeaf(key string) bool

	GetTrees() map[string]DataTree
	GetLeaves() map[string]string
}

DataTree is the host of key and branches of values

type FileSystemStorageAdapter added in v0.6.0

type FileSystemStorageAdapter struct {
	sync.Mutex
}

FileSystemStorageAdapter is a StorageAdapter that uses the file system to persist and fetch app settings. It is the default StorageAdapter for AppSettings.

func NewFileSysPersister added in v0.6.0

func NewFileSysPersister() *FileSystemStorageAdapter

NewFileSysPersister creates a new FileSystemStorageAdapter with the given filename.

func (*FileSystemStorageAdapter) Fetch added in v0.6.0

func (f *FileSystemStorageAdapter) Fetch(filename string) ([]byte, error)

Fetch retrieves the persisted app settings from the file system.

func (*FileSystemStorageAdapter) Persist added in v0.6.0

func (f *FileSystemStorageAdapter) Persist(filename string, data []byte) error

type Option added in v0.3.0

type Option func(*AppSettings)

Option sets an option of the passed AppSettings

func OptionStorageAdapter added in v0.6.0

func OptionStorageAdapter(storage StorageAdapter) Option

OptionStorageAdapter sets the storage adapter for the AppSettings

type StorageAdapter added in v0.6.0

type StorageAdapter interface {
	// Fetch retrieves the persisted app settings.
	// If the result is empty, it will return ErrorEmptyFetch
	// Any initialization should be done in the Fetch method.
	Fetch(filename string) ([]byte, error)

	// Persist causes the current state of the app settings to be persisted.
	Persist(filename string, data []byte) error
}

StorageAdapter is an interface for persisting and fetching serialized app settings

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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