utils

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2023 License: Apache-2.0 Imports: 21 Imported by: 7

README

go-utils

Go helpers and utilities. See GoDoc for more details.

GitHub tag (latest SemVer) Tests CodeQL Analysis GolangCI Lint Go Report Card Go Reference License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidEmailAddress = errors.New("invalid email address")
	ErrInvalidIcanSuffix   = errors.New("invalid ICAN suffix")
)
View Source
var (
	ErrInvalidReader   = errors.New("invalid reader")
	ErrEmptyInput      = errors.New("empty input")
	ErrInvalidPartSize = errors.New("part size must be greater than 0")
)

Predefined errors

Functions

func AmountToFloat64

func AmountToFloat64(amount uint64, decimals uint8) float64

AmountToFloat64 converts amount lamports to float64 with given decimals.

func AmountToString

func AmountToString(amount uint64, decimals uint8) string

AmountToString converts amount lamports to string with given decimals.

func AmountToUint64

func AmountToUint64(amount float64, decimals uint8) uint64

AmountToUint64 converts amount from float64 to uint64 with given decimals.

func AnyToString

func AnyToString(v interface{}) string

AnyToString converts any type to string

func Base58ToBytes

func Base58ToBytes(s string) ([]byte, error)

Base58ToBytes converts base58 string to bytes.

func Base64ToBytes

func Base64ToBytes(s string) ([]byte, error)

Base64ToBytes converts base64 string to bytes.

func BytesToBase58

func BytesToBase58(b []byte) string

BytesToBase58 converts bytes to base58 string.

func BytesToBase64

func BytesToBase64(b []byte) string

BytesToBase64 converts bytes to base64 string.

func Capitalize

func Capitalize(s string) string

Capitalize capitalizes all words in a string

func DownloadFile

func DownloadFile(url string) ([]byte, error)

DownloadFile downloads the file from the given URL and returns the bytes.

func Float64ToString

func Float64ToString(amount float64) string

Float64ToString converts float64 to string with minimum number of decimals. For example, 1.000000000 will be converted to "1", 1.100000000 will be converted to "1.1".

func FullyQualifiedStructName added in v0.1.4

func FullyQualifiedStructName(v interface{}) string

FullyQualifiedStructName name returns object name in format [package].[type name]. It ignores if the value is a pointer or not.

func GetFileByPath

func GetFileByPath(path string) ([]byte, error)

GetFileByPath returns the file bytes from the given path. If the path is a URL, it will download the file and return the bytes. If the path is a local file, it will read the file and return the bytes.

func GetFileContentType

func GetFileContentType(input io.Reader) (string, error)

GetFileContentType returns the content type of a file.

func GetFileContentTypeByBytes

func GetFileContentTypeByBytes(input []byte) (string, error)

GetFileContentTypeByBytes returns the content type of a file.

func GetFileNameWithoutExtension

func GetFileNameWithoutExtension(fileName string) string

Get the file name without extension. fileName string: the file name.

func GetFileSize

func GetFileSize(file io.ReadSeeker) (int64, error)

Get the file size. file io.ReadSeeker: the file to be uploaded.

func GetFileTypeByURI

func GetFileTypeByURI(uri string) string

GetFileTypeByURI returns the file type of the given URI

func GetMaxFileParts

func GetMaxFileParts(file io.ReadSeeker, partSize int64) (int64, error)

Get max file parts can be if the file is split into parts with the given part size. The max file parts is 10000. file io.ReadSeeker: the file to be uploaded. partSize int64: the part size.

func GetVarType

func GetVarType(v interface{}) string

GetVarType returns the type of the given variable as a string.

func IntAmountToFloat64

func IntAmountToFloat64(amount int64, decimals uint8) float64

IntAmountToFloat64 converts int64 amount lamports to float64 with given decimals.

func IsAudioResponse added in v0.1.3

func IsAudioResponse(r *http.Request) bool

is response accepted as audio?

func IsCssResponse added in v0.1.3

func IsCssResponse(r *http.Request) bool

is response accepted as css?

func IsFormRequest added in v0.1.3

func IsFormRequest(r *http.Request) bool

Is request a form request?

func IsHtmlResponse added in v0.1.3

func IsHtmlResponse(r *http.Request) bool

is response accepted as html?

func IsImageResponse added in v0.1.3

func IsImageResponse(r *http.Request) bool

