common

package
v0.2.27 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2021 License: Apache-2.0 Imports: 12 Imported by: 15

Documentation

Index

Constants

View Source
const (
	DefaultAttempts int64 = 10
	DefaultDelay          = 500 * time.Millisecond
	DefaultTimeout        = 10 * time.Second
)

Variables

This section is empty.

Functions

func BytesToString added in v0.1.7

func BytesToString(b []byte) string

BytesToString converts byte slice type to string

func CombineMessageWithError added in v0.2.20

func CombineMessageWithError(message string, err error) string

CombineMessageWithError returns a new string which combines given message and error

func ConvertInterfaceToMapInterfaceInterface

func ConvertInterfaceToMapInterfaceInterface(in interface{}) (map[interface{}]interface{}, error)

ConvertInterfaceToMapInterfaceInterface converts input data which must be map type to map interface interface, it means each pair of key and value in the map will be interface type

func ConvertInterfaceToSliceInterface

func ConvertInterfaceToSliceInterface(in interface{}) ([]interface{}, error)

ConvertInterfaceToSliceInterface converts input data which must be slice type to interface slice, it means each element in the slice is interface type.

func ConvertNumberToString added in v0.1.7

func ConvertNumberToString(in interface{}) (string, error)

ConvertNumberToString tries to convert number to string, if input is neither number type nor string, it will return error

func ConvertToBool added in v0.2.23

func ConvertToBool(in interface{}) (bool, error)

func ConvertToFloat added in v0.2.23

func ConvertToFloat(in interface{}) (float64, error)

func ConvertToInt added in v0.2.23

func ConvertToInt(in interface{}) (int64, error)

func ConvertToSlice added in v0.2.23

func ConvertToSlice(in interface{}, kind reflect.Kind) (interface{}, error)

func ConvertToString added in v0.2.23

func ConvertToString(in interface{}) (string, error)

func ConvertToUint added in v0.2.23

func ConvertToUint(in interface{}) (uint64, error)

func CopyStructWithFields added in v0.2.3

func CopyStructWithFields(in interface{}, fields ...string) (interface{}, error)

CopyStructWithFields returns a new struct with only specified fields NOTE:

  1. tags and values of fields are exactly same
  2. only exported and addressable fields will be copied
  3. if any field in fields does not exist in the input struct, it returns error
  4. if values in input struct is a pointer, then value in the new struct will point to the same object
  5. returning struct is totally a new data type, so you could not use any (*type) assertion
  6. if fields argument is empty, a new struct which contains the whole fields of input struct will be returned
  7. technically, for convenience purpose, this function creates a new struct as same as input struct, then removes fields that does not exist in the given fields

func CopyStructWithoutFields added in v0.2.3

func CopyStructWithoutFields(in interface{}, fields ...string) (interface{}, error)

CopyStructWithoutFields returns a new struct without specified fields, there are something you should know. NOTE: 1. tags and values of remaining fields are exactly same 2. only exported and addressable fields will be copied 3. if any field in fields does not exist in the input struct, it simply ignores 4. if values in input struct is a pointer, then value in the new struct will point to the same object 5. returning struct is totally a new data type, so you could not use any (*type) assertion 6. if fields argument is empty, a new struct which contains the whole fields of input struct will be returned

func ElementInSlice

func ElementInSlice(e interface{}, s interface{}) (bool, error)

ElementInSlice checks if given element is in the slice

func GetValueOfStruct added in v0.2.10

func GetValueOfStruct(in interface{}, field string) (interface{}, error)

GetValueOfStruct get value of specified field of input struct, the fields must exist and be exported, otherwise, it will return an error, the first argument must be a pointer to struct

func KeyInMap

func KeyInMap(k interface{}, m interface{}) (bool, error)

KeyInMap checks if given key is in the map

func MarshalStructWithFields added in v0.2.3

func MarshalStructWithFields(in interface{}, fields ...string) ([]byte, error)

MarshalStructWithFields marshals input struct using json.Marshal() with given fields, first argument must be a pointer to struct, not the struct itself

func MarshalStructWithTag added in v0.2.15

func MarshalStructWithTag(in interface{}, tag string) ([]byte, error)

MarshalStructWithTag marshals input struct using json.Marshal() with fields that contain given tag, first argument must be a pointer to struct, not the struct itself

