Documentation ¶
Overview ¶
Package utils implements general purpose utility
Index ¶
- Variables
- func Decode(src io.Reader, dest interface{}) error
- func Encode(src interface{}, dest io.Writer) error
- func GenerateID(totalLength int, prefix string) string
- func GenerateRandomString(n int) string
- func GetEnv(key string, defaultVal string) string
- func GetEnvAsSlice(name string, defaultVal []string, sep string) []string
- func GetEnvBool(key string, defaultVal bool) bool
- func GetEnvInt(key string, defaultVal int) int
- func GetEnvMust(key string) string
- func GetHash(val string) string
- func GetHostName() string
- func IsASCII(s string) bool
- func IsUTF8(s string) bool
- func LenientJSONTransformer(src interface{}, dest interface{}) error
- func Prepend[T any](a []T, b T) []T
- func StrictJSONTransformer(src interface{}, dest interface{}) error
- type CustomJsonTagHandler
- type Message
- type Payload
Constants ¶
This section is empty.
Variables ¶
var BodyJson = NewCustomJsonTagHandler("body")
var HeaderJson = NewCustomJsonTagHandler("header")
Functions ¶
func GenerateID ¶
GenerateID generates a id with the prefix and totalLength
x := GenerateID(20, "cust_")
In the above example len(x) would be equal to 20, prefix "cust_" of length 5 and the remaining slots(15) will be filled by random characters regex: ^cust_[a-zA-Z0-9]{15}$
func GenerateRandomString ¶
GenerateRandomString generates a random string of n characters, the generated string will of form ^[A-Za-z0-9]{n}$
func GetEnvBool ¶
func GetEnvMust ¶
func GetHostName ¶
func GetHostName() string
func LenientJSONTransformer ¶
func LenientJSONTransformer(src interface{}, dest interface{}) error
LenientJSONTransformer copies fields from src(map/ struct object) to dest struct object
Example:
a := map[string]any{ "key1": "value1", "key2": 10, "key3": false, "key4": "abc", } type sample struct { Key1 string B int `json:"key2"` C bool `json:"key3"` } b := &sample{} err := utils.LenientJSONTransformer(a, b) if err != nil { assert.NilError(t, err) } fmt.Printf("%+v", b) // Output: &{Key1:value1 B:10 C:false}
func Prepend ¶
func Prepend[T any](a []T, b T) []T
Prepend add a element[T] to the beginning of a list[T] it appends
func StrictJSONTransformer ¶
func StrictJSONTransformer(src interface{}, dest interface{}) error
StrictJSONTransformer copies fields from src(map/ struct object) to dest struct object throws error if there are keys in src that dosen't have slot in dest
Example 1:
a := map[string]any{ "key1": "value1", "key2": 10, "key3": false, } type sample struct { Key1 string B int `json:"key2"` C bool `json:"key3"` } b := &sample{} err := utils.StrictJSONTransformer(a, b) if err != nil { // err is nil ... } fmt.Printf("%+v", b) // Output: &{Key1:value1 B:10 C:false}
Example 2:
a := map[string]any{ "key1": "value1", "key2": 10, "key3": false, "key4":"fasf", } type sample struct { Key1 string B int `json:"key2"` C bool `json:"key3"` } b := &sample{} err := utils.StrictJSONTransformer(a, b) if err != nil { //err is not nil fmt.Print(err) // Output: StrictJsonTransformer: error decoding content: utils_test.TestVal.ReadObject: found unknown field: newField, error found in #10 byte of ...|\"newField\":\"random v|..., bigger context ...|{\"decimalVal\":\"123.1232\",\"intVal\":10,\"newField\":\"random value\"}\n|... ... }
Types ¶
type CustomJsonTagHandler ¶
func NewCustomJsonTagHandler ¶
func NewCustomJsonTagHandler(tag string) *CustomJsonTagHandler