viewutil

package
v0.0.0-...-bc49051 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Funcs template.FuncMap = map[string]any{}/* 101 elements not displayed */

)

Funcs is a singleton for viewfuncs.

Functions

func AsBytes

func AsBytes(v interface{}) []byte

AsBytes attempts to return a bytes representation of a value.

func AsDuration

func AsDuration(val any) (typedVal time.Duration, err error)

AsDuration returns a given value as a duration.

func AsFloat64

func AsFloat64(val interface{}) (typedVal float64, err error)

AsFloat64 returns a given value as a float64.

func AsInt

func AsInt(val interface{}) (typedVal int, err error)

AsInt returns a given value as a int64.

func AsString

func AsString(v any) string

AsString attempts to return a string representation of a value.

func AtIndex

func AtIndex(index int, collection interface{}) (interface{}, error)

AtIndex returns an element at a given index.

func Base64

func Base64(v string) string

Base64 encodes data as a string as a base6 string.

func Base64Decode

func Base64Decode(v string) (string, error)

Base64Decode decodes a base 64 string.

func CSV

func CSV(collection interface{}) (string, error)

CSV returns a csv of a given collection.

func Ceil

func Ceil(d float64) float64

Ceil returns the value rounded up to the nearest integer.

func Concat

func Concat(strs ...string) string

Concat concatenates a list of strings.

func Contains

func Contains(substr, v string) bool

Contains returns if a string contains a given substring.

func ControlFor

func ControlFor(obj any, fieldName string) template.HTML

ControlFor returns just the input for a given struct field by name.

func DurationRound

func DurationRound(d time.Duration, to time.Duration) time.Duration

DurationRound rounds a duration value.

func DurationRoundMillis

func DurationRoundMillis(d time.Duration) time.Duration

DurationRoundMillis rounds a duration value to milliseconds.

func DurationRoundSeconds

func DurationRoundSeconds(d time.Duration) time.Duration

DurationRoundSeconds rounds a duration value to seconds.

func FileExists

func FileExists(path string) bool

FileExists returns if the file at a given path exists.

func First

func First(collection interface{}) (interface{}, error)

First returns the first element of a collection.

func Floor

func Floor(d float64) float64

Floor returns the value rounded down to zero.

func FormFor

func FormFor(obj any, action string) template.HTML

FormFor yields an html form input for a given value.

The value is reflected and the `form:"..."` struct tags are used to set options for individual fields.

func FormatFileSize

func FormatFileSize(sizeBytes int) string

FormatFileSize formats an int as a file size.

func FormatMoney

func FormatMoney(d float64) string

FormatMoney returns a float as a formatted string rounded to two decimal places.

func FormatPct

func FormatPct(d float64) string

FormatPct formats a float as a percentage (it is multiplied by 100, then suffixed with '%')

func GenerateKey

func GenerateKey(keySize int) string

GenerateKey generates a key of a given size base 64 encoded.

func GenerateOrdinalNames

func GenerateOrdinalNames(format string, replicas int) []string

GenerateOrdinalNames generates ordinal names by passing the index to a given formatter. The formatter should be in Sprintf format (i.e. using a '%d' token for where the index should go).

Example:

{{ generate_ordinal_names "worker-%d" 3 }} // [worker-0 worker-1 worker-2]

func HMAC512

func HMAC512(key, v string) (string, error)

HMAC512 returns the hmac signed sha 512 sum of a string.

func HasPrefix

func HasPrefix(prefix, v string) bool

HasPrefix returns if a string has a given prefix.

func HasSuffix

func HasSuffix(suffix, v string) bool

HasSuffix returns if a string has a given suffix.

func IndentSpaces

func IndentSpaces(spaceCount int, v interface{}) string

IndentSpaces indents a string by a given set of spaces.

func IndentTabs

func IndentTabs(tabCount int, v interface{}) string

IndentTabs indents a string with a given number of tabs.

func Join

func Join(sep string, collection any) (string, error)

Join creates a string joined with a given separator.

func Last

func Last(collection interface{}) (interface{}, error)

Last returns the last element of a collection.

func Matches

func Matches(expr, v string) (bool, error)

Matches returns if a string matches a given regular expression.

func ParseBool

func ParseBool(raw interface{}) (bool, error)

ParseBool attempts to parse a value as a bool. "truthy" values include "true", "1", "yes". "falsey" values include "false", "0", "no".

func ParseFloat64

func ParseFloat64(v string) (float64, error)

ParseFloat64 parses a value as a float64.

func ParseInt

