util

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2021 License: MIT Imports: 30 Imported by: 46

Documentation

Overview

Package util provides a variety of handy functions while developing backends

Index

Constants

View Source
const (
	SymbolComma           = ","
	SymbolSingleQuotation = "'"
)
View Source
const (
	MaxLimit = 300
)

Variables

This section is empty.

Functions

func ADtoROC

func ADtoROC(adStr, format string) (TW_Date string, err error)

西元轉中華民國

func CheckParam

func CheckParam(param map[string]interface{}, target map[string][]string) (bool, map[string]string)

CheckParam can help to verify if the input is valid or not.

Input:

s      : input map for validation.
target : A map indicates that what validation should be applied to the input.

Output:

bool   : Return true if valid, otherwise a false is returned.
error  : the output of the detail of the error.

func CheckRequiredAndParam

func CheckRequiredAndParam(param map[string]interface{}, requireFields []string, target map[string][]string) (bool, map[string]string)

func CheckStruct

func CheckStruct(s interface{}) (bool, error)

CheckStruct can help to verify the content of the struct with tags. It accepts recursive structs.

Remember to Add valid:"[tags]" to the struct.

func CheckTimeFormat

func CheckTimeFormat(tim string) error

CheckTimeFormat is use to check if the string is correct time format or not

func CreatePrivateKeyPem

func CreatePrivateKeyPem(filePath string, priv *rsa.PrivateKey) error

func DecodeMap

func DecodeMap(ecodeMapStr string) (*map[string]interface{}, error)

func DecodePublicKey

func DecodePublicKey(base64Key string) (*rsa.PublicKey, error)

func DecodeString

func DecodeString(encodeStr string, privateKey *rsa.PrivateKey) (*bytes.Buffer, error)

func DecodeToken

func DecodeToken(token string) map[string]interface{}

func DecodeTokenByKey

func DecodeTokenByKey(req *http.Request, key string) map[string]interface{}

func EncodeMap

func EncodeMap(dataMap *map[string]interface{}) (string, error)

func EncodePublicKey

func EncodePublicKey(publicKey crypto.PublicKey) (*bytes.Buffer, error)

func EncodePublicKeyPem

func EncodePublicKeyPem(publicKey crypto.PublicKey) (*bytes.Buffer, error)

func EncodeString

func EncodeString(str string, publicKey *rsa.PublicKey) (*bytes.Buffer, error)

func FileExists

func FileExists(filename string) bool

func GetClientInfo

func GetClientInfo(req *http.Request) map[string]interface{}

func GetClientKey

func GetClientKey(req *http.Request) string

func GetCtxVal

func GetCtxVal(req *http.Request, ck CtxKey) interface{}

func GetFirstDayOfMonth

func GetFirstDayOfMonth() time.Time

func GetFullUrlStr

func GetFullUrlStr(req *http.Request) string

func GetMutiFormPostValue

func GetMutiFormPostValue(req *http.Request, fileKeys []string, valueKeys []string) (map[string]RequestFile, map[string]interface{}, error)

func GetPathVars

func GetPathVars(req *http.Request, keys []string) map[string]interface{}

func GetPostValue

func GetPostValue(req *http.Request, defaultEmpty bool, keys []string) (map[string]interface{}, error)

func GetQueryValue

func GetQueryValue(req *http.Request, keys []string, defaultEmpty bool) map[string]interface{}

func InitValidator

func InitValidator()

InitValidator is used to initialize the validator. Call this funtion before using any validator.

func IntToFixStrLen

func IntToFixStrLen(val int, length int) (string, error)

func IsAlpha

func IsAlpha(str string) bool

func IsBool

func IsBool(str string) (bool, error)

IsBool transform strings of true and false into bool.

func IsFloat64

func IsFloat64(param string, bound []float64) (float64, error)

IsFloat64 can help to verify if the input is Float64 and the value is within the range or not.

If the bound array is not 2, it will skip the ranging test

func IsHomeNum

func IsHomeNum(number string) bool

func IsIdNumber

func IsIdNumber(id string) bool

是否為身份證字號

func IsInt

func IsInt(param string, bound []int) (int, error)

IsInt can help to verify if the input is Int and the value is within the range or not.

If the bound array is not 2, it will skip the ranging test

func IsLogin

func IsLogin(req *http.Request) bool

func IsMail

func IsMail(param string) (bool, error)

isMail can help to verify if the input is Email format or not

func IsMobileNum

func IsMobileNum(number string) bool

func IsStrInList

func IsStrInList(input string, target ...string) bool

IsStrInList can help to see if input is one of the target or not. a non-empty error will be returned if input does match any of the target.

func IsVATnumber

func IsVATnumber(num string) bool

驗證統一編號

func IsValidPwd

func IsValidPwd(str string) (bool, error)

func JoinStrWithQuotation

func JoinStrWithQuotation(separateSymbol string, quotation string, strs ...string) string

func MD5

func MD5(str string) string

func MD5Byte

func MD5Byte(data []byte) string

func ReadPrivateKeyPem

func ReadPrivateKeyPem(filePath string) (*rsa.PrivateKey, error)

func RemoveScriptTag

func RemoveScriptTag(htmlStr string) (string, error)

func ReturnExist

func ReturnExist(ori interface{}, new interface{}) interface{}

ReturnExist will return new if new is not empty else return original

func SHA1

func SHA1(str string) string

func SetCtxKeyVal

func SetCtxKeyVal(r *http.Request, ck CtxKey, val interface{}) *http.Request

func StrAppend

func StrAppend(strs ...string) string

func ToStrAry

func ToStrAry(input interface{}) []string

Types

type CtxKey

type CtxKey string

type Pagination

type Pagination interface {
	Output(w io.Writer) error
}

func NewPagination

func NewPagination(
	source PaginationSource,
	limit, page int64,
	format func(i interface{}) map[string]interface{},
) (Pagination, error)

type PaginationSource

type PaginationSource interface {
	Count() (int64, error)
	Data(limit, p int64, format func(i interface{}) map[string]interface{}) ([]map[string]interface{}, error)
}

type RequestFile

type RequestFile struct {
	ReqFile   multipart.File
	ReqHeader *multipart.FileHeader
}

Notes

Bugs

  • haha

Jump to

Keyboard shortcuts

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