memstore

package
v12.2.0-beta4 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2022 License: BSD-3-Clause Imports: 9 Imported by: 4

Documentation

Overview

Package memstore contains a store which is just a collection of key-value entries with immutability capabilities.

Developers can use that storage to their own apps if they like its behavior. It's fast and in the same time you get read-only access (safety) when you need it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GobEncode

func GobEncode(store Store, w io.Writer) error

GobEncode accepts a store and writes as series of bytes to the "w" writer.

func GobEncodeEntry

func GobEncodeEntry(entry Entry, w io.Writer) error

GobEncodeEntry accepts an entry and writes as series of bytes to the "w" writer.

func GobSerialize

func GobSerialize(store Store) ([]byte, error)

GobSerialize same as GobEncode but it returns the bytes using a temp buffer.

func GobSerializeEntry

func GobSerializeEntry(entry Entry) ([]byte, error)

GobSerializeEntry same as GobEncodeEntry but it returns the bytes using a temp buffer.

Types

type Entry

type Entry struct {
	Key      string      `json:"key" msgpack:"key" yaml:"Key" toml:"Value"`
	ValueRaw interface{} `json:"value" msgpack:"value" yaml:"Value" toml:"Value"`
	// contains filtered or unexported fields
}

Entry is the entry of the context storage Store - .Values()

func GobDecodeEntry

func GobDecodeEntry(b []byte) (entry Entry, err error)

GobDecodeEntry accepts a series of bytes and returns the entry.

func (Entry) BoolDefault

func (e Entry) BoolDefault(def bool) (bool, error)

BoolDefault returns the user's value as bool. a string which is "1" or "t" or "T" or "TRUE" or "true" or "True" or "0" or "f" or "F" or "FALSE" or "false" or "False". Any other value returns an error.

If not found returns "def" and a non-nil error.

func (Entry) Float32Default

func (e Entry) Float32Default(key string, def float32) (float32, error)

Float32Default returns the entry's value as float32. If not found returns "def" and a non-nil error.

func (Entry) Float64Default

func (e Entry) Float64Default(def float64) (float64, error)

Float64Default returns the entry's value as float64. If not found returns "def" and a non-nil error.

func (Entry) GetByKindOrNil

func (e Entry) GetByKindOrNil(k reflect.Kind) interface{}

GetByKindOrNil will try to get this entry's value of "k" kind, if value is not that kind it will NOT try to convert it the "k", instead it will return nil, except if boolean; then it will return false even if the value was not bool.

If the "k" kind is not a string or int or int64 or bool then it will return the raw value of the entry as it's.

func (Entry) Int16Default

func (e Entry) Int16Default(def int16) (int16, error)

Int16Default returns the entry's value as int16. If not found returns "def" and a non-nil error.

func (Entry) Int32Default

func (e Entry) Int32Default(def int32) (int32, error)

Int32Default returns the entry's value as int32. If not found returns "def" and a non-nil error.

func (Entry) Int64Default

func (e Entry) Int64Default(def int64) (int64, error)

Int64Default returns the entry's value as int64. If not found returns "def" and a non-nil error.

func (Entry) Int8Default

func (e Entry) Int8Default(def int8) (int8, error)

Int8Default returns the entry's value as int8. If not found returns "def" and a non-nil error.

func (Entry) IntDefault

func (e Entry) IntDefault(def int) (int, error)

IntDefault returns the entry's value as int. If not found returns "def" and a non-nil error.

func (Entry) String

func (e Entry) String() string

String returns the entry's value as string.

func (Entry) StringDefault

func (e Entry) StringDefault(def string) string

StringDefault returns the entry's value as string. If not found returns "def".

func (Entry) StringTrim

func (e Entry) StringTrim() string

StringTrim returns the entry's string value without trailing spaces.

func (Entry) TimeDefault added in v12.2.0

func (e Entry) TimeDefault(def time.Time) (time.Time, error)

TimeDefault returns the stored time.Time value based on its "key". If does not exist or the stored key's value is not a time it returns the "def" time value and a not found error.

func (Entry) Uint16Default

