util

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLimitReached = errors.New("limit reached")

ErrLimitReached is the error returned by the Limiter and LimitWriter when the predefined limit has been reached

Functions

func DurationToHuman added in v1.3.0

func DurationToHuman(d time.Duration) (str string)

DurationToHuman converts a duration to a human readable format

func ExpandHome added in v1.8.0

func ExpandHome(path string) string

ExpandHome replaces "~" with the user's home directory

func FileExists

func FileExists(filename string) bool

FileExists checks if a file exists, and returns true if it does

func InIntList added in v1.8.0

func InIntList(haystack []int, needle int) bool

InIntList returns true if needle is contained in haystack

func InStringList added in v1.5.2

func InStringList(haystack []string, needle string) bool

InStringList returns true if needle is contained in haystack

func InStringListAll added in v1.8.0

func InStringListAll(haystack []string, needles []string) bool

InStringListAll returns true if all needles are contained in haystack

func ParseFutureTime added in v1.6.0

func ParseFutureTime(s string, now time.Time) (time.Time, error)

ParseFutureTime parses a date/time string to a time.Time. It supports unix timestamps, durations and natural language dates

func ParsePriority added in v1.8.0

func ParsePriority(priority string) (int, error)

ParsePriority parses a priority string into its equivalent integer value

func PriorityString added in v1.9.0

func PriorityString(priority int) (string, error)

PriorityString converts a priority number to a string

func RandomString

func RandomString(length int) string

RandomString returns a random string with a given length

func ShortTopicURL added in v1.9.0

func ShortTopicURL(s string) string

ShortTopicURL shortens the topic URL to be human-friendly, removing the http:// or https://

func SplitNoEmpty added in v1.8.0

func SplitNoEmpty(s string, sep string) []string

SplitNoEmpty splits a string using strings.Split, but filters out empty strings

Types

type CachingEmbedFS added in v1.5.0

type CachingEmbedFS struct {
	ModTime time.Time
	FS      embed.FS
}

CachingEmbedFS is a wrapper around embed.FS that allows setting a ModTime, so that the default static file server can send 304s back. It can be used like this:

  var (
     //go:embed docs
     docsStaticFs     embed.FS
     docsStaticCached = &util.CachingEmbedFS{ModTime: time.Now(), FS: docsStaticFs}
  )

	 http.FileServer(http.FS(docsStaticCached)).ServeHTTP(w, r)

func (CachingEmbedFS) Open added in v1.5.0

func (f CachingEmbedFS) Open(name string) (fs.File, error)

Open opens a file in the embedded filesystem and returns a fs.File with the static ModTime

type Limiter added in v1.1.3

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

Limiter is a helper that allows adding values up to a well-defined limit. Once the limit is reached ErrLimitReached will be returned. Limiter may be used by multiple goroutines.

func NewLimiter added in v1.1.3

func NewLimiter(limit int64) *Limiter

NewLimiter creates a new Limiter

func (*Limiter) Add added in v1.1.3

func (l *Limiter) Add(n int64) error

Add adds n to the limiters internal value, but only if the limit has not been reached. If the limit would be exceeded after adding n, ErrLimitReached is returned.

func (*Limiter) Set added in v1.1.3

func (l *Limiter) Set(n int64)

Set sets the value of the limiter to n. This function ignores the limit. It is meant to set the value based on reality.

func (*Limiter) Sub added in v1.1.3

func (l *Limiter) Sub(n int64)

Sub subtracts a value from the limiters internal value

func (*Limiter) Value added in v1.1.3

func (l *Limiter) Value() int64

Value returns the internal value of the limiter

Jump to

Keyboard shortcuts

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