utils

package
v5.6.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package utils implements general purpose utility

Index

Constants

This section is empty.

Variables

View Source
var BodyJson = NewCustomJsonTagHandler("body")
View Source
var HeaderJson = NewCustomJsonTagHandler("header")

Functions

func Decode

func Decode(src io.Reader, dest interface{}) error

func Encode

func Encode(src interface{}, dest io.Writer) error

func GenerateID

func GenerateID(totalLength int, prefix string) string

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

func GenerateRandomString(n int) string

GenerateRandomString generates a random string of n characters, the generated string will of form ^[A-Za-z0-9]{n}$

func GetEnv

func GetEnv(key string, defaultVal string) string

func GetEnvAsSlice

func GetEnvAsSlice(name string, defaultVal []string, sep string) []string

func GetEnvBool

func GetEnvBool(key string, defaultVal bool) bool

func GetEnvInt

func GetEnvInt(key string, defaultVal int) int

func GetEnvMust

func GetEnvMust(key string) string

func GetHash

func GetHash(val string) string

func GetHostName

func GetHostName() string

func IsASCII

func IsASCII(s string) bool

func IsUTF8

func IsUTF8(s string) bool

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

type CustomJsonTagHandler struct {
	jsoniter.API
}

func NewCustomJsonTagHandler

func NewCustomJsonTagHandler(tag string) *CustomJsonTagHandler

type Message

type Message struct {
	Entity   string             `json:"entity"`
	Event    string             `json:"event"`
	Contains []string           `json:"contains"`
	Payload  map[string]Payload `json:"payload"`
}

func NewMessage

func NewMessage(entity string, event string) *Message

func (*Message) AddPayload

func (m *Message) AddPayload(name string, payload Payload) error

func (*Message) GetPayload

func (m *Message) GetPayload(name string) (p Payload, err error)

type Payload

type Payload map[string]interface{}

Directories

Path Synopsis
Package httputil implements utility for httpClient with request retry and optional tracing
Package httputil implements utility for httpClient with request retry and optional tracing

Jump to

Keyboard shortcuts

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