Documentation ¶
Overview ¶
Package keyvalue provides the Key-Value based access utility.
Index ¶
- func GetDateOr(g Getter, key interface{}, or time.Time) time.Time
- func GetFloatOr(g Getter, key interface{}, or float64) float64
- func GetIntOr(g Getter, key interface{}, or int) int
- func GetOr(g Getter, key interface{}, or interface{}) interface{}
- func GetStringOr(g Getter, key interface{}, or string) string
- func IsKeyError(err error) bool
- type GetProxy
- func (p *GetProxy) Get(key string) (interface{}, error)
- func (p *GetProxy) GetDateOr(key string, or time.Time) time.Time
- func (p *GetProxy) GetFloatOr(key string, or float64) float64
- func (p *GetProxy) GetIntOr(key string, or int) int
- func (p *GetProxy) GetOr(key string, or interface{}) interface{}
- func (p *GetProxy) GetStringOr(key string, or string) string
- type Getter
- type GetterFunc
- type GetterSetter
- type GetterStringKeyFunc
- type KeyError
- type List
- type Map
- type Setter
- type StringKeyMap
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetDateOr ¶
Example ¶
m := Map{ "Date": "2017/01/01", } fmt.Println(GetDateOr(m, "Date", time.Date(2017, time.Month(12), 31, 0, 0, 0, 0, xtime.JST))) fmt.Println(GetDateOr(m, "Invalid", time.Date(2017, time.Month(12), 31, 0, 0, 0, 0, xtime.JST)))
Output: 2017-01-01 00:00:00 +0900 JST 2017-12-31 00:00:00 +0900 JST
func GetFloatOr ¶
GetFloatOr is float64 version of GetOr.
func GetIntOr ¶
GetIntOr is int version of GetOr.
Example ¶
m := Map{ "Foo": 1, "Int8": int8(1), "StringNum": "1", "InvalidStringNum": "a", } fmt.Println(GetIntOr(m, "Foo", 1)) fmt.Println(GetIntOr(m, "Int8", 2)) fmt.Println(GetIntOr(m, "StringNum", 2)) fmt.Println(GetIntOr(m, "InvalidStringNum", 2))
Output: 1 1 1 2
func GetOr ¶
func GetOr(g Getter, key interface{}, or interface{}) interface{}
GetOr gets a value from Getter or return the defalut `or` value if not found.
func GetStringOr ¶
GetStringOr is string version of GetOr.
Types ¶
type GetProxy ¶
type GetProxy struct {
// contains filtered or unexported fields
}
GetProxy provies the proxy functions for Getter to use Get* package functions. When you implements Getter interface, you can embed GetProxy to proxy itself to provide Get* functions on that struct.
type MyGetter struct { ... *keyvalue.GetProxy // To } func NewMyGetter() *MyGetter { g := &MyGetter{} ... g.GetProxy = keyvalue.GetProxy(g) // proxy self. return g }
func NewGetProxy ¶
NewGetProxy returns a new GetProxy for Getter
func NewQueryProxy ¶
NewQueryProxy returns *GetProxy for url.Values
func (*GetProxy) GetFloatOr ¶
GetFloatOr is shorthand for keyvalue.GetFloatOr.
type Getter ¶
type Getter interface {
Get(interface{}) (interface{}, error)
}
Getter is an interface to get a value by a key
type GetterFunc ¶
type GetterFunc func(interface{}) (interface{}, error)
GetterFunc is to create a proxy wrapper for Getter
func (GetterFunc) Get ¶
func (f GetterFunc) Get(key interface{}) (interface{}, error)
Get implements Getter#Get
func (GetterFunc) Proxy ¶
func (f GetterFunc) Proxy() *GetProxy
Proxy returns GetProxy for this func.
type GetterSetter ¶
type GetterSetter interface { Get(interface{}) (interface{}, error) Set(interface{}, interface{}) error }
GetterSetter is an interface to get/set a valeu by a key
type GetterStringKeyFunc ¶
GetterStringKeyFunc is to create a proxy wrapper for Getter
func (GetterStringKeyFunc) Get ¶
func (f GetterStringKeyFunc) Get(key interface{}) (interface{}, error)
Get implements Getter#Get
func (GetterStringKeyFunc) Proxy ¶
func (f GetterStringKeyFunc) Proxy() *GetProxy
Proxy returns GetProxy for this func.
type List ¶
type List struct { *GetProxy // contains filtered or unexported fields }
List is a list of key-value store.
Example ¶
m1 := Map{ "Foo": "1", } m2 := Map{ "Foo": "3", "Bar": "2", } list := NewList(m1, m2) fmt.Println(list.GetStringOr("Foo", "1")) fmt.Println(list.GetStringOr("Bar", "2")) fmt.Println(list.GetStringOr("Hoge", "-"))
Output: 1 2 -
type Map ¶
type Map map[interface{}]interface{}
Map is an alias for map[interface{}]interface{} that implements GetterSetter interface
type Setter ¶
type Setter interface {
Set(interface{}, interface{}) error
}
Setter is an interface to set a value bey a key
type StringKeyMap ¶
type StringKeyMap map[string]interface{}
StringKeyMap is an alias for map[string]interface{} that implements GetterSetter interface
func NewStringKeyMap ¶
func NewStringKeyMap() StringKeyMap
NewStringKeyMap returns a new Map (shorthand for `Map(make(map[string]interface{}))`)
func (StringKeyMap) Del ¶
func (m StringKeyMap) Del(key interface{}) error
Del implements Setter#Del
func (StringKeyMap) Get ¶
func (m StringKeyMap) Get(key interface{}) (interface{}, error)
Get implements Getter#Get
func (StringKeyMap) Set ¶
func (m StringKeyMap) Set(key interface{}, v interface{}) error
Set implements Setter#Set