Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "sync" "time" "github.com/rvflash/eve" ) const ( boolVal = true intVal = 42 floatVal = 3.14 strVal = "rv" hostVal = "http://sh01.prod" portVal = 8080 toVal = "300ms" ) func main() { vars := eve.New("test", server) if err := vars.Envs("qa", "fr"); err != nil { fmt.Println(err) return } if vars.MustBool("bool") { str, _ := vars.String("str") fmt.Print(str) } if d, ok := vars.Lookup("int"); ok { fmt.Printf(": %d", d.(int)) } } // newClient returns a test client to fake a RPC cache. func newClient(d time.Duration) *handler { c := &handler{} c.timer = time.AfterFunc(d, func() { c.mu.Lock() c.offline = true c.mu.Unlock() }) return c } type handler struct { timer *time.Timer offline bool mu sync.Mutex } // Lookup implements the client.Getter interface. func (c *handler) Lookup(key string) (interface{}, bool) { c.mu.Lock() defer c.mu.Unlock() if c.offline { return nil, false } switch key { case "TEST_BOOL", "TEST_QA_FR_BOOL": return boolVal, true case "TEST_INT", "TEST_QA_FR_INT": return intVal, true case "TEST_FLOAT", "TEST_QA_FR_FLOAT": return floatVal, true case "TEST_STR", "TEST_QA_FR_STR": return strVal, true case "TEST_QA_FR_HOST": return hostVal, true case "TEST_QA_FR_PORT": return portVal, true case "TEST_QA_FR_TO": return toVal, true } return nil, false } // Lookup implements the client.Checker interface. func (c *handler) Available() bool { c.mu.Lock() defer c.mu.Unlock() return !c.offline } var server = newClient(time.Minute)
Output: rv: 42
Index ¶
- Variables
- func PartialServers(addr ...string) (caches []client.Getter, partial bool, err error)
- func Servers(addr ...string) ([]client.Getter, error)
- type Client
- func (c *Client) Bool(key string) (bool, error)
- func (c *Client) Envs(envs ...string) error
- func (c *Client) Float64(key string) (float64, error)
- func (c *Client) Get(key string) interface{}
- func (c *Client) Int(key string) (int, error)
- func (c *Client) Lookup(key string) (interface{}, bool)
- func (c *Client) MustBool(key string) bool
- func (c *Client) MustFloat64(key string) float64
- func (c *Client) MustInt(key string) int
- func (c *Client) MustProcess(spec interface{})
- func (c *Client) MustString(key string) string
- func (c *Client) Process(spec interface{}) error
- func (c *Client) String(key string) (string, error)
- func (c *Client) UseHandler(h Handler) *Client
- type Handler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalid is returned is the data is not well formed. ErrInvalid = errors.New("invalid data") // ErrNotFound is returned if the variable is not found. ErrNotFound = errors.New("not found") // ErrDataSource is returned if the RPC service is not available. ErrDataSource = errors.New("no available rpc service") // ErrNoPointer is returned if the element to manage is not pointer. ErrNoPointer = errors.New("mandatory struct pointer") )
Error messages.
var ( // Cache represents a local in-memory cache. Cache = client.NewCache(client.DefaultCacheDuration) // OS gives access to the environment variables. OS = &client.OS{} )
Initializes the data sources.
var Tick = time.Second * 30
Tick is the time duration to sleep before checking if at least one RPC cache is available.
Functions ¶
func PartialServers ¶ added in v0.0.9
PartialServers tries to connect to each net address and do not care of connection errors. If a connection error occurred, the error is discarded and the process continues.
Types ¶
type Client ¶
type Client struct { Handler // contains filtered or unexported fields }
Client represents the EVE client to handle the data sources.
func New ¶
New returns an instance of a Client. The first parameter is the project's identifier. The second, optional, represents a list of data getter. By default, eve tries to get the variable's value: > In its own memory cache. > In the list of available environment variables. > in the other date getter like RPC cache. The Eve client only sets variables in its own cache.
func (*Client) Envs ¶
Envs allows to define until 2 environments. The adding's order is important, the first must be the first environment defined in the EVE's project. It returns an error if the number of environment is unexpected.
func (*Client) Get ¶ added in v0.0.1
Get retrieves the value of the environment variable named by the key. If it not exists, a nil value is returned.
func (*Client) Lookup ¶
Lookup retrieves the value of the environment variable named by the key. If it not exists, the second boolean will be false.
func (*Client) MustFloat64 ¶
MustFloat64 is like Float64 but panics if the variable cannot be retrieved.
func (*Client) MustProcess ¶ added in v0.0.2
func (c *Client) MustProcess(spec interface{})
MustProcess is like Process but panics if it fails to feed the spec.
func (*Client) MustString ¶
MustString is like String but panics if the variable cannot be retrieved.
func (*Client) Process ¶ added in v0.0.2
Process uses the reflection to assign values on each element. It returns on error if one on its fields can not be retrieved.
Example ¶
package main import ( "fmt" "sync" "time" "github.com/rvflash/eve" ) const ( boolVal = true intVal = 42 floatVal = 3.14 strVal = "rv" hostVal = "http://sh01.prod" portVal = 8080 toVal = "300ms" ) type exFields struct { Addr string `eve:"host" required:"true"` Port int Timeout time.Duration `eve:"to"` Retry bool } func main() { vars := eve.New("test", server) if err := vars.Envs("qa", "fr"); err != nil { fmt.Println(err) return } var mycnf exFields if err := vars.Process(&mycnf); err != nil { fmt.Println(err) return } fmt.Printf( "%s:%d, to=%.1fs, retry=%v", mycnf.Addr, mycnf.Port, mycnf.Timeout.Seconds(), mycnf.Retry, ) } // newClient returns a test client to fake a RPC cache. func newClient(d time.Duration) *handler { c := &handler{} c.timer = time.AfterFunc(d, func() { c.mu.Lock() c.offline = true c.mu.Unlock() }) return c } type handler struct { timer *time.Timer offline bool mu sync.Mutex } // Lookup implements the client.Getter interface. func (c *handler) Lookup(key string) (interface{}, bool) { c.mu.Lock() defer c.mu.Unlock() if c.offline { return nil, false } switch key { case "TEST_BOOL", "TEST_QA_FR_BOOL": return boolVal, true case "TEST_INT", "TEST_QA_FR_INT": return intVal, true case "TEST_FLOAT", "TEST_QA_FR_FLOAT": return floatVal, true case "TEST_STR", "TEST_QA_FR_STR": return strVal, true case "TEST_QA_FR_HOST": return hostVal, true case "TEST_QA_FR_PORT": return portVal, true case "TEST_QA_FR_TO": return toVal, true } return nil, false } // Lookup implements the client.Checker interface. func (c *handler) Available() bool { c.mu.Lock() defer c.mu.Unlock() return !c.offline } var server = newClient(time.Minute)
Output: http://sh01.prod:8080, to=0.3s, retry=false
func (*Client) UseHandler ¶
UseHandler defines a new handler to use. It returns the updated client.