vars

package module
v6.0.0-...-723d0cb Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: MIT Imports: 11 Imported by: 1

README

VARS

license PkgGoDev tests Go Report Card Coverage Status benchmarks GitHub last commit

About

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

Install

go get github.com/mkungla/vars/v6

Usage

working with vars.Value

package main

import (
  "fmt"
  "github.com/mkungla/vars/v6"
)

func main() {
  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
}

working with vars.Collection

Because of underlying sync.Map it is meant to be populated once and read many times read thoroughly sync.Map docs to understand where .Collection may not me right for you!

package main

import (
  "fmt"
  "github.com/mkungla/vars/v6"
)

func main() {
  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
}

read collection from file

package main

import (
  "fmt"
  "io/ioutil"
  "github.com/mkungla/vars/v6"
)

func main() {
  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
}

License

Package vars is licensed under the MIT LICENSE.

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

Examples

Constants

This section is empty.

Variables

View Source
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) Len

func (c *Collection) Len() int

Len of collection.

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 Type

type Type uint

Type represents type of raw value.

const (
	TypeUnknown Type = iota
	TypeString
	TypeBool
	TypeFloat32
	TypeFloat64
	TypeComplex64
	TypeComplex128
	TypeInt
	TypeInt8
	TypeInt16
	TypeInt32
	TypeInt64
	TypeUint
	TypeUint8
	TypeUint16
	TypeUint32
	TypeUint64
	TypeUintptr
	TypeBytes
	TypeRunes
	TypeMap
	TypeReflectVal
	TypeDuration
	TypeArray
)

func (Type) String

func (t Type) String() string

nolint: funlen, cyclop

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

func NewTypedValue(val string, vtype Type) (Value, error)

NewTypedValue tries to parse value to given type. nolint: cyclop

func NewValue

func NewValue(val any) Value

NewValue return Value, If error occurred while parsing VAR represents default 0, nil value.

func (Value) Bool

func (v Value) Bool() bool

Bool returns boolean representation of the var Value.

func (Value) Bytes

func (v Value) Bytes() []byte

Bytes returns []bytes representation of the Value.

func (Value) Complex128

func (v Value) Complex128() complex128

Complex128 returns complex128 representation of the Value.

func (Value) Complex64

func (v Value) Complex64() complex64

Complex64 returns complex64 representation of the Value.

func (Value) Empty

func (v Value) Empty() bool

Empty returns true if this Value is empty.

func (Value) Fields

func (v Value) Fields() []string

Fields calls strings.Fields on Value string.

func (Value) Float32

func (v Value) Float32() float32

Float32 returns Float32 representation of the Value.

func (Value) Float64

func (v Value) Float64() float64

Float64 returns float64 representation of Value.

func (Value) Int

func (v Value) Int() int

Int returns int representation of the Value.

func (Value) Int16

func (v Value) Int16() int16

Int16 returns int16 representation of the Value.

func (Value) Int32

func (v Value) Int32() int32

Int32 returns int32 representation of the Value.

func (Value) Int64

func (v Value) Int64() int64

Int64 returns int64 representation of the Value.

func (Value) Int8

func (v Value) Int8() int8

Int8 returns int8 representation of the Value.

func (Value) Len

func (v Value) Len() int

Len returns the length of the string representation of the Value.

func (Value) Raw

func (v Value) Raw() any

func (Value) Runes

func (v Value) Runes() []rune

Runes returns []rune representation of the var value.

func (Value) String

func (v Value) String() string

String returns string representation of the Value.

func (Value) Type

func (v Value) Type() Type

Type of value.

func (Value) Uint

func (v Value) Uint() uint

Uint returns uint representation of the Value.

func (Value) Uint16

func (v Value) Uint16() uint16

Uint16 returns uint16 representation of the Value.

func (Value) Uint32

func (v Value) Uint32() uint32

Uint32 returns uint32 representation of the Value.

func (Value) Uint64

func (v Value) Uint64() uint64

Uint64 returns uint64 representation of the Value.

func (Value) Uint8

func (v Value) Uint8() uint8

Uint8 returns uint8 representation of the Value.

func (Value) Uintptr

func (v Value) Uintptr() uintptr

Uintptr returns uintptr representation of the Value.

type Variable

type Variable struct {
	// contains filtered or unexported fields
}

Variable is universl representation of key val pair.

func New

func New(key string, val interface{}) Variable

New return untyped Variable, If error occurred while parsing Variable represents default 0, nil value.

func NewFromKeyVal

func NewFromKeyVal(kv string) (v Variable, err error)

NewFromKeyVal parses variable from single "key=val" pair and returns Variable.

func NewTyped

func NewTyped(key string, val string, vtype Type) (Variable, error)

NewTyped parses variable and sets appropriately parser error for given type if parsing to requested type fails.

func (Variable) Bool

func (v Variable) Bool() bool

Bool returns boolean representation of the var value.

func (Variable) Bytes

func (v Variable) Bytes() []byte

Bytes returns []bytes representation of the var value.

func (Variable) Complex128

func (v Variable) Complex128() complex128

Complex128 returns complex128 representation of the var value.

func (Variable) Complex64

func (v Variable) Complex64() complex64

Complex64 returns complex64 representation of the var value.

func (Variable) Empty

func (v Variable) Empty() bool

Empty returns true if this Value is empty.

func (Variable) Fields

func (v Variable) Fields() []string

Fields calls strings.Fields on Value string.

func (Variable) Float32

func (v Variable) Float32() float32

Float32 returns Float32 representation of the var value.

func (Variable) Float64

func (v Variable) Float64() float64

Float64 returns float64 representation of the var value.

func (Variable) Int

func (v Variable) Int() int

Int returns int representation of the var value.

func (Variable) Int16

func (v Variable) Int16() int16

Int16 returns int16 representation of the var value.

func (Variable) Int32

func (v Variable) Int32() int32

Int32 returns int32 representation of the var value.

func (Variable) Int64

func (v Variable) Int64() int64

Int64 returns int64 representation of the var value.

func (Variable) Int8

func (v Variable) Int8() int8

Int8 returns int8 representation of the var value.

func (Variable) Key

func (v Variable) Key() string

Key returns assigned key for this variable.

func (Variable) Len

func (v Variable) Len() int

Len returns the length of the string representation of the Value.

func (Variable) Runes

func (v Variable) Runes() []rune

Runes returns []rune representation of the var value.

func (Variable) String

func (v Variable) String() string

String returns string representation of the var value.

func (Variable) Type

func (v Variable) Type() Type

Type of original variable.

func (Variable) Uint

func (v Variable) Uint() uint

Uint returns uint representation of the var value.

func (Variable) Uint16

func (v Variable) Uint16() uint16

Uint16 returns uint16 representation of the var value.

func (Variable) Uint32

func (v Variable) Uint32() uint32

Uint32 returns uint32 representation of the var value.

func (Variable) Uint64

func (v Variable) Uint64() uint64

Uint64 returns uint64 representation of the var value.

func (Variable) Uint8

func (v Variable) Uint8() uint8

Uint8 returns uint8 representation of the var value.

func (Variable) Uintptr

func (v Variable) Uintptr() uintptr

Uintptr returns uintptr representation of the var value.

func (Variable) Value

func (v Variable) Value() Value

Value returns Value of variable.

Jump to

Keyboard shortcuts

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