utils

package
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LESS_THAN_ZERO int32 = iota + 1
	EQUAL_ZERO
	EQUAL_ONE
	GREATER_THAN_ZERO
	GREATER_THAN_ONE
	GREATER_THAN_TWO
	GREATER_THAN_THREE
	GREATER_THAN_FOUR
	GREATER_THAN_TEN
	ZERO_TO_TEN
	ZERO_TO_TWENTY
	TEN_TO_TWENTY
	TWENTY_TO_FIFTY
	ZERO_TO_FIFTY
	ZERO_TO_HUNDRED
	GREATER_THAN_FIFTY
	GREATER_THAN_TWENTY
	GREATER_THAN_HUNDRED
)

Variables

This section is empty.

Functions

func AppendFile

func AppendFile(data []byte, filePath string, mode int) error

Append to a file. Create if not exist, or append to existing file.

Mode:
- 0: Before
- 1: After
Example:
- AppendFile([]byte("Hello World"), "temp/output.txt", 2)

func ArrayInt64ToString

func ArrayInt64ToString(a []int64, delim string) string

func ArrayIntToString

func ArrayIntToString(a []int, delim string) string

func ArrayJsonNumberToString

func ArrayJsonNumberToString(a []json.Number, delim string) string

func ArrayStringToString

func ArrayStringToString(a []string, delim string) string

func BitwiseToString

func BitwiseToString(bitwise int64) string

func ClearCurrentFolder

func ClearCurrentFolder(dirPath string) error

Clear folder removes current directory.

func ClearFile

func ClearFile(files ...string) error

Clear file removes the named files.

func CopyFile

func CopyFile(srcPath, dstPath string) error

CopyFile copies a file from src to dst.

func CreateFilePath

func CreateFilePath(filePath string) error

func DecodeModel

func DecodeModel(input map[string]interface{}, result interface{}) error

func Distinct

func Distinct[T string | int32 | int64 | float32 | float64](list []T) []T

Distinct function remove duplicates from slice. Distinct return a new slice.

 Example:
	newList := Distinct(oldList)

func DistinctStr

func DistinctStr(str string, delim string) string

func DoubleSlice

func DoubleSlice(s interface{}) []interface{}

Deal with variadic functions in Go.

func Find

func Find[T any](list []T, f func(T) bool) T

Find function find first element based on conditions. Find return a new value T.

 Example:
	item := Find(list, func(n name) bool {return n == "ABC"})

func FormatUnixToString

func FormatUnixToString(d int64, format DateTime) string

func GetFromInterface

func GetFromInterface(src map[string]interface{}, key string, defaultValue interface{}) interface{}

Get value from interface. Return default value if key not exists or key exists with zero value.

func GetFromInterfaceV2

func GetFromInterfaceV2[T any](src map[string]interface{}, key string, defaultValue T) T

Get value from interface. Return default value if key not exists or key exists with zero value.

func GetFromInterfaceV3

func GetFromInterfaceV3[T any](src map[string]interface{}, key string, defaultValue T, checkZeroValue ...bool) T

Get value from interface. Return default value if key not exists or key exists with zero value (optional).

NOTE: when checkZeroValue is true, it will check if the value is zero value. When checkZeroValue is false, it only check if the key exists.

func GetGormQuery

func GetGormQuery(query *gorm.DB, params map[string]interface{})

func GetHasMore

func GetHasMore(currentPage int, totalCount int, pageSize int) bool

Get has more

func GetScopePagination

func GetScopePagination(value interface{}, pagination *Pagination, query *gorm.DB) func(db *gorm.DB) *gorm.DB

func GetTotalPages

func GetTotalPages(totalCount int, pageSize int) int

Get total pages int

func HttpClient

func HttpClient() *http.Client

func IndexOf

func IndexOf[T any](list []T, f func(T) bool) int

IndexOf returns the index of the first element that satisfies the condition.

 Example:
	index := IndexOf(list, func(n name) bool {return n == "ABC"})

func IsZero

func IsZero(p interface{}) bool

Detect whether a value is the zero value for its type.

func MapQuantityFromTo

func MapQuantityFromTo(condition int64, res map[string]interface{}, key string) map[string]interface{}

func Parse

func Parse[T parsable](in interface{}) (out T, err error)

Number to String OR String to Number

func Pop

func Pop[T any](list []T) (T, []T)

Pop removes the last element. Pop returns new slice and that element.

 Example:
	x, a := Pop(items)

func PrettyPrint

func PrettyPrint(v interface{}) string

Print with formatted

func RemoveAt

func RemoveAt[T any](list []T, i int) []T

RemoveAt removes the element based on index. RemoveAt returns new slice.

 Example:
	a := RemoveAt(items, 1)

func SendRequest

func SendRequest(client *http.Client, headers map[string]string, endpoint, method string, data interface{}) ([]byte, error)

