Documentation
¶
Overview ¶
Package utils provides general utils for the project.
Index ¶
- Constants
- func AbsHomePath() string
- func AnonymousUser() int
- func AppFolder() string
- func ArrayStrToInt(strArray []string) ([]int, error)
- func ConfigPath() string
- func Contains(strings []string, element string) bool
- func ContainsInt(ints []int, i int) bool
- func ConvertToMap(config interface{}) map[string]interface{}
- func CurrentUserID(ctx context.Context) int
- func DMPath() string
- func FileExists(path string) bool
- func GenerateGUID() string
- func GenerateUID() string
- func GetAll(config string) map[string]interface{}
- func GetConfig(section string, identifier string, config ...string) string
- func GetConfigArr(section string, identifier string, config ...string) []string
- func GetConfigSection(section string, config ...string) map[string]string
- func GetConfigSectionAll(section string, config string) interface{}
- func GetConfigSectionI(section string, config ...string) map[string]interface{}
- func GetIP(r *http.Request) string
- func GetStrVar(str string) []string
- func HashPassword(password string) (string, error)
- func HomePath() string
- func IfElse(cond bool, trueV interface{}, falseV interface{}) interface{}
- func InterfaceToStringArray(input []interface{}) []string
- func IsAnonymousUser(ctx context.Context) bool
- func Iterate(strings []string, f func(s string) string) []string
- 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 RandomStr(n int) []byte
- func ReplaceStrVar(str string, values map[string]string) string
- func ResizeImage(from string, to string, size string) error
- func RunningMode() string
- func SendMail(to []string, subject, body 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
- func VarFolder() string
Examples ¶
Constants ¶
const CtxKeyUserID = key(1)
CtxKeyUserID defines context key for user id
Variables ¶
This section is empty.
Functions ¶
func AbsHomePath ¶
func AbsHomePath() string
func AnonymousUser ¶
func AnonymousUser() int
func ArrayStrToInt ¶
Convert a string array to int array
func ConfigPath ¶
func ConfigPath() string
func ContainsInt ¶
func ConvertToMap ¶
func ConvertToMap(config interface{}) map[string]interface{}
convert viper map interface{} configuration into array map.
func CurrentUserID ¶
func DMPath ¶
func DMPath() string
DMPath returns folder path of the framework. It can be used to load system file(eg. internal setting file)
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 GetConfigArr ¶
Get config string array
func GetConfigSection ¶
Get sections with string values
func GetConfigSectionAll ¶
func GetConfigSectionI ¶
Get section of the config, config: config file, eg. content(will look for content.yaml or content.json with overriding)
func GetStrVar ¶
Get variable from defined brakets. eg "{name} is {realname}" will get ["name", "realname"]
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 Iterate ¶
Iterate string slice so it will be easy to do operation inside. eg. make ["1","2"] to be ["a-1", "a-2"]
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 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 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
Types ¶
This section is empty.