func (e Entry) Uint16Default(def uint16) (uint16, error)

Uint16Default returns the entry's value as uint16. If not found returns "def" and a non-nil error.

func (Entry) Uint32Default

func (e Entry) Uint32Default(def uint32) (uint32, error)

Uint32Default returns the entry's value as uint32. If not found returns "def" and a non-nil error.

func (Entry) Uint64Default

func (e Entry) Uint64Default(def uint64) (uint64, error)

Uint64Default returns the entry's value as uint64. If not found returns "def" and a non-nil error.

func (Entry) Uint8Default

func (e Entry) Uint8Default(def uint8) (uint8, error)

Uint8Default returns the entry's value as uint8. If not found returns "def" and a non-nil error.

func (Entry) UintDefault

func (e Entry) UintDefault(def uint) (uint, error)

UintDefault returns the entry's value as uint. If not found returns "def" and a non-nil error.

func (Entry) Value

func (e Entry) Value() interface{}

Value returns the value of the entry, respects the immutable.

func (Entry) WeekdayDefault added in v12.2.0

func (e Entry) WeekdayDefault(def time.Weekday) (time.Weekday, error)

WeekdayDefault returns the stored time.Weekday value based on its "key". If does not exist or the stored key's value is not a weekday it returns the "def" weekday value and a not found error.

type ErrEntryNotFound

type ErrEntryNotFound struct {
	Key  string       // the entry's key.
	Kind reflect.Kind // i.e bool, int, string...
	Type reflect.Type // i.e time.Time{} or custom struct.
}

ErrEntryNotFound may be returned from memstore methods if a key (with a certain kind) was not found. Usage:

var e *ErrEntryNotFound errors.As(err, &e) To check for specific key error: errors.As(err, &ErrEntryNotFound{Key: "key"}) To check for specific key-kind error: errors.As(err, &ErrEntryNotFound{Key: "key", Kind: reflect.Int})

func (*ErrEntryNotFound) As

func (e *ErrEntryNotFound) As(target interface{}) bool

As can be used to manually check if the error came from the memstore is a not found entry, the key must much in order to return true. Usage: errors.As(err, &ErrEntryNotFound{Key: "key", Kind: reflect.Int})

Do NOT use this method directly, prefer` errors.As` method as explained above.

Implements: go/src/errors/wrap.go#84

func (*ErrEntryNotFound) Error

func (e *ErrEntryNotFound) Error() string

type Store

type Store []Entry

Store is a collection of key-value entries with immutability capabilities.

func GobDecode

func GobDecode(b []byte) (store Store, err error)

GobDecode accepts a series of bytes and returns the store.

func (*Store) Exists added in v12.2.0

func (r *Store) Exists(key string) bool

Exists is a small helper which reports whether a key exists. It's not recommended to be used outside of templates. Use Get or GetEntry instead which will give you back the entry value too, so you don't have to loop again the key-value storage to get its value.

func (*Store) Get

func (r *Store) Get(key string) interface{}

Get returns the entry's value based on its key. If not found returns nil.

func (*Store) GetBool

func (r *Store) GetBool(key string) (bool, error)

GetBool returns the user's value as bool, based on its key. a string which is "1" or "t" or "T" or "TRUE" or "true" or "True" or "0" or "f" or "F" or "FALSE" or "false" or "False". Any other value returns an error.

If not found returns false and a non-nil error.

func (*Store) GetBoolDefault

func (r *Store) GetBoolDefault(key string, def bool) bool

GetBoolDefault returns the user's value as bool, based on its key. a string which is "1" or "t" or "T" or "TRUE" or "true" or "True" or "0" or "f" or "F" or "FALSE" or "false" or "False".

If not found returns "def".

func (*Store) GetDefault

func (r *Store) GetDefault(key string, def interface{}) interface{}

GetDefault returns the entry's value based on its key. If not found returns "def". This function checks for immutability as well, the rest don't.

func (*Store) GetEntry

func (r *Store) GetEntry(key string) (Entry, bool)

GetEntry returns a pointer to the "Entry" found with the given "key" if nothing found then it returns an empty Entry and false.