func Shift

func Shift[T any](list []T) (T, []T)

Shift removes the first element. Shift returns new slice and that element.

 Example:
	x, a := Shift(items)

func Slug

func Slug(s string) string

func StringToArrayInt

func StringToArrayInt(str, delim string) []int

func StringToArrayInt64

func StringToArrayInt64(str string, delim string) []int64

func StringToArrayString

func StringToArrayString(str, delim string, trim bool) []string

func StringToBitwiseInt64

func StringToBitwiseInt64(str string) int64

func TimeToString

func TimeToString(t time.Time) string

func ToPointer

func ToPointer[T any](i T) *T

func UnixTime

func UnixTime(d int64) time.Time

func Unshift

func Unshift[T any](list []T, x T) []T

Unshift adds the specified elements to the beginning. Unshift returns new slice.

 Example:
	items := Unshift(items, a)

func ValidateEmail

func ValidateEmail(email string) bool

Validate email address

func ValidateStruct

func ValidateStruct(s interface{}) error

Validate struct fields

func ValidateStructCtx

func ValidateStructCtx(ctx context.Context, s interface{}) error

Validate struct fields

func Where

func Where[T any](list []T, f func(T) bool) []T

Where function filter elements based on conditions. Where return a new slice.

 Example:
	evens := Filter(list, func(i int) bool {return i % 2 == 0})

func WriteFile

func WriteFile(data []byte, filePath string) error

Write to a file. Create if not exist, or overwrite the existing file.

Example:
WriteFile([]byte("Hello World"), "temp/output.txt")

func ZZip

func ZZip(folder string, files ...string) error

func ZipMultipleFile

func ZipMultipleFile(outputFile string, sourceFiles ...string) error

ZipMultipleFile zip list of sourceFile path to outputFile path

func ZipSingleFile

func ZipSingleFile(sourceFile, outputFile string) error

ZipSingleFile zip the sourceFile path to outputFile path

Types

type DateTime

type DateTime string
const (
	RFC822              DateTime = time.RFC822
	Kitchen             DateTime = time.Kitchen
	UnixDate            DateTime = time.UnixDate
	YYYY_MM_DD          DateTime = "2006-01-02"
	DD_MM_YYYY          DateTime = "02-01-2006"
	YYYY_MM_DD_HH_MM_SS DateTime = "2006-01-02 15:04:05"
	DD_MM_YYYY_HH_MM_SS DateTime = "02-01-2006 15:04:05"
	UNIX                         = 100000000
	UNIX_MILLI                   = 1000000000000
)

type Pagination

type Pagination struct {
	Limit    int64       `json:"limit"`
	Page     int64       `json:"page"`
	SortBy   string      `json:"sort_by"`
	SortDesc int64       `json:"sort_desc"`
	Count    int64       `json:"count"`
	Pages    int64       `json:"pages"`
	Records  interface{} `json:"records"`
}

func NewPagination

func NewPagination(limit interface{}, page interface{}, sort_by interface{}, sort_desc interface{}, with_total bool) *Pagination

func (*Pagination) GetLimit

func (p *Pagination) GetLimit() int

func (*Pagination) GetOffset

func (p *Pagination) GetOffset() int

func (*Pagination) GetPage

func (p *Pagination) GetPage() int

func (*Pagination) GetSort

func (p *Pagination) GetSort() string

type PaginationQuery

type PaginationQuery struct {
	Size    int    `json:"size,omitempty"`
	Page    int    `json:"page,omitempty"`
	OrderBy string `json:"orderBy,omitempty"`
}

Pagination query params

func GetPaginationFromCtx

func GetPaginationFromCtx(c gin.Context) (*PaginationQuery, error)

nolint Get pagination query struct from

func (*PaginationQuery) GetLimit

func (q *PaginationQuery) GetLimit() int

Get limit

func (*PaginationQuery) GetOffset

func (q *PaginationQuery) GetOffset() int

Get offset

func (*PaginationQuery) GetOrderBy

func (q *PaginationQuery) GetOrderBy() string

Get OrderBy

func (*PaginationQuery) GetPage

func (q *PaginationQuery) GetPage() int

Get OrderBy

func (*PaginationQuery) GetQueryString

func (q *PaginationQuery) GetQueryString() string

func (*PaginationQuery) GetSize

func (q *PaginationQuery) GetSize() int

Get OrderBy

func (*PaginationQuery) SetOrderBy

func (q *PaginationQuery) SetOrderBy(orderByQuery string)

Set order by

func (*PaginationQuery) SetPage

func (q *PaginationQuery) SetPage(pageQuery string) error

Set page number

func (*PaginationQuery) SetSize

func (q *PaginationQuery) SetSize(sizeQuery string) error

Set page size

Jump to

Keyboard shortcuts

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