util

package
v0.17.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2024 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const ISO8601 = "2006-01-02T15:04:05.000Z"

ISO8601 is a formatter for serializing times that forces ISO8601 behavior.

Variables

This section is empty.

Functions

func Collate added in v0.14.0

func Collate[T any, K comparable](in []T, get func(T) K) []K

Collate will collect the values of type K from input type []T, passing each item to 'get' and deduplicating the end result. This is equivalent to calling Gather() followed by Deduplicate().

func DePunify added in v0.9.0

func DePunify(domain string) (string, error)

DePunify converts the given punycode string to its original unicode representation (lowercased). Noop if the domain is (already) not puny.

Returns an error if conversion fails.

func Decr added in v0.17.0

func Decr[N Number](n N) N

Decr performs a safe decrement of n, clamping minimum value at zero.

func Deduplicate added in v0.14.0

func Deduplicate[T comparable](in []T) []T

Deduplicate deduplicates entries in the given slice.

func DeduplicateFunc added in v0.14.0

func DeduplicateFunc[T any, C comparable](in []T, key func(v T) C) []T

DeduplicateFunc deduplicates entries in the given slice, using the result of key() to gauge uniqueness.

func Div added in v0.17.0

func Div[N Number](n1, n2 N) N

Div performs a safe division of n1 and n2, checking for zero n2. In the case of zero n2, zero is returned.

func DoOnce added in v0.14.0

func DoOnce(fn func()) func()

DoOnce wraps a function to only perform it once.

func EmptyPageableResponse added in v0.6.0

func EmptyPageableResponse() *apimodel.PageableResponse

EmptyPageableResponse just returns an empty PageableResponse with no link header or items.

func EqualPtrs added in v0.13.0

func EqualPtrs[T comparable](t1, t2 *T) bool

EqualPtrs returns whether the values contained within two comparable ptr types are equal.

func ExtractNamestringParts added in v0.3.5

func ExtractNamestringParts(namestring string) (username, host string, err error)

ExtractNamestringParts extracts the username test_user and the domain example.org from a string like @test_user@example.org.

If nothing is matched, it will return an error.

func ExtractWebfingerParts added in v0.3.5

func ExtractWebfingerParts(subject string) (
	string,
	string,
	error,
)

ExtractWebfingerParts returns the username and domain from the "subject" part of a webfinger response: either an account namestring or an actor URI.

All AP implementations in the wild perform webfinger account resource queries with the "acct" scheme and without a leading "@"" on the username. This is also the format the "subject" in a webfinger response adheres to.

Despite this fact, we're permissive about a single leading @. This makes a query for "acct:user@domain.tld" and "acct:@user@domain.tld" equivalent.

We also permit a resource of "user@domain.tld" or "@user@domain.tld", without a scheme. In that case it gets interpreted as if it was using "acct:".

Will error if parsing fails, or if the extracted username or domain are empty.

func FormatISO8601 added in v0.3.4

func FormatISO8601(t time.Time) string

FormatISO8601 converts the given time to UTC and then formats it using the ISO8601 const, which the Mastodon API is able to understand.

func FromSet added in v0.17.0

func FromSet[T comparable](in Set[T]) []T

FromSet extracts the values from set to slice.

func Gather added in v0.17.0

func Gather[T, V any](out []V, in []T, get func(T) V) []V

Gather will collect the values of type V from input type []T, passing each item to 'get' and appending V to the return slice.

func GatherIf added in v0.17.0

func GatherIf[T, V any](out []V, in []T, get func(T) (V, bool)) []V

GatherIf is functionally similar to Gather(), but only when return bool is true. If you don't need to check the boolean, Gather() will be very slightly faster.

func KeyBy added in v0.17.0

func KeyBy[S any, T comparable](in []S, key func(S) T) map[T]S

KeyBy creates a map of T->S, keyed by value returned from key func.

func Must added in v0.16.0

func Must(fn func())

Must executes 'fn' repeatedly until it successfully returns without panic.