func ParseInt(v interface{}) (int, error)

ParseInt parses a value as an integer.

func ParseInt64

func ParseInt64(v interface{}) (int64, error)

ParseInt64 parses a value as an int64.

func ParseJSON

func ParseJSON(v string) (interface{}, error)

ParseJSON returns an object encoded as json.

func ParseTime

func ParseTime(format, v string) (time.Time, error)

ParseTime parses a time string with a given format.

func ParseTimeUnix

func ParseTimeUnix(v int64) time.Time

ParseTimeUnix returns a timestamp from a unix format.

func ParseURL

func ParseURL(v string) (*url.URL, error)

ParseURL parses a url.

func ParseUUID

func ParseUUID(v string) (uuid.UUID, error)

ParseUUID parses a uuid.

func Prefix

func Prefix(pref, v string) string

Prefix appends a given string to a prefix.

func Quote

func Quote(v string) string

Quote returns a string wrapped in " characters. It will trim space before and after, and only add quotes if they don't already exist.

func ReadFile

func ReadFile(path string) (string, error)

ReadFile reads the contents of a file path as a string.

func Render

func Render(templateText string, model any) (template.HTML, error)

Render compiles a template from text and renders it.

func Reverse

func Reverse(collection interface{}) (interface{}, error)

Reverse reverses an array.

func Round

func Round(places, d float64) float64

Round returns the value rounded to a given set of places. It uses midpoint rounding.

func SHA256

func SHA256(v string) string

SHA256 returns the sha256 sum of a string.

func SHA512

func SHA512(v string) string

SHA512 returns the sha512 sum of a string.

func SequenceRange

func SequenceRange(start, end int) []int

SequenceRange returns an array of ints from min to max, not including max. Given (0,5) as inputs, it would return [0,1,2,3,4]

func Slice

func Slice(from, to int, collection interface{}) (interface{}, error)

Slice returns a subrange of a collection.

func Slugify

func Slugify(v string) string

Slugify returns a slug format string. It replaces whitespace with `-` It path escapes any other characters.

func Split

func Split(sep, v string) []string

Split splits a string by a separator.

func SplitN

func SplitN(sep string, n float64, v string) []string

SplitN splits a string by a separator a given number of times.

func StripQuotes

func StripQuotes(v string) string

StripQuotes strips leading and trailing quotes.

func Suffix

func Suffix(suf, v string) string

Suffix appends a given prefix to a string.

func TSV

func TSV(collection interface{}) (string, error)

TSV returns a tab separated values of a given collection.

func TimeDay

func TimeDay(t time.Time) int

TimeDay returns the day component of a timestamp.

func TimeFormat

func TimeFormat(format string, t time.Time) string

TimeFormat returns the time with a given format string.

func TimeFormatDateLong

func TimeFormatDateLong(t time.Time) string

TimeFormatDateLong returns the short date for a timestamp.

func TimeFormatDateMonthDay

func TimeFormatDateMonthDay(t time.Time) string

TimeFormatDateMonthDay returns the month dat format for a timestamp. The format string is "1/2".

func TimeFormatDateShort

func TimeFormatDateShort(t time.Time) string

TimeFormatDateShort returns the short date for a timestamp. The format string is "1/02/2006"

func TimeFormatDateShortRev

func TimeFormatDateShortRev(t time.Time) string

TimeFormatDateShortRev returns the short date for a timestamp in YYYY/mm/dd format.

func TimeFormatKitchen

func TimeFormatKitchen(t time.Time) string

TimeFormatTimeKitchen returns the kitchen format for a timestamp. The format string is "3:04PM".

func TimeFormatMedium

func TimeFormatMedium(t time.Time) string

TimeFormatTimeMedium returns the medium format for a timestamp. The format string is "1/02/2006 3:04:05 PM".

func TimeFormatRFC3339

func TimeFormatRFC3339(t time.Time) string

TimeFormatRFC3339 returns the RFC3339 format for a timestamp.

func TimeFormatShort

func TimeFormatShort(t time.Time) string

TimeFormatShort returns the short format for a timestamp. The format string is "1/02/2006 3:04:05 PM".

func TimeHour

func TimeHour(t time.Time) int

TimeHour returns the hour component of a timestamp.

func TimeInLocation

func TimeInLocation(loc string, t time.Time) (time.Time, error)

TimeInLocation returns the time in a given location by string. If the location is invalid, this will error.

func TimeInUTC

func TimeInUTC(t time.Time) time.Time

TimeInUTC returns the time in a given location by string. If the location is invalid, this will error.