func MarshalStructWithoutFields added in v0.2.3

func MarshalStructWithoutFields(in interface{}, fields ...string) ([]byte, error)

MarshalStructWithFields marshals input struct using json.Marshal() without given fields, first argument must be a pointer to struct, not the struct itself

func NewMapWithStructTag added in v0.2.17

func NewMapWithStructTag(m map[string]interface{}, in interface{}, tag string) (map[string]interface{}, error)

NewMapWithStructTag returns a new map as the result, it loops the keys of given map and tags of the struct, if key and tag are same, the field of the the input struct will be the key of the new map, the value of the given map will be the value of the new map, if any key in the given map could not match any tag in the struct, it will return error, so make sure that each key the given map could match a field tag in the struct

func Retry added in v0.1.4

func Retry(doFunc func() error, attempts int64, delay, timeout time.Duration) error

RetryWithRetryOption retries the func until it returns no error or reaches attempts limit or timed out, either one is earlier

func RetryWithRetryOption added in v0.1.4

func RetryWithRetryOption(doFunc func() error, opts ...RetryOption) (err error)

RetryWithRetryOption retries the func until it returns no error or reaches attempts limit or timed out, either one is earlier

func SetRandomValueToNil added in v0.2.25

func SetRandomValueToNil(values ...interface{}) error

SetRandomValueToNil set each value in slice values if value is a random value

func SetValueOfStruct added in v0.2.3

func SetValueOfStruct(in interface{}, field string, value interface{}) error

SetValueOfStruct sets value of specified field of input struct, the fields must exist and be exported, otherwise, it will return an error, the first argument must be a pointer to struct

func SetValueOfStructByKind added in v0.2.23

func SetValueOfStructByKind(in interface{}, field string, value interface{}, kind reflect.Kind) error

func SetValuesWithMap added in v0.2.26

func SetValuesWithMap(in interface{}, fields map[string]interface{}) error

SetValuesWithMap sets values of input struct with given map, the fields of map must exist and be exported, otherwise, it will return an error, the first argument must be a pointer to struct

func SetValuesWithMapAndRandom added in v0.2.27

func SetValuesWithMapAndRandom(in interface{}, fields map[string]interface{}) error

SetValuesWithMapAndRandom sets values of input struct with given map, if fields in struct does not exist in given map, some of them--depends on the data type--will be set with default value, the fields of map must exist and be exported, otherwise, it will return an error, the first argument must be a pointer to struct

func StringToBytes added in v0.1.7

func StringToBytes(s string) []byte

StringToBytes converts string type to byte slice

func TrimSpaceOfStructString

func TrimSpaceOfStructString(in interface{}) error

TrimSpaceOfStructString trims spaces of each member variable of the struct

func UnmarshalToMapWithStructTag added in v0.2.18

func UnmarshalToMapWithStructTag(data []byte, in interface{}, tag string) (map[string]interface{}, error)

UnmarshalToMapWithStructTag returns a map as the result, it works as following logic:

  1. unmarshals given data to to a temporary map to get keys
  2. unmarshals given data to the input struct, to get field names and values with appropriate data types
  3. loops keys in the temporary map, loops tags of the input struct in a nested
  4. if the key matches the tag, set field name as the key of result map, set field value as the value of the result map
  5. if any key in the temporary map can not match any field tag of the struct, it returns error, so make sure that each key of the given data could match a field tag in the struct

func UnmarshalToMapWithStructTagFromString added in v0.2.18

func UnmarshalToMapWithStructTagFromString(s string, in interface{}, tag string) (map[string]interface{}, error)

UnmarshalToMapWithStructTag converts given string to []byte and then call UnmarshalToMapWithStructTag() function

func ValueInMap

func ValueInMap(v interface{}, m interface{}) (bool, error)

ValueInMap checks if given value is in the map

Types

type RetryOption added in v0.1.4

type RetryOption struct {
	Attempts int64
	Delay    time.Duration
	Timeout  time.Duration
}

RetryOption is options for Retry()

func NewRetryOption added in v0.1.4

func NewRetryOption(attempts int64, delay, timeout time.Duration) RetryOption

NewRetryOption returns RetryOption

Jump to

Keyboard shortcuts

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