syntax

package
v0.0.0-...-12d29e9 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: Apache-2.0, MIT Imports: 7 Imported by: 34

Documentation

Overview

Package syntax provides types for identifiers and other string formats.

These are primarily simple string alias types for parsing or verifying protocol-level syntax of identifiers, not routines for things like resolution or verification against application policies.

Index

Constants

View Source
const (
	// Preferred atproto Datetime string syntax, for use with [time.Format].
	//
	// Note that *parsing* syntax is more flexible.
	AtprotoDatetimeLayout = "2006-01-02T15:04:05.999Z"
)
View Source
const (
	Base32SortAlphabet = "234567abcdefghijklmnopqrstuvwxyz"
)

Variables

View Source
var (

	// special handle string constant indicating that handle resolution failed
	HandleInvalid = Handle("handle.invalid")
)

Functions

func Base32Sort

func Base32Sort() *base32.Encoding

func ParseDatetimeTime

func ParseDatetimeTime(raw string) (time.Time, error)

Validates and converts a string to a golang time.Time in a single step.

Types

type ATURI

type ATURI string

String type which represents a syntaxtually valid AT URI, as would pass Lexicon syntax validation for the 'at-uri' field (no query or fragment parts)

Always use ParseATURI instead of wrapping strings directly, especially when working with input.

Syntax specification: https://atproto.com/specs/at-uri-scheme

func ParseATURI

func ParseATURI(raw string) (ATURI, error)

func (ATURI) Authority

func (n ATURI) Authority() AtIdentifier

Every valid ATURI has a valid AtIdentifier in the authority position.

If this ATURI is malformed, returns empty

func (ATURI) Collection

func (n ATURI) Collection() NSID

Returns a valid NSID if there is one in the appropriate part of the path, otherwise empty.

func (ATURI) MarshalText

func (a ATURI) MarshalText() ([]byte, error)

func (ATURI) Normalize

func (n ATURI) Normalize() ATURI

func (ATURI) Path

func (n ATURI) Path() string

Returns path segment, without leading slash, as would be used in an atproto repository key. Or empty string if there is no path.

func (ATURI) RecordKey

func (n ATURI) RecordKey() RecordKey

func (ATURI) String

func (n ATURI) String() string

func (*ATURI) UnmarshalText

func (a *ATURI) UnmarshalText(text []byte) error

type AtIdentifier

type AtIdentifier struct {
	Inner interface{}
}

func ParseAtIdentifier

func ParseAtIdentifier(raw string) (*AtIdentifier, error)

func (AtIdentifier) AsDID

func (n AtIdentifier) AsDID() (DID, error)

func (AtIdentifier) AsHandle

func (n AtIdentifier) AsHandle() (Handle, error)

func (AtIdentifier) IsDID

func (n AtIdentifier) IsDID() bool

func (AtIdentifier) IsHandle

func (n AtIdentifier) IsHandle() bool

func (AtIdentifier) MarshalText

func (a AtIdentifier) MarshalText() ([]byte, error)

func (AtIdentifier) Normalize

func (n AtIdentifier) Normalize() AtIdentifier

func (AtIdentifier) String

func (n AtIdentifier) String() string

func (*AtIdentifier) UnmarshalText

func (a *AtIdentifier) UnmarshalText(text []byte) error

type CID

type CID string

Represents a CIDv1 in string format, as would pass Lexicon syntax validation.

You usually want to use the github.com/ipfs/go-cid package and type when working with CIDs ("Links") in atproto. This specific type (syntax.CID) is an informal/incomplete helper specifically for doing fast string verification or pass-through without parsing, re-serialization, or normalization.

Always use ParseCID instead of wrapping strings directly, especially when working with network input.

func ParseCID

func ParseCID(raw string) (CID, error)

func (CID) MarshalText

func (c CID) MarshalText() ([]byte, error)

func (CID) String

func (c CID) String() string

func (*CID) UnmarshalText

