Documentation
¶
Index ¶
- Constants
- func DePunify(domain string) (string, error)
- func Decr[N Number](n N) N
- func Div[N Number](n1, n2 N) N
- func DoOnce(fn func()) func()
- func EmptyPageableResponse() *apimodel.PageableResponse
- func EqualPtrs[T comparable](t1, t2 *T) bool
- func ExtractNamestringParts(namestring string) (username, host string, err error)
- func ExtractWebfingerParts(subject string) (string, string, error)
- func FormatISO8601(t time.Time) string
- func FormatISO8601Date(t time.Time) string
- func FromSet[T comparable](in Set[T]) []T
- func IsNil(i interface{}) bool
- func KeyBy[S any, T comparable](in []S, key func(S) T) map[T]S
- func Must(fn func())
- func PackagePageableResponse(params PageableResponseParams) (*apimodel.PageableResponse, gtserror.WithCode)
- func ParseISO8601(in string) (time.Time, error)
- func Ptr[T any](t T) *T
- func PtrIf[T comparable](t T) *T
- func PtrOrValue[T any](t *T, value T) T
- func PtrOrZero[T any](t *T) T
- func Punify(domain string) (string, error)
- func PunifySafely(domain string) (string, error)
- func PunifyURI(in *url.URL) (*url.URL, error)
- func PunifyURIToStr(in *url.URL) (string, error)
- func URIMatches(expect *url.URL, uris ...*url.URL) (ok bool, err error)
- type Number
- type PageableResponseParams
- type Set
Constants ¶
const ISO8601 = "2006-01-02T15:04:05.000Z"
ISO8601 is a formatter for serializing times that forces ISO8601 behavior.
const ISO8601Date = "2006-01-02"
Variables ¶
This section is empty.
Functions ¶
func DePunify ¶ added in v0.9.0
DePunify converts any punycode-encoded unicode characters in domain name back to their origin unicode. Please note that this performs minimal validation of domain name.
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 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
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
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
FormatISO8601 converts the given time to UTC and then formats it using the ISO8601 const, which the Mastodon API is able to understand.
func FormatISO8601Date ¶ added in v0.17.0
Mastodon returns UTC dates (without time) for last_status_at/LastStatusAt as a special case, but most of the time you want to use FormatISO8601 instead.
func FromSet ¶ added in v0.17.0
func FromSet[T comparable](in Set[T]) []T
FromSet extracts the values from set to slice.
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 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
ParseISO8601 parses the given time string according to the ISO8601 const.
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 PunifySafely ¶ added in v0.18.0
PunifySafely validates the provided domain name, and converts unicode chars to ASCII, i.e. punified form.
func PunifyURI ¶ added in v0.17.0
PunifyURI returns a new copy of URI with the 'host' part converted to punycode with PunifySafely(). For simple comparisons prefer the faster URIMatches().
func PunifyURIToStr ¶ added in v0.17.0
PunifyURIToStr returns given URI serialized with the 'host' part converted to punycode with PunifySafely(). For simple comparisons prefer the faster URIMatches().
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.
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.