func (*Store) GetEntryAt

func (r *Store) GetEntryAt(index int) (Entry, bool)

GetEntryAt returns the internal Entry of the memstore based on its index, the stored index by the router. If not found then it returns a zero Entry and false.

func (*Store) GetFloat64

func (r *Store) GetFloat64(key string) (float64, error)

GetFloat64 returns the entry's value as float64, based on its key. If not found returns -1 and a non nil error.

func (*Store) GetFloat64Default

func (r *Store) GetFloat64Default(key string, def float64) float64

GetFloat64Default returns the entry's value as float64, based on its key. If not found returns "def".

func (*Store) GetInt

func (r *Store) GetInt(key string) (int, error)

GetInt returns the entry's value as int, based on its key. If not found returns -1 and a non-nil error.

func (*Store) GetInt16

func (r *Store) GetInt16(key string) (int16, error)

GetInt16 returns the entry's value as int16, based on its key. If not found returns -1 and a non-nil error.

func (*Store) GetInt16Default

func (r *Store) GetInt16Default(key string, def int16) int16

GetInt16Default returns the entry's value as int16, based on its key. If not found returns "def".

func (*Store) GetInt32

func (r *Store) GetInt32(key string) (int32, error)

GetInt32 returns the entry's value as int32, based on its key. If not found returns -1 and a non-nil error.

func (*Store) GetInt32Default

func (r *Store) GetInt32Default(key string, def int32) int32

GetInt32Default returns the entry's value as int32, based on its key. If not found returns "def".

func (*Store) GetInt64

func (r *Store) GetInt64(key string) (int64, error)

GetInt64 returns the entry's value as int64, based on its key. If not found returns -1 and a non-nil error.

func (*Store) GetInt64Default

func (r *Store) GetInt64Default(key string, def int64) int64

GetInt64Default returns the entry's value as int64, based on its key. If not found returns "def".

func (*Store) GetInt8

func (r *Store) GetInt8(key string) (int8, error)

GetInt8 returns the entry's value as int8, based on its key. If not found returns -1 and a non-nil error.

func (*Store) GetInt8Default

func (r *Store) GetInt8Default(key string, def int8) int8

GetInt8Default returns the entry's value as int8, based on its key. If not found returns "def".

func (*Store) GetIntDefault

func (r *Store) GetIntDefault(key string, def int) int

GetIntDefault returns the entry's value as int, based on its key. If not found returns "def".

func (*Store) GetOrSet added in v12.2.0

func (r *Store) GetOrSet(key string, setFunc func() interface{}) interface{}

GetOrSet is like `GetDefault` but it accepts a function which is fired and its result is used to `Set` if the "key" was not found or its value is nil.

func (*Store) GetString

func (r *Store) GetString(key string) string

GetString returns the entry's value as string, based on its key.

func (*Store) GetStringDefault

func (r *Store) GetStringDefault(key string, def string) string

GetStringDefault returns the entry's value as string, based on its key. If not found returns "def".

func (*Store) GetStringTrim

func (r *Store) GetStringTrim(name string) string

GetStringTrim returns the entry's string value without trailing spaces.

func (*Store) GetTime added in v12.2.0

func (r *Store) GetTime(key string) (time.Time, error)

GetTime returns the stored time.Time value based on its "key". If does not exist or the stored key's value is not a time it returns a zero time value and a not found error.

func (*Store) GetUint

func (r *Store) GetUint(key string) (uint, error)

GetUint returns the entry's value as uint, based on its key. If not found returns 0 and a non-nil error.

func (*Store) GetUint16

func (r *Store) GetUint16(key string) (uint16, error)

GetUint16 returns the entry's value as uint16, based on its key. If not found returns 0 and a non-nil error.

func (*Store) GetUint16Default

func (r *Store) GetUint16Default(key string, def uint16) uint16

GetUint16Default returns the entry's value as uint16, based on its key. If not found returns "def".

func (*Store) GetUint32

func (r *Store) GetUint32(key string) (uint32, error)

GetUint32 returns the entry's value as uint32, based on its key. If not found returns 0 and a non-nil error.

func (*Store) GetUint32Default