func (c *CID) UnmarshalText(text []byte) error

type DID

type DID string

Represents a syntaxtually valid DID identifier, as would pass Lexicon syntax validation.

Always use ParseDID instead of wrapping strings directly, especially when working with input.

Syntax specification: https://atproto.com/specs/did

func ParseDID

func ParseDID(raw string) (DID, error)

func (DID) AtIdentifier

func (d DID) AtIdentifier() AtIdentifier

func (DID) Identifier

func (d DID) Identifier() string

The final "identifier" segment of the DID

func (DID) MarshalText

func (d DID) MarshalText() ([]byte, error)

func (DID) Method

func (d DID) Method() string

The "method" part of the DID, between the 'did:' prefix and the final identifier segment, normalized to lower-case.

func (DID) String

func (d DID) String() string

func (*DID) UnmarshalText

func (d *DID) UnmarshalText(text []byte) error

type Datetime

type Datetime string

Represents the a Datetime in string format, as would pass Lexicon syntax validation: the intersection of RFC-3339 and ISO-8601 syntax.

Always use ParseDatetime instead of wrapping strings directly, especially when working with network input.

Syntax is specified at: https://atproto.com/specs/lexicon#datetime

func DatetimeNow

func DatetimeNow() Datetime

Creates a new valid Datetime string matching the current time, in preferred syntax.

func ParseDatetime

func ParseDatetime(raw string) (Datetime, error)

func ParseDatetimeLenient

func ParseDatetimeLenient(raw string) (Datetime, error)

func (Datetime) MarshalText

func (d Datetime) MarshalText() ([]byte, error)

func (Datetime) String

func (d Datetime) String() string

func (Datetime) Time

func (d Datetime) Time() time.Time

Parses the Datetime string in to a golang time.Time.

This method assumes that ParseDatetime was used to create the Datetime, which already verified parsing, and thus that time.Parse will always succeed. In the event of an error, zero/nil will be returned.

func (*Datetime) UnmarshalText

func (d *Datetime) UnmarshalText(text []byte) error

type Handle

type Handle string

String type which represents a syntaxtually valid handle identifier, as would pass Lexicon syntax validation.

Always use ParseHandle instead of wrapping strings directly, especially when working with input.

Syntax specification: https://atproto.com/specs/handle

func ParseHandle

func ParseHandle(raw string) (Handle, error)

func (Handle) AllowedTLD

func (h Handle) AllowedTLD() bool

Some top-level domains (TLDs) are disallowed for registration across the atproto ecosystem. The *syntax* is valid, but these should never be considered acceptable handles for account registration or linking.

The reserved '.test' TLD is allowed, for testing and development. It is expected that '.test' domain resolution will fail in a real-world network.

func (Handle) AtIdentifier

func (h Handle) AtIdentifier() AtIdentifier

func (Handle) IsInvalidHandle

func (h Handle) IsInvalidHandle() bool

Is this the special "handle.invalid" handle?

func (Handle) MarshalText

func (h Handle) MarshalText() ([]byte, error)

func (Handle) Normalize

func (h Handle) Normalize() Handle

func (Handle) String

func (h Handle) String() string

func (Handle) TLD

func (h Handle) TLD() string

func (*Handle) UnmarshalText

func (h *Handle) UnmarshalText(text []byte) error

type Language

type Language string

Represents a Language specifier in string format, as would pass Lexicon syntax validation.

Always use ParseLanguage instead of wrapping strings directly, especially when working with network input.

The syntax is BCP-47. This is a partial/naive parsing implementation, designed for fast validation and exact-string passthrough with no normaliztion. For actually working with BCP-47 language specifiers in atproto code bases, we recommend the golang.org/x/text/language package.

func ParseLanguage

func ParseLanguage(raw string) (Language, error)

func (Language) MarshalText

func (l Language) MarshalText() ([]byte, error)

func (Language) String

func (l Language) String() string

