Documentation
¶
Index ¶
- Variables
- func Must[T any](r T, err error) T
- type AnyMatcher
- type ID
- func FromBytes(input []byte) (ID, error)
- func FromString(s string) (ID, error)
- func New() (ID, error)
- func NewRandom() (ID, error)
- func NewRandomTagged(tag byte) (ID, error)
- func NewTagged(tag byte) (ID, error)
- func Parse(s string) (ID, error)
- func ParseWithRequire(s string, reqs ...Requirement) (ID, error)
- func (r ID) Bytes() []byte
- func (r *ID) ClearTag() *ID
- func (r ID) Equal(other ID) bool
- func (r ID) Format(f fmt.State, c rune)
- func (r ID) HasTag(tag byte) bool
- func (r ID) HasType(t Type) bool
- func (r ID) IsNil() bool
- func (r ID) IsTagged() bool
- func (r ID) MarshalBinary() ([]byte, error)
- func (r ID) MarshalJSON() ([]byte, error)
- func (r ID) MarshalText() ([]byte, error)
- func (r *ID) Scan(src interface{}) error
- func (r *ID) SetTag(tag byte) *ID
- func (r *ID) SetTime(ts time.Time) error
- func (r ID) String() string
- func (r ID) Tag() byte
- func (r ID) Time() time.Time
- func (r ID) ToBase32String() string
- func (r ID) ToBase64String() string
- func (r ID) ToHexString() string
- func (r ID) ToString() string
- func (r ID) Type() Type
- func (r *ID) UnmarshalBinary(data []byte) error
- func (r *ID) UnmarshalJSON(b []byte) error
- func (r *ID) UnmarshalText(b []byte) error
- func (r ID) Value() (driver.Value, error)
- type NullID
- type NullRefID
- type RefID
- type Requirement
- type Tagger
- func (t Tagger) AnyMatcher() AnyMatcher
- func (t Tagger) HasCorrectTag(r ID) bool
- func (t Tagger) HasTag(r ID, tag byte) bool
- func (t Tagger) IsTagged(r ID) bool
- func (t Tagger) New() (ID, error)
- func (t Tagger) NewRandom() (ID, error)
- func (t Tagger) Parse(s string) (ID, error)
- func (t Tagger) ParseWithRequire(s string, reqs ...Requirement) (ID, error)
- func (t Tagger) Tag() byte
- type Type
Constants ¶
This section is empty.
Variables ¶
var ( // Nil is the nil ID, that has all 128 bits set to zero. Nil = ID{} )
Functions ¶
func Must ¶
Must is a helper that wraps a call to a function returning (any, error) and panics if the error is non-nil. It is intended for use in variable initializations such as
var ( refA = refid.Must(refid.New()) refB = refid.Must(refid.NewTagged(2)) refC = refid.Must(refid.Parse("0r2nbq0wqhjg186167t0gcd1gw")) refD = refid.Must(refid.ParseTagged("0r2nbq0wqhjg186167t0gcd1gw", 2)) )
Types ¶
type AnyMatcher ¶
type AnyMatcher struct {
// contains filtered or unexported fields
}
A matcher that supports the following interfaces:
func MatchAny ¶
func MatchAny(tag byte) AnyMatcher
Create a AnyMatcher matcher that matches that matches against a specific Tag. Any valid IDs that do not match the tag specified, will be considered not matching.
If tag is 0, will support matching any ID (tag is then ignored)
Example usage:
mock.ExpectQuery("^INSERT INTO some_table (.+)"). WithArgs(refid.MatchAny(1), 1). WillReturnRows(rows)
func (AnyMatcher) Match ¶
func (a AnyMatcher) Match(v interface{}) bool
type ID ¶
type ID [size]byte
A ID is a 16 byte identifier that has:
- tagging support (support for 255 distinct tag types)
- go/sql scanner/valuer support
- multiple encodings supported: native (base32), base64, base16 (hex)
func FromBytes ¶
FromBytes creates a new ID from a byte slice. Returns an error if the slice does not have a length of 16. The bytes are copied from the slice.
func New ¶
New returns a new TimePrefixed type ID.
If random bytes cannot be generated, it will return an error.
func NewRandom ¶
NewRandom returns a new RandomPrefixed type ID.
If random bytes cannot be generated, it will return an error.
func NewRandomTagged ¶
NewRandomTagged returns a new RandomPrefixed type ID tagged with tag.
If random bytes cannot be generated, it will return an error.
func NewTagged ¶
NewTagged returns a new TimePrefixed type ID tagged with tag.
If random bytes cannot be generated, it will return an error.
func Parse ¶
Parse parses a textual ID representation, and returns a ID. Supports parsing the following text formats:
- native/base32 (Crockford's alphabet)
- base64
- base16/hex
Will return an error on parse failure.
func ParseWithRequire ¶
func ParseWithRequire(s string, reqs ...Requirement) (ID, error)
ParseWithRequire parses a textual ID representation (same formats as Parse), while additionally requiring each reqs Requirement to pass, and returns a ID.
Returns an error if ID fails to parse or if any of the reqs Requirements fail.
Example:
ParseWithRequire("afd661f4f2tg2vr3dca92qp6k8", HasType(RandomPrefix))
func (ID) Format ¶
Format implements the fmt.Formatter interface.
func (ID) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
Purposefully a value receiver for flexibility (from EffectiveGo): "The rule about pointers vs. values for receivers is that value methods can be invoked on pointers and values, but pointer methods can only be invoked on pointers.""
func (ID) MarshalJSON ¶
MarshalJson implements the json.Marshaler interface.
Purposefully a value receiver for flexibility (from EffectiveGo): "The rule about pointers vs. values for receivers is that value methods can be invoked on pointers and values, but pointer methods can only be invoked on pointers.""
func (ID) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*ID) Scan ¶
Scan implements the sql.Scanner interface. A 16-byte slice will be handled by ID.UnmarshalBinary, while a longer byte slice or a string will be handled by ID.UnmarshalText.
func (ID) String ¶
String returns the native (base32 w/Crockford alphabet) textual representation of a ID
func (ID) ToBase32String ¶
ToBase32String is an alias of [String]
func (ID) ToBase64String ¶
String returns the base64 textual representation of a ID
func (ID) ToHexString ¶
String returns the base16/hex textual representation of a ID
func (*ID) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It will return an error if the slice isn't of appropriate size.
func (*ID) UnmarshalJSON ¶
UnmarshalJson implements the json.Unmarshaler interface.
func (*ID) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. It will return an error if the slice isn't of appropriate size.
type NullID ¶
NullID can be used with the standard sql package to represent a ID value that can be NULL in the database.
func (NullID) MarshalJSON ¶
func (*NullID) Scan ¶
Scan implements the sql.Scanner interface.
func (*NullID) UnmarshalJSON ¶
UnmarshalJSON unmarshals a NullID
type Requirement ¶
func HasTag ¶
func HasTag(tag byte) Requirement
func HasType ¶
func HasType(t Type) Requirement
type Tagger ¶
type Tagger byte
A Tagger is a convenience container for encoding and parsing ID's of a specific tag.
func (Tagger) AnyMatcher ¶
func (t Tagger) AnyMatcher() AnyMatcher
AnyMather returns an AnyMatcher, which will match only against a ID tagged with the same tag as the Tagger
func (Tagger) HasCorrectTag ¶
HasTag reports whether a ID is tagged with the same tag as the Tagger
func (Tagger) IsTagged ¶
IsTagged reports wheater a ID is tagged at all. Note: This only checks that the ID is tagged, not that it is tagged with the same tag as Tagger. For that functionality use Tagger.HasCorrectTag.
func (Tagger) NewRandom ¶
NewRandom generates a new [RandomPrefix] type ID with tag set to the tag of the Tagger
func (Tagger) Parse ¶
Parse parses a ID, additionally enforcing that it is tagged with the same tag as the Tagger
func (Tagger) ParseWithRequire ¶
func (t Tagger) ParseWithRequire(s string, reqs ...Requirement) (ID, error)
ParseWithRequire parses a textual ID representation (same formats as Parse), enforcing that it is tagged with the same tag as the Tagger, while additionally requiring each reqs Requirement to pass, and returns a ID.
Returns an error if ID fails to parse, is not tagged with the same tag as Tagger, or if any of the reqs Requirements fail.
Example:
ParseWithRequire("afd661f4f2tg2vr3dca92qp6k8", HasType(RandomPrefix))