keyvalue

package
v0.0.0-...-519d24f Latest Latest
Warning

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

Go to latest
Published: May 5, 2020 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package keyvalue provides the Key-Value based access utility.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDateOr

func GetDateOr(g Getter, key interface{}, or time.Time) time.Time
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

func GetFloatOr(g Getter, key interface{}, or float64) float64

GetFloatOr is float64 version of GetOr.

func GetIntOr

func GetIntOr(g Getter, key interface{}, or int) int

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

func GetStringOr(g Getter, key interface{}, or string) string

GetStringOr is string version of GetOr.

func IsKeyError

func IsKeyError(err error) bool

IsKeyError returns the error is KeyError or not

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

func NewGetProxy(g Getter) *GetProxy

NewGetProxy returns a new GetProxy for Getter

func NewQueryProxy

func NewQueryProxy(query url.Values) *GetProxy

NewQueryProxy returns *GetProxy for url.Values

func (*GetProxy) Get

func (p *GetProxy) Get(key string) (interface{}, error)

Get shorthand accses to underling Getter.Get.

func (*GetProxy) GetDateOr

func (p *GetProxy) GetDateOr(key string, or time.Time) time.Time

func (*GetProxy) GetFloatOr

func (p *GetProxy) GetFloatOr(key string, or float64) float64

GetFloatOr is shorthand for keyvalue.GetFloatOr.

func (*GetProxy) GetIntOr

func (p *GetProxy) GetIntOr(key string, or int) int

GetIntOr is shorthand for keyvalue.GetIntOr.

func (*GetProxy) GetOr

func (p *GetProxy) GetOr(key string, or interface{}) interface{}

GetOr is shorthand for keyvalue.GetOr.

func (*GetProxy) GetStringOr

func (p *GetProxy) GetStringOr(key string, or string) string

GetStringOr is shorthand for keyvalue.GetStringOr.

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

type GetterStringKeyFunc func(string) (interface{}, error)

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 KeyError

type KeyError string

KeyError is the error when the key is not found.

func (KeyError) Error

func (e KeyError) Error() string

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
-

func NewList

func NewList(g ...Getter) *List

NewList returns a new *List for `g`

func (*List) Get

func (l *List) Get(key interface{}) (interface{}, error)

Get try to get the value from the head of list.

  • If a Getter item returns the value, it is returned.
  • If a Getter item returns an error other than KeyError, it fails and return that error immediately.
  • If a Getter item returns KeyError, it tries the next Getter item.

type Map

type Map map[interface{}]interface{}

Map is an alias for map[interface{}]interface{} that implements GetterSetter interface

func NewMap

func NewMap() Map

NewMap returns a new Map (shorthand for `Map(make(map[string]interface{}))`)

func (Map) Get

func (m Map) Get(key interface{}) (interface{}, error)

Get implements Getter#Get

func (Map) Set

func (m Map) Set(key interface{}, v interface{}) error

Set implements Setter#Set

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

Directories

Path Synopsis
Package config provies configuraiton access functions
Package config provies configuraiton access functions

Jump to

Keyboard shortcuts

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