func (*Language) UnmarshalText

func (l *Language) UnmarshalText(text []byte) error

type NSID

type NSID string

String type which represents a syntaxtually valid Namespace Identifier (NSID), as would pass Lexicon syntax validation.

Always use ParseNSID instead of wrapping strings directly, especially when working with input.

Syntax specification: https://atproto.com/specs/nsid

func ParseNSID

func ParseNSID(raw string) (NSID, error)

func (NSID) Authority

func (n NSID) Authority() string

Authority domain name, in regular DNS order, not reversed order, normalized to lower-case.

func (NSID) MarshalText

func (n NSID) MarshalText() ([]byte, error)

func (NSID) Name

func (n NSID) Name() string

func (NSID) Normalize

func (n NSID) Normalize() NSID

func (NSID) String

func (n NSID) String() string

func (*NSID) UnmarshalText

func (n *NSID) UnmarshalText(text []byte) error

type RecordKey

type RecordKey string

String type which represents a syntaxtually valid RecordKey identifier, as could be included in an AT URI

Always use ParseRecordKey instead of wrapping strings directly, especially when working with input.

Syntax specification: https://atproto.com/specs/record-key

func ParseRecordKey

func ParseRecordKey(raw string) (RecordKey, error)

func (RecordKey) MarshalText

func (r RecordKey) MarshalText() ([]byte, error)

func (RecordKey) String

func (r RecordKey) String() string

func (*RecordKey) UnmarshalText

func (r *RecordKey) UnmarshalText(text []byte) error

type TID

type TID string

Represents a TID in string format, as would pass Lexicon syntax validation.

Always use ParseTID instead of wrapping strings directly, especially when working with network input.

Syntax specification: https://atproto.com/specs/record-key

func NewTID

func NewTID(unixMicros int64, clockId uint) TID

Constructs a new TID from a UNIX timestamp (in milliseconds) and clock ID value.

func NewTIDFromInteger

func NewTIDFromInteger(v uint64) TID

func NewTIDFromTime

func NewTIDFromTime(ts time.Time, clockId uint) TID

Constructs a new TID from a time.Time and clock ID value

func NewTIDNow

func NewTIDNow(clockId uint) TID

Naive (unsafe) one-off TID generation with the current time.

You should usually use a TIDClock to ensure monotonic output.

func ParseTID

func ParseTID(raw string) (TID, error)

func (TID) ClockID

func (t TID) ClockID() uint

Returns the clock ID part of this TID, as an unsigned integer

func (TID) Integer

func (t TID) Integer() uint64

Returns full integer representation of this TID (not used often)

func (TID) MarshalText

func (t TID) MarshalText() ([]byte, error)

func (TID) String

func (t TID) String() string

func (TID) Time

func (t TID) Time() time.Time

Returns the golang time.Time corresponding to this TID's timestamp.

func (*TID) UnmarshalText

func (t *TID) UnmarshalText(text []byte) error

type TIDClock

type TIDClock struct {
	ClockID uint
	// contains filtered or unexported fields
}

TID generator, which keeps state to ensure TID values always monotonically increase.

Uses sync.Mutex, so may block briefly but safe for concurrent use.

func NewTIDClock

func NewTIDClock(clockId uint) *TIDClock

func (*TIDClock) Next

func (c *TIDClock) Next() TID

type URI

type URI string

Represents an arbitrary URI in string format, as would pass Lexicon syntax validation.

The syntax is minimal and permissive, designed for fast verification and exact-string passthrough, not schema-specific parsing or validation. For example, will not validate AT-URI or DID strings.

Always use ParseURI instead of wrapping strings directly, especially when working with network input.

func ParseURI

func ParseURI(raw string) (URI, error)

func (URI) MarshalText

func (u URI) MarshalText() ([]byte, error)

func (URI) String

func (u URI) String() string

func (*URI) UnmarshalText

func (u *URI) UnmarshalText(text []byte) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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