func (r *Store) GetUint32Default(key string, def uint32) uint32

GetUint32Default returns the entry's value as uint32, based on its key. If not found returns "def".

func (*Store) GetUint64

func (r *Store) GetUint64(key string) (uint64, error)

GetUint64 returns the entry's value as uint64, based on its key. If not found returns 0 and a non-nil error.

func (*Store) GetUint64Default

func (r *Store) GetUint64Default(key string, def uint64) uint64

GetUint64Default returns the entry's value as uint64, based on its key. If not found returns "def".

func (*Store) GetUint8

func (r *Store) GetUint8(key string) (uint8, error)

GetUint8 returns the entry's value as uint8, based on its key. If not found returns 0 and a non-nil error.

func (*Store) GetUint8Default

func (r *Store) GetUint8Default(key string, def uint8) uint8

GetUint8Default returns the entry's value as uint8, based on its key. If not found returns "def".

func (*Store) GetUintDefault

func (r *Store) GetUintDefault(key string, def uint) uint

GetUintDefault returns the entry's value as uint, based on its key. If not found returns "def".

func (*Store) GetWeekday added in v12.2.0

func (r *Store) GetWeekday(key string) (time.Weekday, error)

GetWeekday returns the stored time.Weekday value based on its "key". If does not exist or the stored key's value is not a weekday it returns the time.Sunday value and a not found error.

func (*Store) Len

func (r *Store) Len() int

Len returns the full length of the entries.

func (*Store) Remove

func (r *Store) Remove(key string) bool

Remove deletes an entry linked to that "key", returns true if an entry is actually removed.

func (*Store) Reset

func (r *Store) Reset()

Reset clears all the request entries.

func (*Store) Save

func (r *Store) Save(key string, value interface{}, immutable bool) (Entry, bool)

Save same as `Set` However, if "immutable" is true then saves it as immutable (same as `SetImmutable`).

Returns the entry and true if it was just inserted, meaning that it will return the entry and a false boolean if the entry exists and it has been updated.

func (Store) Serialize

func (r Store) Serialize() []byte

Serialize returns the byte representation of the current Store.

func (*Store) Set

func (r *Store) Set(key string, value interface{}) (Entry, bool)

Set saves a value to the key-value storage. Returns the entry and true if it was just inserted, meaning that it will return the entry and a false boolean if the entry exists and it has been updated.

See `SetImmutable` and `Get`.

func (*Store) SetImmutable

func (r *Store) SetImmutable(key string, value interface{}) (Entry, bool)

SetImmutable saves a value to the key-value storage. Unlike `Set`, the output value cannot be changed by the caller later on (when .Get OR .Set)

An Immutable entry should be only changed with a `SetImmutable`, simple `Set` will not work if the entry was immutable, for your own safety.

Returns the entry and true if it was just inserted, meaning that it will return the entry and a false boolean if the entry exists and it has been updated.

Use it consistently, it's far slower than `Set`. Read more about muttable and immutable go types: https://stackoverflow.com/a/8021081

func (*Store) SimpleDate added in v12.2.0

func (r *Store) SimpleDate(key string) string

SimpleDate calls GetTime and formats the time as "yyyyy/mm/dd". It returns an empty string if the key does not exist or the stored value on "key" is not a time.Time type.

func (*Store) Visit

func (r *Store) Visit(visitor func(key string, value interface{}))

Visit accepts a visitor which will be filled by the key-value objects.

type StringEntry added in v12.2.0

type StringEntry struct {
	Key   string `json:"key" msgpack:"key" yaml:"Key" toml:"Value"`
	Value string `json:"value" msgpack:"value" yaml:"Value" toml:"Value"`
}

StringEntry is just a key-value wrapped by a struct. See Context.URLParamsSorted method.

type ValueSetter

type ValueSetter interface {
	Set(key string, newValue interface{}) (Entry, bool)
}

ValueSetter is the interface which can be accepted as a generic solution of RequestParams or memstore when Set is the only requirement, i.e internally on macro/template/TemplateParam#Eval:paramChanger.

Jump to

Keyboard shortcuts

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