is response accepted as image?

func IsJavascriptResponse added in v0.1.3

func IsJavascriptResponse(r *http.Request) bool

is response accepted as javascript?

func IsJsonRequest added in v0.1.3

func IsJsonRequest(r *http.Request) bool

Is request a json request?

func IsJsonResponse added in v0.1.3

func IsJsonResponse(r *http.Request) bool

is response accepted as json?

func IsMultipartRequest added in v0.1.3

func IsMultipartRequest(r *http.Request) bool

Is request a multipart request?

func IsTextRequest added in v0.1.3

func IsTextRequest(r *http.Request) bool

Is request a text request?

func IsTextResponse added in v0.1.3

func IsTextResponse(r *http.Request) bool

is response accepted as text?

func IsVideoResponse added in v0.1.3

func IsVideoResponse(r *http.Request) bool

is response accepted as video?

func IsXmlRequest added in v0.1.3

func IsXmlRequest(r *http.Request) bool

Is request a xml request?

func IsXmlResponse added in v0.1.3

func IsXmlResponse(r *http.Request) bool

is response accepted as xml?

func MapToStruct

func MapToStruct(source map[string]interface{}, target interface{}, customTag string) error

MapToStruct maps a map to a struct. The map key must be the same as the struct field name or the custom tag name. The map value must be the same type as the struct field type. The target must be a non-nil pointer to a struct.

func MergeIntoMap added in v0.1.5

func MergeIntoMap(dst, src map[string]interface{}) map[string]interface{}

MergeIntoMap merges `src` map into `dst` map. `dst` - destination map, `src` - source map if the same key exists in both maps, the value from the second map will be used. if the first map is nil, a new map will be created. if the second map is nil, the first map will be returned. if both maps are nil, a new map will be created.

func MergeIntoMapRecursively added in v0.1.5

func MergeIntoMapRecursively(dst, src map[string]interface{}) map[string]interface{}

MergeIntoMapRecursively recursively merges `src` map into `dst` map. `dst` - destination map, `src` - source map if the same key exists in both maps, the value from the second map will be used. if the first map is nil, a new map will be created. if the second map is nil, the first map will be returned. if both maps are nil, a new map will be created. if the value for a given key is a map, it will be merged recursively.

func NamedStruct added in v0.1.4

func NamedStruct(fallback func(v interface{}) string) func(v interface{}) string

NamedStruct returns the name from a message implementing the following interface:

type namedStruct interface {
	Name() string
}

It ignores if the value is a pointer or not.

func NewContextWithCancel added in v0.1.1

func NewContextWithCancel(
	log interface {
		Printf(format string, v ...interface{})
	},
) (context.Context, context.CancelFunc)

NewContextWithCancel returns a new context with a cancel function. The context will be canceled when an interrupt signal is received.

func ParseFileSize

func ParseFileSize(s string) (int64, error)

Parse file size from string to int64

func Pointer

func Pointer[T any](v T) *T

Pointer is a helper to make a pointer to the given value.

func PrettyPrint

func PrettyPrint(v ...interface{})

PrettyPrint prints the given interface in a pretty format

func PrettyString

func PrettyString(v interface{}) string

PrettyString returns the given interface in a pretty format

func SanitizeEmail

func SanitizeEmail(s string) (string, error)

SanitizeEmail cleans email address from dots, dashes, etc

func StructName added in v0.1.4

func StructName(v interface{}) string

StructName name returns struct name in format [type name]. It ignores if the value is a pointer or not.

func StructToMap

func StructToMap(source interface{}, customTag string) (map[string]interface{}, error)

StructToMap converts a struct to a map. The map key will be the struct field name or the custom tag name. The map value will be the struct field value. The source must be a struct or a pointer to a struct.

func ToSnakeCase

func ToSnakeCase(s string) string

ToSnakeCase converts string to snake case.

func ToTitleCase

func ToTitleCase(s string) string

ToTitleCase converts string to title case.

func TrimRightZeros

func TrimRightZeros(str string) string

TrimRightZeros trims trailing zeros from string.

func TrimStringBetween

func TrimStringBetween(str, start, end string) string

Trim string between two substrings and return the string without it and substrings.

func UcFirst

func UcFirst(s string) string

UcFirst capitalizes first letter of a string

Types

This section is empty.

Jump to

Keyboard shortcuts

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