utils

package
v0.0.0-...-5127ed8 Latest Latest
Warning

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

Go to latest
Published: May 30, 2022 License: MIT Imports: 25 Imported by: 30

Documentation

Index

Constants

This section is empty.

Variables

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

AppRoot app root path

View Source
var DefaultLocale string

DefaultLang Default App Language

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

FormatTime format time to string Overwrite the default logic with

utils.FormatTime = func(time time.Time, layout string, context *qor.Context) string {
    // ....
}
View Source
var GetLocale = func(context *core.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 DefaultLocale
}

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 *core.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 FileServer

func FileServer(dir http.Dir) http.Handler

FileServer file server that disabled file listing

func HtmlifyContext

func HtmlifyContext(value interface{}, ctx *core.Context) template.HTML

func HumanizeString

func HumanizeString(str string) string

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

func HumanizeStringU

func HumanizeStringU(str string) string

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

func IndirectItemType

func IndirectItemType(v interface{}) (typ reflect.Type)

func IndirectType

func IndirectType(v interface{}) (typ reflect.Type)

func ModelType

func ModelType(value interface{}) reflect.Type

Type get value's model type

func NamifyString

func NamifyString(s string) string

HumanizeString Humanize separates string based on capitalizd letters e.g. "order_item-data" -> "OrderItemData"

func NewValue

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

NewValue new struct value with reflect type

func NoEmptyStrings

func NoEmptyStrings(values ...string) (res []string)

func ParseFormKey

func ParseFormKey(key string) (result []interface{}, err error)

func ParseTagOption

func ParseTagOption(str string) map[string]string

ParseTagOption parse tag options to hash

func RenderHtmlTemplate

func RenderHtmlTemplate(tpl string, data interface{}) template.HTML

func SetCookie

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

SetCookie set cookie for context

func SetNonZero

func SetNonZero(rvalue reflect.Value, value interface{})

func SetZero

func SetZero(rvalue reflect.Value)

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 StringifyContext

func StringifyContext(object interface{}, ctx *core.Context) string

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

func StringsFilter

func StringsFilter(accept func(s string) bool, dst *[]string, src []string)

func StringsJoin

func StringsJoin(sep, lastSep string, values ...string) string

func StructType

func StructType(v interface{}) (typ reflect.Type)

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) (r 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 last element

func ToUint

func ToUint(value interface{}) uint64

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

func ToUri

func ToUri(value string) string

func Tuples

func Tuples(args ...string) (r [][]string)

func TuplesIndex

func TuplesIndex(args ...string) (r [][]string)

func TypeId

func TypeId(tp interface{}) string

Types

type ContextKey

type ContextKey string

ContextKey defined type used for context's key

type FormTree

type FormTree struct {
	Map      FormTreeMap           `json:",omitempty"`
	Keys     []string              `json:",omitempty"`
	SliceMap map[int]*FormTreeItem `json:"-"`
	Slice    FormTreeSlice         `json:",omitempty"`
}

func NewFormTree

func NewFormTree(form url.Values) (*FormTree, error)

func NewMultipartFormTree

func NewMultipartFormTree(mform *multipart.Form) (_ *FormTree, err error)

func (*FormTree) AddFile

func (this *FormTree) AddFile(key []interface{}, files []*multipart.FileHeader)

func (*FormTree) AddValue

func (this *FormTree) AddValue(key []interface{}, values []string)

func (*FormTree) Each

func (this *FormTree) Each(slicePriority bool, cb func(data interface{}) error) error

func (*FormTree) Sort

func (this *FormTree) Sort()

type FormTreeItem

type FormTreeItem struct {
	Tree   *FormTree               `json:",omitempty"`
	Values []string                `json:",omitempty"`
	Files  []*multipart.FileHeader `json:",omitempty"`
	Index  int                     `json:",omitempty"`
}

type FormTreeItemSlice

type FormTreeItemSlice struct {
	Items []*FormTreeItem
}

type FormTreeMap

type FormTreeMap map[string]*FormTreeItem

type FormTreeSlice

type FormTreeSlice []*FormTreeItem

type PathValue

type PathValue struct {
	Index int
	Value string
}

type PathValues

type PathValues struct {
	Keys   []string
	Values []*PathValue
	Map    map[string][]*PathValue
	Size   int
}

func ParamsMatch

func ParamsMatch(source string, pth string) (*PathValues, string, bool)

ParamsMatch match string by param

func (*PathValues) Add

func (p *PathValues) Add(key, value string)

func (*PathValues) Dict

func (p *PathValues) Dict() map[string][]string

func (*PathValues) Get

func (p *PathValues) Get(key string) *PathValue

func (*PathValues) GetString

func (p *PathValues) GetString(key string) (value string, ok bool)

type ReadonlyMapString

type ReadonlyMapString interface {
	Get(key string) string
}

type TemplateFuncsData

type TemplateFuncsData struct {
	// contains filtered or unexported fields
}

func NewTemplateFuncsData

func NewTemplateFuncsData(funcs map[string]interface{}, data interface{}) *TemplateFuncsData

func (*TemplateFuncsData) Data

func (tf *TemplateFuncsData) Data() interface{}

func (*TemplateFuncsData) Funcs

func (tf *TemplateFuncsData) Funcs() map[string]interface{}

type URLGenerator

type URLGenerator interface {
	JoinStaticURL(path ...string) string
	JoinPath(path ...string) string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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