utils

package
v0.0.0-...-2a3cdc3 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2020 License: MIT, MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AppRoot, _ = os.Getwd()

AppRoot app root path

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

FormatTime format time to string Overwrite the default logic with

utils.FormatTime = func(time time.Time, format string, context *qor.Context) string {
    // ....
}
View Source
var GetDBFromRequest = func(req *http.Request) *gorm.DB {
	db := req.Context().Value(ContextDBName)
	if tx, ok := db.(*gorm.DB); ok {
		return tx
	}

	return nil
}

GetDBFromRequest get database from request

View Source
var GetLocale = func(context *qor.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 request, cookie, after get the locale, will write the locale to the cookie if possible Overwrite the default logic with

utils.GetLocale = func(context *qor.Context) string {
    // ....
}
View Source
var HTMLSanitizer = bluemonday.UGCPolicy()

HTMLSanitizer html sanitizer to avoid XSS

View Source
var ParseTime = func(timeStr string, context *qor.Context) (time.Time, error) {
	return now.Parse(timeStr)
}

ParseTime parse time from string Overwrite the default logic with

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

Functions

func ExitWithMsg

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

ExitWithMsg debug error messages and print stack

func FileServer

func FileServer(dir http.Dir) http.Handler

FileServer file server that disabled file listing

func GOPATH

func GOPATH() []string

GOPATH return GOPATH from env

func HumanizeString

func HumanizeString(str string) string

HumanizeString Humanize separates string based on capitalizd letters e.g. "OrderItem" -> "Order Item"

func Indirect

func Indirect(v reflect.Value) reflect.Value

Indirect returns last value that v points to

func JoinURL

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

JoinURL updates the path part of the request url.

JoinURL("google.com", "admin") => "google.com/admin"
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 ParamsMatch

func ParamsMatch(source string, pth string) (url.Values, string, bool)

ParamsMatch match string by param

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 the query part of the request url.

PatchURL("google.com","key","value") => "google.com?key=value"

func SafeJoin

func SafeJoin(paths ...string) (string, error)

SafeJoin safe join https://snyk.io/research/zip-slip-vulnerability#go

func SetCookie

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

SetCookie set cookie for context

func SliceUniq

func SliceUniq(s []string) []string

SliceUniq removes duplicate values in given slice

func SortFormKeys

func SortFormKeys(strs []string)

SortFormKeys sort form keys

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 array from value, will ignore blank string to convert it 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 separates 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

type ClosingReadSeeker

type ClosingReadSeeker struct {
	io.ReadSeeker
}

ClosingReadSeeker implement Closer interface for ReadSeeker

func (ClosingReadSeeker) Close

func (ClosingReadSeeker) Close() error

Close implement Closer interface for Buffer

type ContextKey

type ContextKey string

ContextKey defined type used for context's key

var ContextDBName ContextKey = "ContextDB"

ContextDBName db name used for context

Jump to

Keyboard shortcuts

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