kvstr

package
v0.0.0-...-cbde7bb Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2023 License: MIT Imports: 7 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

This section is empty.

Types

type FieldName

type FieldName = 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`"}

type KVMap

type KVMap map[KeyName]string

func (KVMap) TryGet

func (m KVMap) TryGet(name string, opt *QueryOption) (string, bool)

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 KeyName, val string)) error

ForEach iterates over all key-value pairs

func (KVStr) ReflectTo

func (kv KVStr) ReflectTo(target any, queryOption *QueryOption) (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() (KVMap, error)

ToMap will return the result of converting kv str to map

type KeyName

type KeyName = 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`"}

type QueryOption

type QueryOption struct {
	// accept more key that can be converted to the key of kvMap
	TryMapping map[string]KeyName
	TrySnake   bool
	TryCamel   bool
}

Jump to

Keyboard shortcuts

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