config

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 17 Imported by: 0

README

Config

A configuration management library based on BoltDB database.

Features:

  • Underlying database file management
  • Configuration writer APIs
    • SetBool(section string, k string, v bool)
    • SetFloat(section string, k string, v float64)
    • SetInt(section string, k string, v int)
    • SetString(section string, k string, v string)
    • Remove(section string, k string)
    • AddChangeListener(section string, func(k string, v interface{}))
  • Configuration reader APIs
    • Bool(k string) bool
    • BoolOpt(k string, fallback bool) bool
    • Float(k string) float64
    • FloatOpt(k string, fallback float64) float64
    • Int(k string) int
    • IntOpt(k string, fallback int) int
    • String(k string) string
    • StringOpt(k string, fallback string) string

Examples:

package main

import (
	"fmt"
	"github.com/zourva/pareto/config"
	"time"
)

func main() {
	// TODO
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(key string) bool

Bool returns the value associated with the key as a boolean.

func Clamp added in v0.2.0

func Clamp[T box.Number](v *Store, key string, f Getter[T], min, max T)

Clamp overwrites value of key to min or max if its value is not within range [min, max].

func ClampDefault added in v0.2.0

func ClampDefault[T box.Number](v *Store, key string, f Getter[T], min, max, def T)

ClampDefault acts the same as Clamp except that value of key is overwritten by the default value other than the boundary.

func Float64 added in v0.3.0

func Float64(key string) float64

Float64 returns the value associated with the key as a float64.

func Int

func Int(key string) int

Int returns the value associated with the key as an integer.

func Int32 added in v0.3.0

func Int32(key string) int32

Int32 returns the value associated with the key as an integer.

func Int64 added in v0.3.0

func Int64(key string) int64

Int64 returns the value associated with the key as an integer.

func LoadJsonConfig

func LoadJsonConfig(file string, obj any) error

LoadJsonConfig loads config object of a JSON file and deserialize to the given object. Return nil or any error if happened during loading and unmarshal.

func MergeStore added in v0.3.0

func MergeStore(s *Store) error

MergeStore merges the given store into the global one.

func String

func String(key string) string

String returns the value associated with the key as a string.

func Uint added in v0.3.0

func Uint(key string) uint

Uint returns the value associated with the key as an unsigned integer.

func Uint16 added in v0.3.0

func Uint16(key string) uint16

Uint16 returns the value associated with the key as an unsigned integer.

func Uint32 added in v0.3.0

func Uint32(key string) uint32

Uint32 returns the value associated with the key as an unsigned integer.

func Uint64 added in v0.3.0

func Uint64(key string) uint64

Uint64 returns the value associated with the key as an unsigned integer.

func UnmarshalKey added in v0.3.0

func UnmarshalKey(path string, o any) error

Types

type BoltdbProvider added in v0.3.0

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

BoltdbProvider implements a boltdb provider. To use the provider correctly, a concrete parser is needed to parse raw bytes returned by ReadBytes.

func NewBoltdbProvider added in v0.3.0

func NewBoltdbProvider(path string) *BoltdbProvider

NewBoltdbProvider returns a boltdb provider.

func (*BoltdbProvider) Read added in v0.3.0

func (f *BoltdbProvider) Read() (map[string]any, error)

Read is not supported by boltdb provider.

func (*BoltdbProvider) ReadBytes added in v0.3.0

func (f *BoltdbProvider) ReadBytes() ([]byte, error)

ReadBytes is not supported by boltdb provider.

type DataType added in v0.3.0

type DataType = string
const (
	Json    DataType = "json"
	Yaml    DataType = "yaml" // for both yaml and yml
	MsgPack DataType = "msgpack"
)

type File added in v0.2.0

type File[T any] struct {
	FileName   string
	FileKind   Kind
	ModifyTime time.Time
	Content    T
}

func NewFile added in v0.2.0

func NewFile[T any]() *File[T]

func (*File[T]) Init added in v0.2.0

func (f *File[T]) Init(file string, kind Kind) (*T, error)

func (*File[T]) Listen added in v0.2.0

func (f *File[T]) Listen(ctx context.Context, duration time.Duration, changed func(content *T) error)

type FileType added in v0.3.0

type FileType = string
const (
	Text   FileType = "text"
	Boltdb FileType = "boltdb"
	Sqlite FileType = "sqlite"
)

type Flusher added in v0.3.0

type Flusher = func(map[string]any) error

type Getter added in v0.2.0

type Getter[T box.Number] func(string) T

type Kind added in v0.2.0

type Kind int
const (
	JSON Kind = iota
	YAML
)

func (Kind) String added in v0.2.0

func (k Kind) String() string

type Option added in v0.3.0

type Option = func(s *Store)

func WithFlusher added in v0.3.0

func WithFlusher(subPath string, flusher Flusher) Option

type Store added in v0.2.0

type Store struct {
	//*viper.Viper
	*koanf.Koanf
	// contains filtered or unexported fields
}

Store wraps viper and provides extended functionalities. Store uses a storage system as the backlog and accepts multiple config sources to merge them into the storage.

A config store is structured as a tree and be flattened using a key-value pattern, and the config value is accessed using a key, depicted by a tree-node-path.

func GetStore added in v0.2.0

func GetStore() *Store

GetStore returns the global store instance.

func New added in v0.2.0

func New(opts ...Option) *Store

New creates a configuration store. Returns the created store or nil if any error occurred.

func SubStore added in v0.3.0

func SubStore(path string) *Store

SubStore returns a copy of the subtree of the global store. The subtree is identified by the given path.

func (*Store) Flush added in v0.3.0

func (s *Store) Flush(key string) error

Flush writes configurations in store back to the given file.

func (*Store) Int32 added in v0.3.0

func (s *Store) Int32(k string) int32

Int32 compatible with viper api.

func (*Store) Load added in v0.2.0

func (s *Store) Load(file string, ft FileType, dt DataType, rootKeys ...string) error

Load loads config from file into this store. When called multiple times over different files, configs are merged.

func (*Store) MergeStore added in v0.3.0

func (s *Store) MergeStore(store *Store) error

func (*Store) SetDefault added in v0.3.0

func (s *Store) SetDefault(k string, v any)

SetDefault is here for compatible with viper api.

func (*Store) Uint added in v0.3.0

func (s *Store) Uint(key string) uint

Uint compatible with viper api.

func (*Store) Uint16 added in v0.3.0

func (s *Store) Uint16(key string) uint16

Uint16 compatible with viper api.

func (*Store) Uint32 added in v0.3.0

func (s *Store) Uint32(key string) uint32

Uint32 compatible with viper api.

func (*Store) Uint64 added in v0.3.0

func (s *Store) Uint64(key string) uint64

Uint64 compatible with viper api.

func (*Store) UnmarshalKey added in v0.3.0

func (s *Store) UnmarshalKey(path string, o any) error

UnmarshalKey is here for compatible with viper api.

Jump to

Keyboard shortcuts

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