confstore

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Tools to manipulate string config key-values

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnumProcessor added in v0.14.1

func EnumProcessor(enums []string) *enumProcessor

func IntProcessor

func IntProcessor(min int, minInclude bool, max int, maxInclude bool) *intProcessor

func StringProcessor

func StringProcessor(minLength, maxLength int) *stringProcessor

Types

type Interface

type Interface interface {
	// register config key
	//
	// duplicated key not allowed
	//
	// default string value must be valid to ValueProcessor
	RegisterKey(key string, defaultValue string, v ValueProcessor) error

	// deregister config key, noop if key doesn't exist
	DeregisterKey(key string)

	// batch update config key-values
	//
	// key must be registered before
	BatchUpdate(kvStrs []*KVStr) []*KVError

	// update config key-value and persist.
	// ValueProcessor will handle string value
	Update(key string, value string) *KVError

	// update config key-value but do not persist.
	// ValueProcessor will handle string value
	UpdateNoPersist(key string, value string) *KVError

	// batch get config key-values
	//
	// if key doesn't exist, no error will return
	BatchGetValues(keys []string) []*KV

	// batch get config key-value strings
	//
	// if key doesn't exist, no error will return
	BatchGetValueString(keys []string) []*KVStr

	// get config key-value
	//
	// panic if key doesn't exist
	MustGetValue(key string) interface{}

	// get config key-value string
	//
	// panic if key doesn't exist
	MustGetValueString(key string) string

	// get config key-value
	//
	// if key doesn't exist, return nil, false
	GetValue(key string) (v interface{}, hit bool)

	// get config key-value string
	//
	// if key doesn't exist, return nil, false
	GetValueString(key string) (v string, hit bool)

	// reset key's current value to default value
	ResetKey(key string)
}

TODO write examples A interface to manage config key-values

func NewStore

func NewStore(persister Persister, policy LoadPolicy) Interface

type KV

type KV struct {
	Key   string
	Value interface{}
}

func (*KV) String

func (K *KV) String() string

type KVError

type KVError struct {
	Key   string `json:"key"`
	Value string `json:"value"`
	Error string `json:"error"`
}

func (*KVError) String

func (K *KVError) String() string

type KVStr

type KVStr struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

func (*KVStr) String

func (K *KVStr) String() string

type LoadPolicy

type LoadPolicy interface {
	// decide how to load config key-values from Persister into Interface
	DoLoad(s Interface, p Persister) error
}
var (
	NoopLoadPolicy   LoadPolicy = &noopLoadPolicy{}     // Never load config key-values from Persister to Interface
	SimpleLoadPolicy            = NewSimpleLoadPolicy() // Load config key-values from Persister immediately
)

func NewLoadPolicy

func NewLoadPolicy(minInterval time.Duration) LoadPolicy

Create a default load policy that load config key-values from Persister only when last load timestamp is `minInterval` before current time.

This implementation protect underlying Persister from being queried too often

func NewSimpleLoadPolicy

func NewSimpleLoadPolicy() LoadPolicy

type Persister

type Persister interface {
	// load config key-values from underlying persistence layer into Interface
	Load(s Interface) error
	// persist config key-value
	Save(key, value string) error
	// batch persist config key-values
	BatchSave([]*KVStr) error
	// delete config key from underlying persistence layer
	Delete(key string) error
}

config key persistence layer

var (
	NoopPersister Persister = &noopPersister{} // Never persist config key-values
)

func NewRedisPersister

func NewRedisPersister(redisClient *redis.Client, configKeyRoot string) Persister

Create a redis Persister

type ValueProcessor

type ValueProcessor interface {
	Validate(value string) (ok bool, err string)
	Convert(value string) interface{}
}

used to validate string value and convert string value to specific type

var (
	String             ValueProcessor = StringProcessor(0, math.MaxInt32)
	Bool               ValueProcessor = &boolProcessor{}
	Int                ValueProcessor = IntProcessor(math.MinInt32, true, math.MaxInt32, true)
	IntGtZero          ValueProcessor = IntProcessor(0, false, math.MaxInt32, true)
	IntGtEqZero        ValueProcessor = IntProcessor(0, true, math.MaxInt32, true)
	Duration           ValueProcessor = &durationProcessor{}
	Url                ValueProcessor = &urlProcessor{}
	UrlString          ValueProcessor = &urlStringProcessor{}
	CssColorHex        ValueProcessor = &cssColorHex{}
	RsaPrivateKeyPkcs1 ValueProcessor = &rsaPrivateKeyPkcs1{}
	RsaPublicKey       ValueProcessor = &rsaPublicKey{}
)

Jump to

Keyboard shortcuts

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