func TimeIsEpoch

func TimeIsEpoch(t time.Time) bool

TimeIsEpoch returns if the time is the unix epoch time or not.

func TimeIsZero

func TimeIsZero(t time.Time) bool

TimeIsZero returns if the time is set or not.

func TimeMillisecond

func TimeMillisecond(t time.Time) int

TimeMillisecond returns the millisecond component of a timestamp.

func TimeMinute

func TimeMinute(t time.Time) int

TimeMinute returns the minute component of a timestamp.

func TimeMonth

func TimeMonth(t time.Time) int

TimeMonth returns the month component of a timestamp.

func TimeNow

func TimeNow() time.Time

TimeNow returns the current time in the system timezone.

func TimeNowUTC

func TimeNowUTC() time.Time

TimeNowUTC returns the current time in the UTC timezone.

func TimeSecond

func TimeSecond(t time.Time) int

TimeSecond returns the seconds component of a timestamp.

func TimeSince

func TimeSince(t time.Time) time.Duration

TimeSince returns the duration since a given timestamp. It is relative, meaning the value returned can be negative.

func TimeSinceUTC

func TimeSinceUTC(t time.Time) time.Duration

TimeSinceUTC returns the duration since a given timestamp in UTC. It is relative, meaning the value returned can be negative.

func TimeSub

func TimeSub(t1, t2 time.Time) time.Duration

TimeSub the duration difference between two times.

func TimeUnix

func TimeUnix(t time.Time) int64

TimeUnix returns the unix format for a timestamp.

func TimeUnixNano

func TimeUnixNano(t time.Time) int64

TimeUnixNano returns the timetamp as nanoseconds from 1970-01-01.

func TimeYear

func TimeYear(t time.Time) int

TimeYear returns the year component of a timestamp.

func ToJSON

func ToJSON(v any) (string, error)

ToJSON returns an object encoded as json.

func ToJSONPretty

func ToJSONPretty(v any) (string, error)

ToJSONPretty encodes an object as json with indentation.

func ToLower

func ToLower(v string) string

ToLower returns a string case shifted to lower case.

func ToTitle

func ToTitle(v string) string

ToTitle returns a title cased string.

func ToUpper

func ToUpper(v string) string

ToUpper returns a string case shifted to upper case.

func TrimPrefix

func TrimPrefix(prefix, v string) string

TrimPrefix returns if a string has a given prefix.

func TrimSpace

func TrimSpace(v string) string

TrimSpace trims whitespace from the beginning and end of a string.

func TrimSuffix

func TrimSuffix(suffix, v string) string

TrimSuffix returns if a string has a given suffix.

func URLEncode

func URLEncode(value string) string

URLEncode encodes a value as a url token.

func URLHost

func URLHost(v *url.URL) string

URLHost returns the host of a url.

func URLPath

func URLPath(v *url.URL) string

URLPath returns the url path.

func URLPort

func URLPort(v *url.URL) string

URLPort returns the url port. If none is explicitly specified, this will return empty string.

func URLQuery

func URLQuery(name string, v *url.URL) string

URLQuery returns a url query param.

func URLRawQuery

func URLRawQuery(v *url.URL) string

URLRawQuery returns the url raw query.

func URLScheme

func URLScheme(v *url.URL) string

URLScheme returns the scheme of a url.

func UUIDv4

func UUIDv4() uuid.UUID

UUIDv4 generates a uuid v4.

func WithURLHost

func WithURLHost(host string, u *url.URL) *url.URL

WithURLHost returns the host of a url.

func WithURLPath

func WithURLPath(path string, u *url.URL) *url.URL

WithURLPath returns the url path.

func WithURLPort

func WithURLPort(port string, u *url.URL) *url.URL

WithURLPort sets the url port.

func WithURLQuery

func WithURLQuery(key, value string, u *url.URL) *url.URL

WithURLQuery returns a url query param.

func WithURLRawQuery

func WithURLRawQuery(rawQuery string, u *url.URL) *url.URL

WithURLRawQuery returns the url path.

func WithURLScheme

func WithURLScheme(scheme string, u *url.URL) *url.URL

WithURLScheme returns the scheme of a url.

Types

type FormProvider

type FormProvider interface {
	Form(string) template.HTML
}

FormProvider will shortcut reflection in the `FormFor` call.

type InputProvider

type InputProvider interface {
	Input() template.HTML
}

InputProvider will shortcut reflection in the input generation step of a `FormFor` or `ControlFor` call.

Jump to

Keyboard shortcuts

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