util

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: AGPL-3.0 Imports: 12 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. Compared to Deduplicate() this returns []K, NOT input type []T.

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 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 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 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 PtrValueOr added in v0.13.0

func PtrValueOr[T any](t *T, _default T) T

PtrValueOr returns either value of ptr, or default.

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 ShortcodeDomain added in v0.7.0

func ShortcodeDomain(emoji *gtsmodel.Emoji) string

ShortcodeDomain returns the [shortcode]@[domain] for the given emoji.

func UniqueStrings

func UniqueStrings(strings []string) []string

UniqueStrings returns a deduplicated version of the given slice of strings, without changing the order of the entries.

func UniqueURIs added in v0.3.4

func UniqueURIs(uris []*url.URL) []*url.URL

UniqueURIs returns a deduplicated version of the given slice of URIs, without changing the order of the entries.

Types

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.

Jump to

Keyboard shortcuts

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