keyvalue

package
v0.0.0-...-3bd2f2d Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoRows = sql.ErrNoRows

Functions

This section is empty.

Types

type FlagValue

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

FlagValue implements the flag.Value interface. See KeyValueStore.GetFlagValue for usage.

func (FlagValue) Get

func (fv FlagValue) Get() interface{}

Get the key associated with fv, as a string. If the key was not set, return "".

func (FlagValue) Set

func (fv FlagValue) Set(value string) error

Set the key associated with fv to "value". If a validator was passed, it will be called; if the validaotr returns an error, Set will return an error.

func (FlagValue) String

func (fv FlagValue) String() string

Get the key associated with fv. If the key was not set, return "".

type KeyValueStore

type KeyValueStore struct {
	*sqlx.DB
	// contains filtered or unexported fields
}

func OpenDB

func OpenDB(db *sql.DB, driverName string) (*KeyValueStore, error)

OpenDB accepts *sql.DB and driver name to an open github.com/mattn/go-sqlite3 database. It creates the appropriate table if not present, and returns a KeyValueStore. Tables are created with the `keyvalue_` prefix; no tables starting with `keyvalue_` should be in this database if using this mode.

func OpenFile

func OpenFile(name string) (*KeyValueStore, error)

OpenFile takes a filename. It will open the file as a SQLite database, creating it if it doesn't exist.

func (*KeyValueStore) Close

func (store *KeyValueStore) Close()

Close cleans up the database. If KeyValueStore was created by OpenFile, the backing file is closed; otherwise, the references to the underlying database are just dropped.

func (KeyValueStore) Exchange

func (kv KeyValueStore) Exchange(key, value string) (string, error)

Sets "key" to "value", returning the old value, or an empty string if there is no previous value.

func (KeyValueStore) ExchangeBool

func (kv KeyValueStore) ExchangeBool(key string, val bool) (bool, error)

func (KeyValueStore) Get

func (kv KeyValueStore) Get(key string) (string, error)

Get looks the value of "key" in the store. If found, it returns the value; if not found, it will return ErrNoRows.

func (KeyValueStore) GetBoolDef

func (kv KeyValueStore) GetBoolDef(key string) bool

GetBoolDef looks for the value of "key" in the store. If found it returns the value; on any error it returns "false".

func (*KeyValueStore) GetFlagValue

func (kvs *KeyValueStore) GetFlagValue(key string, validators ...func(string) error) FlagValue

GetFlagValue returns a structure of type FlagValue, which can be passed in to flag.Var*() functions. When the flag is set, the key will be set to the value of the flag.

For example:

flag.Var(kv.GetFlagValue("ServerAddress"), "address", "Address to serve http from")

For now, keyvalue databases must be opened before flags are read, with a fixed filename. Allowing the filename to be one of the flags is a future feature.

func (KeyValueStore) Set

func (kv KeyValueStore) Set(key, value string) error

Set "key" to "value" in the store.

func (KeyValueStore) SetBool

func (kv KeyValueStore) SetBool(key string, val bool) error

SetBool sets "key" to a stringified boolean of "val".

func (KeyValueStore) Unset

func (kv KeyValueStore) Unset(key string) error

Delete a key from the store. Future Get() calls will return ErrNoRows.

Jump to

Keyboard shortcuts

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