qor: github.com/qor/qor/utils Index | Files

package utils

import "github.com/qor/qor/utils"


Package Files

buffer.go meta.go params.go utils.go


var AppRoot, _ = os.Getwd()

AppRoot app root path

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 {
    // ....
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

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 {
    // ....
var HTMLSanitizer = bluemonday.UGCPolicy()

HTMLSanitizer html sanitizer to avoid XSS

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) {
    // ....

func ExitWithMsg Uses

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

ExitWithMsg debug error messages and print stack

func FileServer Uses

func FileServer(dir http.Dir) http.Handler

FileServer file server that disabled file listing

func GOPATH Uses

func GOPATH() []string

GOPATH return GOPATH from env

func GetAbsURL Uses

func GetAbsURL(req *http.Request) url.URL

GetAbsURL get absolute URL from request, refer: https://stackoverflow.com/questions/6899069/why-are-request-url-host-and-scheme-blank-in-the-development-server

func HumanizeString Uses

func HumanizeString(str string) string

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

func Indirect Uses

func Indirect(v reflect.Value) reflect.Value

Indirect returns last value that v points to

func JoinURL Uses

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 Uses

func ModelType(value interface{}) reflect.Type

ModelType get value's model type

func NewValue Uses

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

NewValue new struct value with reflect type

func ParamsMatch Uses

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

ParamsMatch match string by param

func ParseTagOption Uses

func ParseTagOption(str string) map[string]string

ParseTagOption parse tag options to hash

func PatchURL Uses

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 Uses

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

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

func SetCookie Uses

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

SetCookie set cookie for context

func SliceUniq Uses

func SliceUniq(s []string) []string

SliceUniq removes duplicate values in given slice

func SortFormKeys Uses

func SortFormKeys(strs []string)

SortFormKeys sort form keys

func Stringify Uses

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 Uses

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

ToArray get array from value, will ignore blank string to convert it to array

func ToFloat Uses

func ToFloat(value interface{}) float64

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

func ToInt Uses

func ToInt(value interface{}) int64

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

func ToParamString Uses

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 Uses

func ToString(value interface{}) string

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

func ToUint Uses

func ToUint(value interface{}) uint64

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

type ClosingReadSeeker Uses

type ClosingReadSeeker struct {

ClosingReadSeeker implement Closer interface for ReadSeeker

func (ClosingReadSeeker) Close Uses

func (ClosingReadSeeker) Close() error

Close implement Closer interface for Buffer

type ContextKey Uses

type ContextKey string

ContextKey defined type used for context's key

var ContextDBName ContextKey = "ContextDB"

ContextDBName db name used for context

Package utils imports 22 packages (graph) and is imported by 199 packages. Updated 2020-02-24. Refresh now. Tools for package owners.