strs

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2021 License: MIT Imports: 8 Imported by: 3

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 Conv2Camel

func Conv2Camel(name string) (camel string)

func Conv2PlainType

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

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

func Conv2Snake

func Conv2Snake(name string) (snake string)

func Conv2SnakeAndCamel

func Conv2SnakeAndCamel(name string) (snake, camel string)

func StartsWith

func StartsWith(str, prefix string) bool

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