lac

package module
v0.0.0-...-1beb1da Latest Latest
Warning

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

Go to latest
Published: May 18, 2024 License: GPL-2.0 Imports: 8 Imported by: 4

README

lac

Live application config

LAC is a configuration library for Golang with focus on gorutine safety.

Entierly for use in my own projects, no guarantees on anything.

General methods and usage

All operations are guarded with a mutex, it is safe to get/set/load/safe from within multiple gorutines.

Loading/saving
func FromBytesJSON(b []byte) (*Conf, error)
func FromFileJSON(path string) (*Conf, error)

func (c *Conf) SetFromBytesJSON(b []byte) error
func (c *Conf) SetFromFileJSON(path string) error

func (c *Conf) ToBytesIndentJSON() ([]byte, error)
func (c *Conf) ToFileIndentJSON(path string, perm os.FileMode) error
Getting/setting data

Raw get operations. They return copy of the data in the config tree.

func (c *Conf) Get(k ...string) (any, bool)
func (c *Conf) GetString(k ...string) (string, bool)
func (c *Conf) GetFloat64(k ...string) (float64, bool)
func (c *Conf) GetInt64(k ...string) (int64, bool)
func (c *Conf) GetInt(k ...string) (int, bool) // just trimmed int64, beware of overflows
func (c *Conf) GetBool(k ...string) (bool, bool)
func (c *Conf) GetMapStringAny(k ...string) (map[string]any, bool)

Use the prefix "D" to specify a default value to return if value in config tree does not exist or of a wrong type.

func (c *Conf) GetDString(d string, k ...string) string
func (c *Conf) GetDFloat64(d float64, k ...string) float64
func (c *Conf) GetDInt64(d int64, k ...string) int64
func (c *Conf) GetDInt(d int, k ...string) int
func (c *Conf) GetDBool(d bool, k ...string) bool

Combine with prefix "S" to also set the default if it is of wrong type or does not exist. (To initialize tree for later editing/writing)

func (c *Conf) GetDSString(d string, k ...string) string
func (c *Conf) GetDSFloat64(d float64, k ...string) float64
func (c *Conf) GetDSInt64(d int64, k ...string) int64
func (c *Conf) GetDSInt(d int, k ...string) int
func (c *Conf) GetDSBool(d bool, k ...string) bool

If you just want to list keys of the path, there is a helper for that

func (c *Conf) GetKeys(k ...string) ([]string, bool)

Raw set operation is also available to update the configuration tree.

func (c *Conf) Set(v any, k ...string)

Example

{
    "hello": "world",
    "testing": {
        "stuff": 42
    }
}
conf, err := lac.FromFileJSON("config.json")
str, ok := conf.GetString("hello") // str = "world", ok = true
str, ok = conf.GetString("hello2") // str = "", ok = false
str = conf.GetDString("a", "doesnotexist") // str = "a"
str = conf.GetDString("a", "hello") // str = "world"
i = conf.GetDInt(0, "testing", "stuff") // i = 42
str = conf.GetDSString("a", "doesnotexist") // str = "a", config updated
err = conf.ToFileIndentJSON("config.json", 0644)
{
    "doesnotexist": "a",
    "hello": "world",
    "testing": {
        "stuff": 42
    }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoKey = errors.New("key not found")
)

Functions

This section is empty.

Types

type Conf

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

func FromBytesJSON

func FromBytesJSON(b []byte) (*Conf, error)

func FromFileJSON

func FromFileJSON(path string) (*Conf, error)

func NewConf

func NewConf() *Conf

func (*Conf) CopyTree

func (c *Conf) CopyTree(t map[string]any)

func (*Conf) Get

func (c *Conf) Get(k ...string) (any, bool)

func (*Conf) GetBool

func (c *Conf) GetBool(k ...string) (bool, bool)

func (*Conf) GetDBool

func (c *Conf) GetDBool(d bool, k ...string) bool

func (*Conf) GetDFloat64

func (c *Conf) GetDFloat64(d float64, k ...string) float64

func (*Conf) GetDInt

func (c *Conf) GetDInt(d int, k ...string) int

func (*Conf) GetDInt64

func (c *Conf) GetDInt64(d int64, k ...string) int64

func (*Conf) GetDSBool

func (c *Conf) GetDSBool(d bool, k ...string) bool

func (*Conf) GetDSFloat64

func (c *Conf) GetDSFloat64(d float64, k ...string) float64

func (*Conf) GetDSInt

func (c *Conf) GetDSInt(d int, k ...string) int

func (*Conf) GetDSInt64

func (c *Conf) GetDSInt64(d int64, k ...string) int64

