Documentation ¶
Overview ¶
Package vars provides the API to parse variables from various input formats/types to common key value pair vars.Value or variable sets to vars.Collection
Index ¶
- Variables
- type Collection
- func (c *Collection) Delete(key string)
- func (c *Collection) Get(k string, defval ...interface{}) (val Value)
- func (c *Collection) GetWithPrefix(prfx string) (vars *Collection)
- func (c *Collection) Has(k string) bool
- func (c *Collection) Len() int
- func (c *Collection) Load(key string) (value Value, ok bool)
- func (c *Collection) LoadAndDelete(key string) (value Value, loaded bool)
- func (c *Collection) LoadOrStore(key string, value interface{}) (actual Value, loaded bool)
- func (c *Collection) MarshalJSON() ([]byte, error)
- func (c *Collection) Range(f func(key string, value Value) bool)
- func (c *Collection) Set(k string, v interface{})
- func (c *Collection) Store(key string, value any)
- func (c *Collection) ToBytes() []byte
- func (c *Collection) ToKeyValSlice() []string
- type Type
- type Value
- func (v Value) Bool() bool
- func (v Value) Bytes() []byte
- func (v Value) Complex128() complex128
- func (v Value) Complex64() complex64
- func (v Value) Empty() bool
- func (v Value) Fields() []string
- func (v Value) Float32() float32
- func (v Value) Float64() float64
- func (v Value) Int() int
- func (v Value) Int16() int16
- func (v Value) Int32() int32
- func (v Value) Int64() int64
- func (v Value) Int8() int8
- func (v Value) Len() int
- func (v Value) Raw() any
- func (v Value) Runes() []rune
- func (v Value) String() string
- func (v Value) Type() Type
- func (v Value) Uint() uint
- func (v Value) Uint16() uint16
- func (v Value) Uint32() uint32
- func (v Value) Uint64() uint64
- func (v Value) Uint8() uint8
- func (v Value) Uintptr() uintptr
- type Variable
- func (v Variable) Bool() bool
- func (v Variable) Bytes() []byte
- func (v Variable) Complex128() complex128
- func (v Variable) Complex64() complex64
- func (v Variable) Empty() bool
- func (v Variable) Fields() []string
- func (v Variable) Float32() float32
- func (v Variable) Float64() float64
- func (v Variable) Int() int
- func (v Variable) Int16() int16
- func (v Variable) Int32() int32
- func (v Variable) Int64() int64
- func (v Variable) Int8() int8
- func (v Variable) Key() string
- func (v Variable) Len() int
- func (v Variable) Runes() []rune
- func (v Variable) String() string
- func (v Variable) Type() Type
- func (v Variable) Uint() uint
- func (v Variable) Uint16() uint16
- func (v Variable) Uint32() uint32
- func (v Variable) Uint64() uint64
- func (v Variable) Uint8() uint8
- func (v Variable) Uintptr() uintptr
- func (v Variable) Value() Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrVariableKeyEmpty is used when variable key is empty string. ErrVariableKeyEmpty = errors.New("variable key can not be empty") // EmptyVar variable. EmptyVar = Variable{} //nolint: gochecknoglobals )
Functions ¶
This section is empty.
Types ¶
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
Collection is like a Go sync.Map safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.
The zero Map is empty and ready for use. A Map must not be copied after first use.
Example ¶
collection := vars.ParseKeyValSlice([]string{ "key1=val1", "key2=2", "_key31=true", "_key32=true", "_key33=true", "_key34=true", }) collection.Set("other4", "1.001") set := collection.GetWithPrefix("_key3") var keys []string set.Range(func(key string, val vars.Value) bool { keys = append(keys, key) return true }) sort.Strings(keys) for _, k := range keys { fmt.Println(k) } fmt.Println(collection.Get("other4").Float64())
Output: _key31 _key32 _key33 _key34 1.001
Example (Envfile) ¶
content, err := ioutil.ReadFile("testdata/dot_env") if err != nil { fmt.Println(err) return } collection := vars.ParseFromBytes(content) goarch := collection.Get("GOARCH") fmt.Printf("GOARCH = %s\n", goarch)
Output: GOARCH = amd64
func ParseFromBytes ¶
func ParseFromBytes(b []byte) *Collection
ParseFromBytes parses []bytes to string, creates []string by new line and calls ParseFromStrings.
func ParseKeyValSlice ¶
func ParseKeyValSlice(kv []string) *Collection
ParseKeyValSlice parses variables from any []"key=val" slice and returns Collection.
func (*Collection) Delete ¶
func (c *Collection) Delete(key string)
Delete deletes the value for a key.
func (*Collection) Get ¶
func (c *Collection) Get(k string, defval ...interface{}) (val Value)
Get retrieves the value of the variable named by the key. It returns the value, which will be empty string if the variable is not set or value was empty.
func (*Collection) GetWithPrefix ¶
func (c *Collection) GetWithPrefix(prfx string) (vars *Collection)
GetWithPrefix return all variables with prefix if any as map[].
func (*Collection) Has ¶
func (c *Collection) Has(k string) bool
Has reprts whether given variable exists.
func (*Collection) Load ¶
func (c *Collection) Load(key string) (value Value, ok bool)
Load returns the variable stored in the Collection for a key, or EmptyVar if no value is present. The ok result indicates whether variable was found in the Collection.
func (*Collection) LoadAndDelete ¶
func (c *Collection) LoadAndDelete(key string) (value Value, loaded bool)
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
func (*Collection) LoadOrStore ¶
func (c *Collection) LoadOrStore(key string, value interface{}) (actual Value, loaded bool)
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
func (*Collection) MarshalJSON ¶
func (c *Collection) MarshalJSON() ([]byte, error)
func (*Collection) Range ¶
func (c *Collection) Range(f func(key string, value Value) bool)
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.
Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently, Range may reflect any mapping for that key from any point during the Range call.
Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.
func (*Collection) Set ¶
func (c *Collection) Set(k string, v interface{})
Set updates key value pair in collection. if not set.
func (*Collection) Store ¶
func (c *Collection) Store(key string, value any)
Store stores the variable for a variable.Key().
func (*Collection) ToBytes ¶
func (c *Collection) ToBytes() []byte
ToBytes returns []byte containing key = "value"\n.
func (*Collection) ToKeyValSlice ¶
func (c *Collection) ToKeyValSlice() []string
ToKeyValSlice produces []string slice of strings in format key = "value".
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value describes the variable value.
Example ¶
vnil := vars.NewValue(nil) fmt.Printf("%t\n", vnil.Type() == vars.TypeUnknown) fmt.Println(vnil.String()) v := vars.NewValue(123456) fmt.Printf("%t\n", v.Type() == vars.TypeInt) fmt.Println(v.String()) fmt.Println(v.Int()) fmt.Println(v.Empty()) fmt.Println(v.Int64()) fmt.Println(v.Float32()) fmt.Println(v.Float64()) fmt.Println(v.Len()) fmt.Println(v.Runes()) fmt.Println(v.Uint64()) fmt.Println(v.Uintptr())
Output: true <nil> true 123456 123456 false 123456 123456 123456 6 [49 50 51 52 53 54] 123456 123456
func NewTypedValue ¶
NewTypedValue tries to parse value to given type. nolint: cyclop
func NewValue ¶
NewValue return Value, If error occurred while parsing VAR represents default 0, nil value.
func (Value) Complex128 ¶
func (v Value) Complex128() complex128
Complex128 returns complex128 representation of the Value.
type Variable ¶
type Variable struct {
// contains filtered or unexported fields
}
Variable is universl representation of key val pair.
func New ¶
New return untyped Variable, If error occurred while parsing Variable represents default 0, nil value.
func NewFromKeyVal ¶
NewFromKeyVal parses variable from single "key=val" pair and returns Variable.
func NewTyped ¶
NewTyped parses variable and sets appropriately parser error for given type if parsing to requested type fails.
func (Variable) Complex128 ¶
func (v Variable) Complex128() complex128
Complex128 returns complex128 representation of the var value.