utils

package
v0.0.0-...-a2de6b1 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2024 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindPhoneNumbers

func FindPhoneNumbers(s, country string) []string

FindPhoneNumbers finds phone numbers anywhere in the given string

func Indent

func Indent(s string, prefix string) string

Indent indents each non-empty line in the given string

func IsNil

func IsNil(v interface{}) bool

IsNil returns whether the given object is nil or an interface to a nil

func Max

func Max[T constraints.Ordered](x, y T) T

Max returns the maximum of two values

func Min

func Min[T constraints.Ordered](x, y T) T

Min returns the minimum of two values

func ParsePhoneNumber

func ParsePhoneNumber(s, country string) string

ParsePhoneNumber tries to parse the given string as a phone number. If successful, it returns it formatted as E164.

func PrefixOverlap

func PrefixOverlap(s1, s2 string) int

PrefixOverlap returns the number of prefix characters which s1 and s2 have in common

func ReadTypeFromJSON

func ReadTypeFromJSON(data []byte) (string, error)

ReadTypeFromJSON reads a field called `type` from the given JSON

func RegisterStructValidator

func RegisterStructValidator(fn validator.StructLevelFunc, types ...interface{})

RegisterStructValidator registers a struct level validator

func RegisterValidatorAlias

func RegisterValidatorAlias(alias, tags string, message ErrorMessageFunc)

RegisterValidatorAlias registers a tag alias

func RegisterValidatorTag

func RegisterValidatorTag(tag string, fn validator.Func, message ErrorMessageFunc)

RegisterValidatorTag registers a tag

func ReplaceEscapedNulls

func ReplaceEscapedNulls(data []byte, repl []byte) []byte

replaces any `\u0000` sequences with the given replacement sequence which may be empty. A sequence such as `\\u0000` is preserved as it is an escaped slash followed by the sequence `u0000`

func Snakify

func Snakify(text string) string

Snakify turns the passed in string into a context reference. We replace all whitespace characters with _ and replace any duplicate underscores

func StringSet

func StringSet(s []string) map[string]bool

StringSet converts a slice of strings to a set (a string > bool map)

func StringSetKeys

func StringSetKeys(m map[string]bool) []string

StringSetKeys returns the keys of string set in lexical order

func StringSliceContains

func StringSliceContains(slice []string, str string, caseSensitive bool) bool

StringSliceContains determines whether the given slice of strings contains the given string

func StringSlices

func StringSlices(s string, indices []int) []string

StringSlices returns the slices of s defined by pairs of indexes in indices

func TokenizeString

func TokenizeString(str string) []string

TokenizeString returns the words in the passed in string, split by non word characters including emojis

func TokenizeStringByChars

func TokenizeStringByChars(str string, chars string) []string

TokenizeStringByChars returns the words in the passed in string, split by the chars in the given string

func TokenizeStringByUnicodeSeg

func TokenizeStringByUnicodeSeg(str string) []string

TokenizeStringByUnicodeSeg tokenizes the given string using the Unicode Text Segmentation standard described at http://www.unicode.org/reports/tr29/

func Truncate

func Truncate(s string, limit int) string

Truncate truncates the given string to ensure it's less than limit characters

func TruncateEllipsis

func TruncateEllipsis(s string, limit int) string

TruncateEllipsis truncates the given string and adds ellipsis where the input is cut

func UnmarshalAndValidate

func UnmarshalAndValidate(data []byte, obj interface{}) error

UnmarshalAndValidate is a convenience function to unmarshal an object and validate it

func UnmarshalAndValidateWithLimit

func UnmarshalAndValidateWithLimit(reader io.ReadCloser, s interface{}, limit int64) error

UnmarshalAndValidateWithLimit unmarshals a struct with a limit on how many bytes can be read from the given reader

func Validate

func Validate(obj interface{}) error

Validate will run validation on the given object and return a set of field specific errors in the format: field <fieldname> <tag specific message>

For example: "field 'flows' is required"

Types

type Attachment

type Attachment string

Attachment is a media attachment on a message in the format <content-type>:<url>. Content type may be a full media type or may omit the subtype when it is unknown.

Examples:

func (Attachment) ContentType

func (a Attachment) ContentType() string

ContentType returns the MIME type of this attachment

func (Attachment) ToParts

func (a Attachment) ToParts() (string, string)

ToParts splits an attachment string into content-type and URL

func (Attachment) URL

func (a Attachment) URL() string

URL returns the full URL of this attachment

type ErrorMessageFunc

type ErrorMessageFunc func(validator.FieldError) string

ErrorMessageFunc is the type for a function that can convert a field error to user friendly message

type Redactor

type Redactor func(s string) string

Redactor is a function which can redact the given string

func NewRedactor

func NewRedactor(mask string, values ...string) Redactor

NewRedactor creates a new redaction function which replaces the given values

type RichError

type RichError interface {
	error
	Code() string
	Extra() map[string]string
}

RichError is a common interface for error types that can provide more detail

type Typed

type Typed interface {
	Type() string
}

Typed is an interface of objects that are marshalled as typed envelopes

type TypedEnvelope

type TypedEnvelope struct {
	Type string `json:"type" validate:"required"`
}

TypedEnvelope can be mixed into envelopes that have a type field

type ValidationErrors

type ValidationErrors []error

ValidationErrors combines multiple validation errors as a single error

func (ValidationErrors) Error

func (e ValidationErrors) Error() string

Error returns a string representation of these validation errors

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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