func (*Conf) GetDSString

func (c *Conf) GetDSString(d string, k ...string) string

func (*Conf) GetDString

func (c *Conf) GetDString(d string, k ...string) string

func (*Conf) GetFloat64

func (c *Conf) GetFloat64(k ...string) (float64, bool)

func (*Conf) GetInt

func (c *Conf) GetInt(k ...string) (int, bool)

func (*Conf) GetInt64

func (c *Conf) GetInt64(k ...string) (int64, bool)

func (*Conf) GetKeys

func (c *Conf) GetKeys(k ...string) ([]string, bool)

func (*Conf) GetMapStringAny

func (c *Conf) GetMapStringAny(k ...string) (map[string]any, bool)

func (*Conf) GetString

func (c *Conf) GetString(k ...string) (string, bool)

func (*Conf) GetToStruct

func (c *Conf) GetToStruct(t any, k ...string) error

func (*Conf) Set

func (c *Conf) Set(v any, k ...string)

func (*Conf) SetFromBytesJSON

func (c *Conf) SetFromBytesJSON(b []byte) error

func (*Conf) SetFromFileJSON

func (c *Conf) SetFromFileJSON(path string) error

func (*Conf) SetTree

func (c *Conf) SetTree(t map[string]any)

func (*Conf) SubTree

func (c *Conf) SubTree(path ...string) *ConfSubtree

func (*Conf) ToBytesIndentJSON

func (c *Conf) ToBytesIndentJSON() ([]byte, error)

func (*Conf) ToBytesJSON

func (c *Conf) ToBytesJSON() ([]byte, error)

func (*Conf) ToFileIndentJSON

func (c *Conf) ToFileIndentJSON(path string, perm os.FileMode) error

func (*Conf) ToFileJSON

func (c *Conf) ToFileJSON(path string, perm os.FileMode) error

func (*Conf) Walk

func (c *Conf) Walk(f ConfWalkFunc)

type ConfSubtree

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

func NewSubTree

func NewSubTree(c *Conf, path ...string) *ConfSubtree

func (*ConfSubtree) Get

func (c *ConfSubtree) Get(k ...string) (any, bool)

func (*ConfSubtree) GetBool

func (c *ConfSubtree) GetBool(k ...string) (bool, bool)

func (*ConfSubtree) GetDBool

func (c *ConfSubtree) GetDBool(d bool, k ...string) bool

func (*ConfSubtree) GetDFloat64

func (c *ConfSubtree) GetDFloat64(d float64, k ...string) float64

func (*ConfSubtree) GetDInt

func (c *ConfSubtree) GetDInt(d int, k ...string) int

func (*ConfSubtree) GetDInt64

func (c *ConfSubtree) GetDInt64(d int64, k ...string) int64

func (*ConfSubtree) GetDSBool

func (c *ConfSubtree) GetDSBool(d bool, k ...string) bool

func (*ConfSubtree) GetDSFloat64

func (c *ConfSubtree) GetDSFloat64(d float64, k ...string) float64

func (*ConfSubtree) GetDSInt

func (c *ConfSubtree) GetDSInt(d int, k ...string) int

func (*ConfSubtree) GetDSInt64

func (c *ConfSubtree) GetDSInt64(d int64, k ...string) int64

func (*ConfSubtree) GetDSString

func (c *ConfSubtree) GetDSString(d string, k ...string) string

func (*ConfSubtree) GetDString

func (c *ConfSubtree) GetDString(d string, k ...string) string

func (*ConfSubtree) GetFloat64

func (c *ConfSubtree) GetFloat64(k ...string) (float64, bool)

func (*ConfSubtree) GetInt

func (c *ConfSubtree) GetInt(k ...string) (int, bool)

func (*ConfSubtree) GetInt64

func (c *ConfSubtree) GetInt64(k ...string) (int64, bool)

func (*ConfSubtree) GetKeys

func (c *ConfSubtree) GetKeys(k ...string) ([]string, bool)

func (*ConfSubtree) GetMapStringAny

func (c *ConfSubtree) GetMapStringAny(k ...string) (map[string]any, bool)

func (*ConfSubtree) GetString

func (c *ConfSubtree) GetString(k ...string) (string, bool)

func (*ConfSubtree) GetToStruct

func (c *ConfSubtree) GetToStruct(t any, k ...string) error

func (*ConfSubtree) Set

func (c *ConfSubtree) Set(v any, k ...string)

func (*ConfSubtree) SubTree

func (c *ConfSubtree) SubTree(path ...string) *ConfSubtree

type ConfWalkFunc

type ConfWalkFunc func(k []string, v any)

Jump to

Keyboard shortcuts

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