Documentation ¶
Index ¶
- Variables
- func CollectionForModel(model interface{}) (string, error)
- func CollectionForModelField(model interface{}, field string) (string, error)
- func RegisterValidation(v *validator.Validate) error
- func SemanticIDTypeFunc(field reflect.Value) interface{}
- func SemanticIDValidation(fl validator.FieldLevel) bool
- func ToStrings(s []SemanticID) []string
- type BSONSemanticIDCodec
- type BSONSemanticIDPointerCodec
- type IDProvider
- type SemanticID
- func FromString(s string) (SemanticID, error)
- func FromStrings(s []string) ([]SemanticID, error)
- func Must(sID SemanticID, err error) SemanticID
- func New(namespace, collection string) (SemanticID, error)
- func NewDefault() (SemanticID, error)
- func NewForModel(model interface{}) (SemanticID, error)
- func NewWithCollection(collection string) (SemanticID, error)
- func NewWithNamespace(namespace string) (SemanticID, error)
- type SemanticIDBuilder
- func (b *SemanticIDBuilder) Build() (SemanticID, error)
- func (b *SemanticIDBuilder) FromString(s string) *SemanticIDBuilder
- func (b *SemanticIDBuilder) NoValidate() *SemanticIDBuilder
- func (b *SemanticIDBuilder) WithCollection(collection string) *SemanticIDBuilder
- func (b *SemanticIDBuilder) WithIDProvider(idp IDProvider) *SemanticIDBuilder
- func (b *SemanticIDBuilder) WithNamespace(namespace string) *SemanticIDBuilder
- type SemanticIDError
- type ULIDProvider
- type UUIDProvider
Constants ¶
This section is empty.
Variables ¶
var ( ErrEmpty = &SemanticIDError{errEmpty, ""} ErrIDProvider = &SemanticIDError{errIDProviderError, ""} ErrInvalid = &SemanticIDError{errInvalidSID, ""} ErrInvalidIDPart = &SemanticIDError{errInvalidID, ""} ErrPartContainsSeparator = &SemanticIDError{errPartContainsSeparator, ""} )
var DefaultCollection = "collection"
DefaultCollection is the collection that will be used if no collection is specified when creating a SemanticID.
var DefaultNamespace = "namespace"
DefaultNamespace is the namespace that will be used if no namespace is specified when creating a SemanticID.
var Separator = "."
Separator that will be used for all SemanticIDs. You should set this once and never change it for your application - Once you change it, SemanticIDs created before that point can't be parsed anymore. By default, this is set to `.` since this makes SemanticIDs entirely URL-safe.
Functions ¶
func CollectionForModel ¶ added in v1.0.0
CollectionForModel returns the collection defined in the `sid` tag on the `ID` field of the passed struct.
func CollectionForModelField ¶ added in v1.0.0
CollectionForModelField returns the collection defined in the `sid` tag on the given field.
func RegisterValidation ¶ added in v1.1.0
func RegisterValidation(v *validator.Validate) error
func SemanticIDTypeFunc ¶ added in v1.1.0
func SemanticIDValidation ¶ added in v1.1.0
func SemanticIDValidation(fl validator.FieldLevel) bool
func ToStrings ¶ added in v1.3.0
func ToStrings(s []SemanticID) []string
ToStrings converts a list of semanticids to their string representation.
Types ¶
type BSONSemanticIDCodec ¶
type BSONSemanticIDCodec struct{}
BSONSemanticIDCodec is a mongodb ValueCodec for encoding and decoding SemanticIDs to and from BSON.
func (*BSONSemanticIDCodec) DecodeValue ¶
func (*BSONSemanticIDCodec) DecodeValue( dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value, ) error
DecodeValue implements the ValueDecoder interface.
func (*BSONSemanticIDCodec) EncodeValue ¶
func (*BSONSemanticIDCodec) EncodeValue( ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value, ) error
EncodeValue implements the ValueEncoder interface.
type BSONSemanticIDPointerCodec ¶
type BSONSemanticIDPointerCodec struct{}
BSONSemanticIDCodec is a mongodb ValueCodec for encoding and decoding SemanticIDs to and from BSON.
func (*BSONSemanticIDPointerCodec) DecodeValue ¶
func (*BSONSemanticIDPointerCodec) DecodeValue( dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value, ) error
DecodeValue implements the ValueDecoder interface.
func (*BSONSemanticIDPointerCodec) EncodeValue ¶
func (*BSONSemanticIDPointerCodec) EncodeValue( ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value, ) error
EncodeValue implements the ValueEncoder interface.
type IDProvider ¶ added in v1.0.0
type IDProvider interface { // Generate generates a random ID Generate() (string, error) // Validate validates an existing ID Validate(id string) error }
IDProvider represents a type that can generate and validate the ID part of semanticids. By default, ULIDs will be used for this, since they offer lots of benefits over UUIDs, but a UUID provider is provided and custom providers can easily be used by either setting the default ID Provider or using the builder.
var DefaultIDProvider IDProvider = NewULIDProvider()
DefaultIDProvider determines the provider that will be used to generate and validate IDs. You can either set this or use the builder to select the provider on an individual basis.
type SemanticID ¶
A SemanticID is a unique identifier for an entity that consists of a namespace, a collection and an ID.
func FromString ¶
func FromString(s string) (SemanticID, error)
FromString attempts to parse a given string into a SemanticID.
func FromStrings ¶ added in v1.2.0
func FromStrings(s []string) ([]SemanticID, error)
FromStrings attempts to parse a given list of strings into a list of SemanticIDs. An error will be returned for the first conversion that errors, which means that a list that returns an error is not guaranteed to only contain that one error.
func Must ¶
func Must(sID SemanticID, err error) SemanticID
Must is a convenience function that converts errors into panics on functions that create or parse a SemanticID.
func New ¶
func New(namespace, collection string) (SemanticID, error)
New creates a unique SemanticID with the given namespace, collection and the global separator (`.` by default).
func NewDefault ¶
func NewDefault() (SemanticID, error)
NewDefault creates a unique SemanticID with the default namespace and collection.
func NewForModel ¶ added in v1.0.0
func NewForModel(model interface{}) (SemanticID, error)
NewForModel creates a unique SemanticID using the collection defined in the `sid` tag in the given model.
func NewWithCollection ¶
func NewWithCollection(collection string) (SemanticID, error)
NewWithCollection creates a unique SemanticID with the given collection and the default namespace.
func NewWithNamespace ¶
func NewWithNamespace(namespace string) (SemanticID, error)
NewWithNamespace creates a unique SemanticID with the given namespace and the default collection.
func (SemanticID) Is ¶ added in v1.2.0
func (sID SemanticID) Is(identity string) bool
Is checks the identity of a SemanticID, given by its Namespace and Collection. It expects a dot-separated Namespace and Collection combination, such that `semanticid.New("auth", "users").Is("auth.users") == true`.
func (SemanticID) IsNil ¶
func (sID SemanticID) IsNil() bool
IsNil checks whether or not the SemanticID has any of its part set to a non-null string.
func (SemanticID) MarshalJSON ¶
func (sid SemanticID) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface for SemanticID
func (SemanticID) String ¶
func (sID SemanticID) String() string
String outputs a string representation of the SemanticID
func (*SemanticID) UnmarshalJSON ¶
func (sid *SemanticID) UnmarshalJSON(b []byte) error
UnmarshalJSON implements the json.Unmarshaler interface for SemanticID
type SemanticIDBuilder ¶ added in v1.0.0
type SemanticIDBuilder struct {
// contains filtered or unexported fields
}
func Builder ¶ added in v1.0.0
func Builder() *SemanticIDBuilder
func (*SemanticIDBuilder) Build ¶ added in v1.0.0
func (b *SemanticIDBuilder) Build() (SemanticID, error)
func (*SemanticIDBuilder) FromString ¶ added in v1.0.0
func (b *SemanticIDBuilder) FromString(s string) *SemanticIDBuilder
func (*SemanticIDBuilder) NoValidate ¶ added in v1.0.0
func (b *SemanticIDBuilder) NoValidate() *SemanticIDBuilder
func (*SemanticIDBuilder) WithCollection ¶ added in v1.0.0
func (b *SemanticIDBuilder) WithCollection(collection string) *SemanticIDBuilder
func (*SemanticIDBuilder) WithIDProvider ¶ added in v1.0.0
func (b *SemanticIDBuilder) WithIDProvider(idp IDProvider) *SemanticIDBuilder
func (*SemanticIDBuilder) WithNamespace ¶ added in v1.0.0
func (b *SemanticIDBuilder) WithNamespace(namespace string) *SemanticIDBuilder
type SemanticIDError ¶ added in v1.1.1
type SemanticIDError struct {
// contains filtered or unexported fields
}
func (*SemanticIDError) Error ¶ added in v1.1.1
func (err *SemanticIDError) Error() string
func (*SemanticIDError) Is ¶ added in v1.1.1
func (err *SemanticIDError) Is(target error) bool
type ULIDProvider ¶ added in v1.0.0
type ULIDProvider struct{}
func NewULIDProvider ¶ added in v1.0.0
func NewULIDProvider() *ULIDProvider
func (*ULIDProvider) Generate ¶ added in v1.0.0
func (up *ULIDProvider) Generate() (string, error)
func (*ULIDProvider) Validate ¶ added in v1.0.0
func (up *ULIDProvider) Validate(id string) error
type UUIDProvider ¶ added in v1.0.0
type UUIDProvider struct{}
func NewUUIDProvider ¶ added in v1.0.0
func NewUUIDProvider() *UUIDProvider
func (*UUIDProvider) Generate ¶ added in v1.0.0
func (up *UUIDProvider) Generate() (string, error)
func (*UUIDProvider) Validate ¶ added in v1.0.0
func (up *UUIDProvider) Validate(id string) error