-
Notifications
You must be signed in to change notification settings - Fork 7
/
subtype_example_test.go
53 lines (42 loc) · 1.37 KB
/
subtype_example_test.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package typeid_test
import (
"fmt"
"go.jetify.com/typeid"
)
// To create a new id type, first implement a custom PrefixType and ensure the
// Prefix() method returns the correct prefix
type UserPrefix struct{}
func (UserPrefix) Prefix() string {
return "user"
}
// And then define your custom id type by embedding TypeID:
type UserID struct {
typeid.TypeID[UserPrefix]
}
// Now do the same for AccountIDs
type AccountPrefix struct{}
func (AccountPrefix) Prefix() string {
return "account"
}
type AccountID struct {
typeid.TypeID[AccountPrefix]
}
func Example() {
// To create new IDs call typeid.New and pass your custom id type as the
// generic argument:
userID, _ := typeid.New[UserID]()
accountID, _ := typeid.New[AccountID]()
// Other than that, your custom types should have the same methods as a
// regular TypeID.
// For example, we can check that each ID has the correct type prefix:
fmt.Printf("User ID prefix: %s\n", userID.Prefix())
fmt.Printf("Account ID prefix: %s\n", accountID.Prefix())
// Despite both of them being TypeIDs, you now get compile-time safety because
// the compiler considers their go types to be different:
// (typeid_test.UserID vs typeid_test.AccountID vs typeid.TypeID)
fmt.Printf("%T != %T\n", userID, accountID)
// Output:
// User ID prefix: user
// Account ID prefix: account
// typeid_test.UserID != typeid_test.AccountID
}