kv

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2023 License: MIT Imports: 12 Imported by: 0

README

consul-kv

A consul key/value tool for golang

Usage

New Config
conf := NewConfig()
With Options
conf := NewConfig(
    WithPrefix(prefix),             // consul kv prefix
    WithAddress(address),           // consul address
    WithAuth(username, password),   // cosul auth
    WithToken(token),               // cousl token
    WithLoger(loger),               // loger
)

Init
if err := conf.Init();err !=nil {
    return err
}
Put
if err := conf.Put(key, value);err !=nil {
    return err
}
Delete
if err := conf.Delete(key);err !=nil {
    return err
}
Get
// scan
if err := conf.Get(key).Scan(x);err !=nil {
    return err
}

// get float
float := conf.Get(key).Float()

// get float with default
float := conf.Get(key).Float(defaultFloat)

// get int
i := conf.Get(key).Int()

// get int with default
i := conf.Get(key).Int(defaultInt)

// get uint
uInt := conf.Get(key).Uint()

// get uint with default
uInt := conf.Get(key).Uint(defaultUint)

// get bool
b := conf.Get(key).Bool()

// get bool with default
b := conf.Get(key).Bool(defaultBool)

// get []byte
bytes := conf.Get(key).Bytes()

// get uint with default
bytes := conf.Get(key).bytes(defaultBytes)

// get string
str := conf.Get(key).String()

// get string with default
str := conf.Get(key).String(defaultStr)

// get time
t := conf.Get(key).Time()

// get time with default
t := conf.Get(key).Time(defaultTime)

// get nested key values
conf.Get(key).Get(nextKey1).Get(nextKey2).String()
Watch
conf.Watch(path, func(r *Result){
    r.Scan(x)
})

Stop Watch
// stop single watcher
conf.StopWatch(path)

// stop multiple watcher
conf.StopWatch(path1, path2)

// stop all watcher
conf.StopWatch()

Next Features:

  • Distributed lock
  • Transaction

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlreadyWatch = errors.New("watch path already exist")
	ErrKeyNotFound  = errors.New("key not found")
)

Error defined

Functions

func Delete

func Delete(path string) error

Delete ...

func Init

func Init(opts ...Option) error

Init ...

func Put

func Put(path string, value any) error

Put ...

func SetOptions

func SetOptions(opts ...Option) error

SetOptions ...

func StopWatch

func StopWatch(path ...string)

StopWatch ...

func Watch

func Watch(path string, handler func(*Result)) error

Watch ...

Types

type Config

type Config struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Config ...

func NewConfig

func NewConfig(opts ...Option) *Config

NewConfig ...

func (*Config) CheckWatcher added in v1.1.0

func (c *Config) CheckWatcher(path string) error

CheckWatcher ...

func (*Config) Delete

func (c *Config) Delete(path string) error

Delete ...

func (*Config) Get

func (c *Config) Get(keys ...string) (ret *Result)

Get ...

func (*Config) Init added in v1.2.1

func (c *Config) Init() error

Init ...

func (*Config) Put

func (c *Config) Put(path string, value any) error

Put ...

func (*Config) StopWatch

func (c *Config) StopWatch(path ...string)

StopWatch ...

func (*Config) Watch

func (c *Config) Watch(path string, handler func(*Result)) error

Watch ...

type Logger added in v1.2.0

type Logger interface {
	Info(msg string, args ...any)
	Warn(msg string, args ...any)
	Error(msg string, args ...any)
}

Logger ...

func NewLogger added in v1.2.0

func NewLogger() Logger

NewLogger ...

type Option

type Option func(opt *Config)

Option ...

func WithAddress

func WithAddress(address string) Option

WithAddress ...

func WithAuth

func WithAuth(username, password string) Option

WithAuth ...

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix ...

func WithToken

func WithToken(token string) Option

WithToken ...

func Withlogger added in v1.2.0

func Withlogger(logger Logger) Option

Withlogger ...

type Result

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

Result ...

func Get

func Get(keys ...string) *Result

Get ...

func (*Result) Bool

func (r *Result) Bool(defaultValue ...bool) bool

Bool ...

func (*Result) Bytes

func (r *Result) Bytes(defaultValue ...[]byte) []byte

Bytes ...

func (*Result) Err

func (r *Result) Err() error

Err ...

func (*Result) Float

func (r *Result) Float(defaultValue ...float64) float64

Float ...

func (*Result) Get

func (r *Result) Get(path string) *Result

Get ...

func (*Result) Int

func (r *Result) Int(defaultValue ...int64) int64

Int ...

func (*Result) Key

func (r *Result) Key() string

Key ...

func (*Result) Scan

func (r *Result) Scan(x any) error

Scan ...

func (*Result) String

func (r *Result) String(defaultValue ...string) string

String

func (*Result) Time

func (r *Result) Time(defaultValue ...time.Time) time.Time

Time ...

func (*Result) Uint

func (r *Result) Uint(defaultValue ...uint64) uint64

Uint ...

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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