utils

package
v0.0.0-...-d4279f8 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2017 License: MIT Imports: 16 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FormatTime = func(date time.Time, format string, context *TM_EC.Context) string {
	return date.Format(format)
}

'FormatTime' format time to string Overwrite the default logic with

utils.FormatTime = func(date time.Time, format string, context *TM_EC.Context) string {
	//	.....
}
View Source
var GetLocale = func(context *TM_EC.Context) string {
	if locale := context.Request.Header.Get("Locale"); locale != "" {
		return locale
	}

	if locale := context.Request.URL.Query().Get("locale"); locale != "" {
		if context.Writer != nil {
			context.Request.Header.Set("Locale", locale)
			SetCookie(http.Cookie{Name: "locale", Value: locale, Expires: time.Now().AddDate(1, 0, 0)}, context)
		}

		return locale
	}

	if locale, err := context.Request.Cookie("locale"); err == nil {
		return locale.Value
	}

	return ""
}

'GetLocale' get locale from reqeust, cookie, after get the locale, will write the locale to the cookie if possible Overwrite the default logic with

utils.GetLocal = func(context *TM_EC.Context) string {
	//	.....
}
View Source
var HTMLSanitizer = bluemonday.UGCPolicy()
View Source
var ParseTime = func(timeStr string, context *TM_EC.Context) (time.Time, error) {
	return now.Parse(timeStr)
}

'ParseTime' parse time from string Overwrite the default logic with

utils.ParseTime = func(timeStr string, context *TM_EC.Context) (time.Time, error) {
	//	.....
}

Functions

func ExitWithMsg

func ExitWithMsg(msg interface{}, value ...interface{})

'ExistWithMsg' debug error messages and print stack

func FileServer

func FileServer(dir http.Dir) http.Handler

'FileServer' file server that disabled file listing

func HumanizeString

func HumanizeString(str string) string

'HumanizeString' humanize separates string based on capitalize letters e.g. "OrderItem" -> "Order Item"

func JoinURL

func JoinURL(originalURL string, paths ...interface{}) (joinedURL string, err error)

'JoinURL' updates the path part of the request url e.g. JoinURL("google.com", "admin") => "google.com/admin" e.g. JoinURL("google.com?q=keyword", "admin") => "google.com/admin?q=keyword"

func ModelType

func ModelType(value interface{}) reflect.Type

'ModelType' get value's model type

func NewValue

func NewValue(t reflect.Type) (v reflect.Value)

'NewValue' new struct value with reflect type

func ParseTagOption

func ParseTagOption(str string) map[string]string

'ParseTagOption' parse tag options to hash

func PatchURL

func PatchURL(originalURL string, params ...interface{}) (patchedURL string, err error)

'PatchURL' updates thr query part of the request url. e.g. PatchURL("google.com","key","value") -> "google.com?key=value"

func SetCookie

func SetCookie(cookie http.Cookie, context *TM_EC.Context)

'SetCookie' set cookie for context

func Stringify

func Stringify(object interface{}) string

'Stringify' stringify any data, if it is a struct, will try to use its Name, title, Code field, else will use its primary key

func ToArray

func ToArray(value interface{}) (values []string)

'ToArray' get a array from value, will ignore blank string to convert is to array

func ToFloat

func ToFloat(value interface{}) float64

'ToFloat' get float from value, if passed value is empty string ,result will be 0

func ToInt

func ToInt(value interface{}) int64

'ToInt' get int from value, if passed value is empty string ,result will be 0

func ToParamString

func ToParamString(str string) string

'ToParamString' replaces spaces and sparaters words (by uppercase letters) with underscores in a string, also downcase it e.g. "ToParamString" -> "to_param_string", "To ParamString" -> "to_param_string"

func ToString

func ToString(value interface{}) string

'ToString' get string from value, if passed value is a slice, will use the first element

func ToUInt

func ToUInt(value interface{}) uint64

'ToUInt' get uint from value, if passed value is empty string ,result will be 0

Types

This section is empty.

Jump to

Keyboard shortcuts

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