Documentation
¶
Index ¶
- Variables
- func Must[T any](r T, err error) T
- type AnyMatcher
- type NullRefID
- type RefID
- func FromBytes(input []byte) (RefID, error)
- func FromString(s string) (RefID, error)
- func New() (RefID, error)
- func NewRandom() (RefID, error)
- func NewRandomTagged(tag byte) (RefID, error)
- func NewTagged(tag byte) (RefID, error)
- func Parse(s string) (RefID, error)
- func ParseWithRequire(s string, reqs ...Requirement) (RefID, error)
- func (r RefID) Bytes() []byte
- func (r *RefID) ClearTag() *RefID
- func (r RefID) Equal(other RefID) bool
- func (r RefID) Format(f fmt.State, c rune)
- func (r RefID) HasTag(tag byte) bool
- func (r RefID) HasType(t Type) bool
- func (r RefID) IsNil() bool
- func (r RefID) IsTagged() bool
- func (r RefID) MarshalBinary() ([]byte, error)
- func (r RefID) MarshalJSON() ([]byte, error)
- func (r RefID) MarshalText() ([]byte, error)
- func (r *RefID) Scan(src interface{}) error
- func (r *RefID) SetTag(tag byte) *RefID
- func (r *RefID) SetTime(ts time.Time) error
- func (r RefID) String() string
- func (r RefID) Tag() byte
- func (r RefID) Time() time.Time
- func (r RefID) ToBase32String() string
- func (r RefID) ToBase64String() string
- func (r RefID) ToHexString() string
- func (r RefID) ToString() string
- func (r RefID) Type() Type
- func (r *RefID) UnmarshalBinary(data []byte) error
- func (r *RefID) UnmarshalJSON(b []byte) error
- func (r *RefID) UnmarshalText(b []byte) error
- func (r RefID) Value() (driver.Value, error)
- type Requirement
- type Tagger
- func (t Tagger) AnyMatcher() AnyMatcher
- func (t Tagger) HasCorrectTag(r RefID) bool
- func (t Tagger) HasTag(r RefID, tag byte) bool
- func (t Tagger) IsTagged(r RefID) bool
- func (t Tagger) New() (RefID, error)
- func (t Tagger) NewRandom() (RefID, error)
- func (t Tagger) Parse(s string) (RefID, error)
- func (t Tagger) ParseWithRequire(s string, reqs ...Requirement) (RefID, error)
- func (t Tagger) Tag() byte
- type Type
Constants ¶
This section is empty.
Variables ¶
var ( // Nil is the nil RefID, that has all 128 bits set to zero. Nil = RefID{} )
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 RefIDs that do not match the tag specified, will be considered not matching.
If tag is 0, will support matching any RefID (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 NullRefID ¶ added in v1.0.1
NullRefID can be used with the standard sql package to represent a RefID value that can be NULL in the database.
func (NullRefID) MarshalJSON ¶ added in v1.0.1
MarshalJSON marshals the NullRefID as null or the nested RefID
func (*NullRefID) Scan ¶ added in v1.0.1
Scan implements the sql.Scanner interface.
func (*NullRefID) UnmarshalJSON ¶ added in v1.0.1
UnmarshalJSON unmarshals a NullRefID
type RefID ¶ added in v1.0.1
type RefID [size]byte
A RefID 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 RefID 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 RefID.
If random bytes cannot be generated, it will return an error.
func NewRandom ¶ added in v1.0.2
NewRandom returns a new RandomPrefixed type RefID.
If random bytes cannot be generated, it will return an error.
func NewRandomTagged ¶ added in v1.0.2
NewRandomTagged returns a new RandomPrefixed type RefID tagged with tag.
If random bytes cannot be generated, it will return an error.
func NewTagged ¶
NewTagged returns a new TimePrefixed type RefID tagged with tag.
If random bytes cannot be generated, it will return an error.
func Parse ¶
Parse parses a textual RefID representation, and returns a RefID. 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) (RefID, error)
ParseWithRequire parses a textual RefID representation (same formats as Parse), while additionally requiring each reqs Requirement to pass, and returns a RefID.
Returns an error if RefID fails to parse or if any of the reqs Requirements fail.
Example:
ParseWithRequire("afd661f4f2tg2vr3dca92qp6k8", HasType(RandomPrefix))
func (RefID) Bytes ¶ added in v1.0.1
Bytes returns a slice of a copy of the current RefID underlying data.
func (RefID) Equal ¶ added in v1.0.1
Equal compares a RefID to another RefID to see if they have the same underlying bytes.
func (RefID) Format ¶ added in v1.0.1
Format implements the fmt.Formatter interface.
func (RefID) HasTag ¶ added in v1.0.1
IsTagged reports whether the RefID is tagged and if so, if it is tagged with tag.
func (RefID) MarshalBinary ¶ added in v1.0.1
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 (RefID) MarshalJSON ¶ added in v1.0.1
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 (RefID) MarshalText ¶ added in v1.0.1
MarshalText implements the encoding.TextMarshaler interface.
func (*RefID) Scan ¶ added in v1.0.1
Scan implements the sql.Scanner interface. A 16-byte slice will be handled by RefID.UnmarshalBinary, while a longer byte slice or a string will be handled by RefID.UnmarshalText.
func (*RefID) SetTime ¶ added in v1.0.1
SetTime sets the time component of a RefID to the time specified by ts.
func (RefID) String ¶ added in v1.0.1
String returns the native (base32 w/Crockford alphabet) textual represenation of a RefID
func (RefID) Tag ¶ added in v1.0.1
Tag returns the current tag of the RefID. If the RefID is untagged, it will retrun 0.
func (RefID) ToBase32String ¶ added in v1.0.2
ToBase32String is an alias of [String]
func (RefID) ToBase64String ¶ added in v1.0.1
String returns the base64 textual represenation of a RefID
func (RefID) ToHexString ¶ added in v1.0.1
String returns the base16/hex textual represenation of a RefID
func (*RefID) UnmarshalBinary ¶ added in v1.0.1
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It will return an error if the slice isn't of appropriate size.
func (*RefID) UnmarshalJSON ¶ added in v1.0.1
UnmarshalJson implements the json.Unmarshaler interface.
func (*RefID) UnmarshalText ¶ added in v1.0.1
UnmarshalText implements the encoding.TextUnmarshaler interface. It will return an error if the slice isn't of appropriate size.
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 RefID's of a specific tag.
func (Tagger) AnyMatcher ¶
func (t Tagger) AnyMatcher() AnyMatcher
AnyMather returns an AnyMatcher, which will match only against a RefID tagged with the same tag as the Tagger
func (Tagger) HasCorrectTag ¶
HasTag reports whether a RefID is tagged with the same tag as the Tagger
func (Tagger) IsTagged ¶
IsTagged reports wheater a RefID is tagged at all. Note: This only checks that the RefID is tagged, not that it is tagged with the same tag as Tagger. For that functionality use Tagger.HasCorrectTag.
func (Tagger) New ¶
New generates a new [TimePrefix] type RefID with tag set to the tag of the Tagger
func (Tagger) NewRandom ¶ added in v1.0.2
NewRandom generates a new [RandomPrefix] type RefID with tag set to the tag of the Tagger
func (Tagger) Parse ¶
Parse parses a RefID, 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) (RefID, error)
ParseWithRequire parses a textual RefID 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 RefID.
Returns an error if RefID 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))
