-
Notifications
You must be signed in to change notification settings - Fork 7
/
prefix.go
38 lines (32 loc) · 902 Bytes
/
prefix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package typeid
// PrefixType is the interface that defines the type if a type id.
// Implement your own version of this interface if you want to define a custom
// type:
// type UserPrefix struct {}
// func (UserPrefix) Prefix() string { return "user" }
type PrefixType interface {
Prefix() string
}
// Any is a special prefix that can be used to represent TypeIDs that allow for
// any valid prefix.
type AnyPrefix struct{}
func (a AnyPrefix) Prefix() string {
return "*" // Any is treated specially, so in practice this string will never be used.
}
// AnyID represents TypeIDs that accept any valid prefix.
type AnyID struct {
TypeID[AnyPrefix]
}
func isAnyPrefix[P PrefixType]() bool {
var prefixType P
switch any(prefixType).(type) {
case AnyPrefix:
return true
default:
return false
}
}
func defaultPrefix[P PrefixType]() string {
var prefixType P
return prefixType.Prefix()
}