func OrderBy added in v0.14.0

func OrderBy[T any, K comparable](in []T, keys []K, key func(T) K)

OrderBy orders a slice of given type by the provided alternative slice of comparable type.

func PackagePageableResponse added in v0.6.0

func PackagePageableResponse(params PageableResponseParams) (*apimodel.PageableResponse, gtserror.WithCode)

PackagePageableResponse is a convenience function for returning a bunch of pageable items (notifications, statuses, etc), as well as a Link header to inform callers of where to find next/prev items.

func ParseISO8601 added in v0.6.0

func ParseISO8601(in string) (time.Time, error)

ParseISO8601 parses the given time string according to the ISO8601 const.

func Ptr added in v0.11.0

func Ptr[T any](t T) *T

Ptr returns a pointer to the passed in type

func PtrIf added in v0.17.0

func PtrIf[T comparable](t T) *T

PtrIf returns ptr value only if 't' non-zero.

func PtrOrValue added in v0.17.0

func PtrOrValue[T any](t *T, value T) T

PtrOrValue returns either contained value of ptr, or 'value'.

func PtrOrZero added in v0.17.0

func PtrOrZero[T any](t *T) T

PtrOrZero returns either value of ptr, or zero.

func Punify added in v0.9.0

func Punify(domain string) (string, error)

Punify converts the given domain to lowercase then to punycode (for international domain names).

Returns the resulting domain or an error if the punycode conversion fails.

func PunifyURI added in v0.17.0

func PunifyURI(in *url.URL) (*url.URL, error)

PunifyURI returns a copy of the given URI with the 'host' part converted to punycode.

func PunifyURIToStr added in v0.17.0

func PunifyURIToStr(in *url.URL) (string, error)

PunifyURIToStr returns given URI serialized with the 'host' part converted to punycode.

func URIMatches added in v0.17.0

func URIMatches(expect *url.URL, uris ...*url.URL) (bool, error)

URIMatches returns true if the expected URI matches any of the given URIs, taking account of punycode.

Types

type Number added in v0.17.0

type Number interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~uintptr | ~float32 | ~float64
}

type PageableResponseParams added in v0.6.0

type PageableResponseParams struct {
	Items            []interface{} // Sorted slice of items (statuses, notifications, etc)
	Path             string        // path to use for next/prev queries in the link header
	NextMaxIDKey     string        // key to use for the next max id query param in the link header, defaults to 'max_id'
	NextMaxIDValue   string        // value to use for next max id
	PrevMinIDKey     string        // key to use for the prev min id query param in the link header, defaults to 'min_id'
	PrevMinIDValue   string        // value to use for prev min id
	Limit            int           // limit number of entries to return
	ExtraQueryParams []string      // any extra query parameters to provide in the link header, should be in the format 'example=value'
}

PageableResponseParams models the parameters to pass to PackagePageableResponse.

The given items will be provided in the paged response.

The other values are all used to create the Link header so that callers know which endpoint to query next and previously in order to do paging.

type Set added in v0.17.0

type Set[T comparable] map[T]struct{}

Set represents a hashmap of only keys, useful for deduplication / key checking.

func ToSet added in v0.17.0

func ToSet[T comparable](in []T) Set[T]

ToSet creates a Set[T] from given values.

func ToSetFunc added in v0.17.0

func ToSetFunc[S any, T comparable](in []S, key func(S) T) Set[T]

ToSetFunc creates a Set[T] from input slice, keys provided by func.

func (Set[T]) Has added in v0.17.0

func (s Set[T]) Has(v T) bool

Has returns if value is in Set.

func (Set[T]) In added in v0.17.0

func (s Set[T]) In(vs []T) []T

In returns input slice filtered to only contain those in receiving set.

func (Set[T]) NotIn added in v0.17.0

func (s Set[T]) NotIn(vs []T) []T

NotIn is the functional inverse of In().

Jump to

Keyboard shortcuts

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