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 ¶ added in v1.0.6
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 ¶ added in v1.0.2
NewRandom returns a new RandomPrefixed type ID.
If random bytes cannot be generated, it will return an error.
func NewRandomTagged ¶ added in v1.0.2
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 ¶ added in v1.0.2
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) Bytes ¶ added in v1.0.6
Bytes returns a slice of a copy of the current ID underlying data.
func (ID) Equal ¶ added in v1.0.6
Equal compares a ID to another ID to see if they have the same underlying bytes.
func (ID) Format ¶ added in v1.0.6
Format implements the fmt.Formatter interface.
func (ID) HasTag ¶ added in v1.0.6
IsTagged reports whether the ID is tagged and if so, if it is tagged with tag.
func (ID) MarshalBinary ¶ added in v1.0.6
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 ¶ added in v1.0.6
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 ¶ added in v1.0.6
MarshalText implements the encoding.TextMarshaler interface.
func (*ID) Scan ¶ added in v1.0.6
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) SetTime ¶ added in v1.0.6
SetTime sets the time component of a ID to the time specified by ts.
func (ID) String ¶ added in v1.0.6
String returns the native (base32 w/Crockford alphabet) textual represenation of a ID
func (ID) Tag ¶ added in v1.0.6
Tag returns the current tag of the ID. If the ID is untagged, it will retrun 0.
func (ID) ToBase32String ¶ added in v1.0.6
ToBase32String is an alias of [String]
func (ID) ToBase64String ¶ added in v1.0.6
String returns the base64 textual represenation of a ID
func (ID) ToHexString ¶ added in v1.0.6
String returns the base16/hex textual represenation of a ID
func (*ID) UnmarshalBinary ¶ added in v1.0.6
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It will return an error if the slice isn't of appropriate size.
func (*ID) UnmarshalJSON ¶ added in v1.0.6
UnmarshalJson implements the json.Unmarshaler interface.
func (*ID) UnmarshalText ¶ added in v1.0.6
UnmarshalText implements the encoding.TextUnmarshaler interface. It will return an error if the slice isn't of appropriate size.
type NullID ¶ added in v1.0.6
NullID can be used with the standard sql package to represent a ID value that can be NULL in the database.
func (NullID) MarshalJSON ¶ added in v1.0.6
func (*NullID) Scan ¶ added in v1.0.6
Scan implements the sql.Scanner interface.
func (*NullID) UnmarshalJSON ¶ added in v1.0.6
UnmarshalJSON unmarshals a NullID
type Requirement ¶ added in v1.0.2
func HasTag ¶ added in v1.0.2
func HasTag(tag byte) Requirement
func HasType ¶ added in v1.0.2
func HasType(t Type) Requirement
type Tagger ¶
type Tagger byte
A Tagger is a conveniece 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 ¶ added in v1.0.2
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 ¶ added in v1.0.2
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))