Documentation ¶
Overview ¶
Package utils provides general utils for the project.
Index ¶
- Constants
- func AnonymousUser() int
- func ArrayStrToInt(strArray []string) ([]int, error)
- func ClearDebugToken()
- func Contains(strings []string, element string) bool
- func ContainsInt(ints []int, i int) bool
- func CurrentUserID(ctx context.Context) int
- func FileExists(path string) bool
- func GenerateGUID() string
- func GenerateUID() string
- func GetDebugToken() string
- func GetIP(r *http.Request) string
- func GetStrVar(str string) []string
- func HandleSendMail(sendMail func(mail MailMessage) error)
- func HashPassword(password string) (string, error)
- func IfElse(cond bool, trueV interface{}, falseV interface{}) interface{}
- func InterfaceToStringArray(input []interface{}) []string
- func IsAnonymousUser(ctx context.Context) bool
- func IsIdentifier(input string) bool
- func Iterate(strings []string, f func(s string) string) []string
- func ListContains(list []map[string]string, key string, value string) bool
- func MatchCondition(conditions map[string]interface{}, target map[string]interface{}) (bool, []string)
- func MatchPassword(password string, hash string) bool
- func NameToIdentifier(input string) string
- func NewDebugToken(duration time.Duration) string
- func RandomStr(n int) []byte
- func ReplaceStrVar(str string, values map[string]string) string
- func ResizeImage(from string, to string, size string) error
- func SecurePath(path string) string
- func SendFullMail(mailMessage MailMessage) error
- func SendMail(to []string, subject, body string, bcc ...string) error
- func Split(str string, seperator ...string) []string
- func StripSQLPhrase(str string) string
- func UnmarshalData(filepath string, v interface{}) error
- func UpperName(input string) string
- type MailMessage
Examples ¶
Constants ¶
const CtxKeyUserID = key(1)
CtxKeyUserID defines context key for user id
Variables ¶
This section is empty.
Functions ¶
func ArrayStrToInt ¶
Convert a string array to int array
func ClearDebugToken ¶
func ClearDebugToken()
func ContainsInt ¶
func CurrentUserID ¶
func FileExists ¶
func GenerateGUID ¶
func GenerateGUID() string
Generate a guid which is completely random without order
func GenerateUID ¶
func GenerateUID() string
Generate unique id with order. It should be cluster safe.
func GetDebugToken ¶
func GetDebugToken() string
func GetStrVar ¶
Get variable from defined brakets. eg "{name} is {realname}" will get ["name", "realname"]
func HandleSendMail ¶
func HandleSendMail(sendMail func(mail MailMessage) error)
func HashPassword ¶
func IfElse ¶
func IfElse(cond bool, trueV interface{}, falseV interface{}) interface{}
IfElse implements ternary. Equvelant to <cond>?<trueV>:<falseV> in other language. Ref: https://github.com/golang/go/issues/20774 The reason was insane.
There might be performance not perfect, since it needs type convertion.
func InterfaceToStringArray ¶
func InterfaceToStringArray(input []interface{}) []string
func IsAnonymousUser ¶
func IsIdentifier ¶
func Iterate ¶
Iterate string slice so it will be easy to do operation inside. eg. make ["1","2"] to be ["a-1", "a-2"]
func ListContains ¶
If key exists in a map list
func MatchCondition ¶
func MatchCondition(conditions map[string]interface{}, target map[string]interface{}) (bool, []string)
Iternate condition rules to see if all are matching. If there are keys in condition rules but not in realValues, match fails. * mean always all eg.1) conditions: {id: 12, type:"image"} or {id:[11,12], type:["image", "article"]} target: {id:12,type:"article"} 2) conditions: {id: [11,12], type:"image" } target: {id:[12, 13], type: ["image", "article"]} 3) conditions: {id:11, type: "*"} target: {id:[11, 12], type:"image"} 4) conditions: {id:11, type: "image"} target: {id:[11, 12], type:nil} //nil will be treated as pass
func MatchPassword ¶
func NameToIdentifier ¶
convert name lie "Hello world.?" to "hello-world"
Example ¶
result := GetStrVar("{this} is {good}") fmt.Println(result[0])
Output: this
func NewDebugToken ¶
Generate a debug token and store it into memory, new one will always override existing one After 'duration' it will be removed.
func ReplaceStrVar ¶
Replace variable with values in string
Example ¶
result := ReplaceStrVar("{this} is {value}", map[string]string{"this": "test", "value": "test1"}) fmt.Println(result)
Output: test is test1
func ResizeImage ¶
Generate a resized image
func SecurePath ¶
WashPath remove .. make sure it can only go under, not upp
func SendFullMail ¶
func SendFullMail(mailMessage MailMessage) error
Send mail with possible attachments. Note: From in most case(depending on handler) will be from config file Security: attachment needs to be checked to make sure it can not send any file
func StripSQLPhrase ¶
Strip unregular string to avoid sql injection. Note this is not used to strip whole sql, but phrase of sql.(eg. ORDER BY ...), not applicable for values.
func UnmarshalData ¶
UnmarshalData Load json and unmall into variable