confy

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2021 License: MIT Imports: 11 Imported by: 3

README

confy

A stupid simple configuration store.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMemNoKey = errors.New("no such key in MemData")

ErrMemNoKey occurs when a given key does not exist.

Functions

This section is empty.

Types

type Confy

type Confy struct {
	Data
	Form
}

A Confy is a stupid simple configuration store.

func New

func New(s Data, f Form) *Confy

New makes a new Confy with the given Data and Form.

func (*Confy) Load

func (c *Confy) Load(key string, ptr interface{}) error

Load wraps a call to Data.Reader and Form.Unmarshal.

func (*Confy) Store

func (c *Confy) Store(key string, value interface{}) error

Store wraps a call to Data.Writer and Form.Marshal.

type Data

type Data interface {
	// Reader fetches a ReadCloser for the given key.
	Reader(key string) (io.ReadCloser, error)

	// Writer fetches a WriteCloser for the given key.
	Writer(key string) (io.WriteCloser, error)

	// Delete removes the given key if it exists.
	Delete(key string) error

	// Keys returns available keys.
	Keys() ([]string, error)
}

Data exposes a storage backend.

func ConfigFileData

func ConfigFileData(app, ext string) (Data, error)

ConfigFileData creates a FileData with the given app name and file extension.

func NewFileData

func NewFileData(base, ext string) (Data, error)

NewFileData creates a FileData with a given base directory and file extension.

func NewMemData

func NewMemData() Data

NewMemData creates a usable MemData.

type FileData

type FileData struct {
	Base string
	Ext  string
}

FileData is a filesystem-based Data.

func (FileData) Delete

func (fs FileData) Delete(key string) error

Delete removes the given key if it exists - in this case, using os.Remove.

func (FileData) Keys

func (fs FileData) Keys() ([]string, error)

Keys returns available keys - in this case, using os.ReadDir.

func (FileData) Reader

func (fs FileData) Reader(key string) (io.ReadCloser, error)

Reader fetches a ReadCloser for the given key - in this case, using os.Open.

func (FileData) Writer

func (fs FileData) Writer(key string) (io.WriteCloser, error)

Writer fetches a WriteCloser for the given key - in this case, using os.Create.

type Form

type Form interface {
	// Marshal encodes the given value into the given Writer.
	Marshal(w io.Writer, value interface{}) error

	// Unmarshal decodes the given Reader into the given pointer.
	Unmarshal(r io.Reader, ptr interface{}) error
}

Form describes an object encoding format.

type JSONForm

type JSONForm struct {
	// Marshal options
	EscapeHTML bool
	Prefix     string
	Indent     string

	// Unmarshal options
	DisallowUnknownFields bool
	UseNumber             bool
}

JSONForm is a JSON-based Form, ideal for structured data.

func (JSONForm) Marshal

func (jf JSONForm) Marshal(w io.Writer, value interface{}) error

Marshal encodes the given value into the given Writer - in this case, using json.NewEncoder.

func (JSONForm) Unmarshal

func (jf JSONForm) Unmarshal(r io.Reader, value interface{}) error

Unmarshal decodes the given Reader into the given pointer - in this case, using json.NewDecoder.

type MemData

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

MemData is a concurrent-safe, in-memory Data.

func (MemData) Delete

func (ms MemData) Delete(key string) error

Delete removes the given key if it exists - in this case, using internal maps and locks.

func (MemData) Keys

func (ms MemData) Keys() ([]string, error)

Keys returns available keys - in this case, using internal maps and locks.

func (MemData) Reader

func (ms MemData) Reader(key string) (io.ReadCloser, error)

Reader fetches a ReadCloser for the given key - in this case, using internal maps and locks.

func (MemData) Writer

func (ms MemData) Writer(key string) (io.WriteCloser, error)

Writer fetches a WriteCloser for the given key - in this case, using internal maps and locks.

type YAMLForm

type YAMLForm struct {
	// Marshal options
	Indent int

	// Unmarshal options
	KnownFields bool
}

YAMLForm is a YAML-based Form, ideal for readable configuration.

func (YAMLForm) Marshal

func (yf YAMLForm) Marshal(w io.Writer, value interface{}) error

Marshal encodes the given value into the given Writer - in this case, using yaml.NewEncoder.

func (YAMLForm) Unmarshal

func (yf YAMLForm) Unmarshal(r io.Reader, value interface{}) error

Unmarshal decodes the given Reader into the given pointer - in this case, using yaml.NewDecoder.

Jump to

Keyboard shortcuts

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