kvstr

package
v0.0.0-...-86b5199 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKVStrInvalidToken = errors.New("kv-str invalid token")
	ErrKVStrReflectError = errors.New("kv-str reflect error")
)

Functions

func ConvStrToPlainType

func ConvStrToPlainType(str string, p reflect.Type) (interface{}, error)

ConvStrToPlainType takes any string str and convert it into any plain types (possibly truncated)

Types

type KVStr

type KVStr string

KVStr is a string containing a set of key value pairs

By convention, KV strings are optionally concatenated with space-separated key=value pairs. Each key=value pair is separated by a comma (U+002C ',') or semicolon (U+003B ';')

Each key is a non-empty string that satisfies the syntax of go identity. Value can have four types:

numeric: Any integer, floating point, or complex number

  	that satisfies the go syntax
	e.g. `a=1,b=2.,c=.3,d=01 ,e=10e2`
	=> {a:1, b:2, c:.3, d:01, e:10e2}

character: Any character enclosed by single-quotes `'`

e.g. `char='x'` => {char:x}

boolean: Only the value of `false` means that the value

is false, in other cases you can just write key,
the `=value` part can be omitted
e.g. `switch,window` => {switch:true, window:true}

string: Any character enclosed by back-quotes ```, or

any character concatenation to the next separator
that is not one of the above cases, where
consecutive whitespace characters are treated as
a single whitespace character
e.g. `str=this   is a string,str2=`quoted string``
=> {str:"this is a string", str2="`quoted string`"}

func (KVStr) ForEach

func (kv KVStr) ForEach(fn func(key, val string)) error

ForEach iterates over all key-value pairs

func (KVStr) ReflectTo

func (kv KVStr) ReflectTo(target interface{}) (extra map[string]string, err error)

ReflectTo copies kv by name to a struct

The key used to assign a value should be equal to the name of the field corresponding to the target struct, or the snake form of its name.

e.g. The field `FieldName` can be assigned by the

key `FieldName` or `field_name`.

func (KVStr) ToMap

func (kv KVStr) ToMap() (map[string]string, error)

ToMap will return the result of converting kv str to map

Jump to

Keyboard shortcuts

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