forked from jetify-com/typeid-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
152 lines (128 loc) · 3.95 KB
/
examples_test.go
File metadata and controls
152 lines (128 loc) · 3.95 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package typeid_test
import (
"database/sql"
"encoding/json"
"fmt"
"go.jetify.com/typeid/v2"
)
// ExampleGenerate demonstrates creating a new TypeID for a user entity
func ExampleGenerate() {
userID, err := typeid.Generate("user")
if err != nil {
panic(err)
}
// Use the generated ID for a new user
fmt.Printf("Created new user with ID: %s\n", userID)
// Output format: Created new user with ID: user_[26-char-suffix]
}
// ExampleParse demonstrates parsing a TypeID from a string, such as from a URL parameter
func ExampleParse() {
// Parse a TypeID received from a client request
orderIDStr := "order_00041061050r3gg28a1c60t3gf"
orderID, err := typeid.Parse(orderIDStr)
if err != nil {
fmt.Printf("Invalid order ID: %v\n", err)
return
}
fmt.Printf("Processing order: %s\n", orderID.String())
fmt.Printf("Order type: %s\n", orderID.Prefix())
// Output:
// Processing order: order_00041061050r3gg28a1c60t3gf
// Order type: order
}
// ExampleTypeID_MarshalText demonstrates using TypeID in JSON APIs
func ExampleTypeID_MarshalText() {
// Define a struct for API responses
type Product struct {
ID typeid.TypeID `json:"id"`
Name string `json:"name"`
Price float64 `json:"price"`
}
// Create a product with TypeID
product := Product{
ID: typeid.MustParse("product_00041061050r3gg28a1c60t3gf"),
Name: "Widget",
Price: 29.99,
}
// Marshal to JSON for API response
jsonData, err := json.Marshal(product)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", jsonData)
// Output:
// {"id":"product_00041061050r3gg28a1c60t3gf","name":"Widget","price":29.99}
}
// ExampleTypeID_UnmarshalText demonstrates parsing TypeID from JSON requests
func ExampleTypeID_UnmarshalText() {
// JSON payload from client request
jsonPayload := `{
"user_id": "user_00041061050r3gg28a1c60t3gf",
"product_id": "product_00041061050r3gg28a1c60t3gg",
"quantity": 2
}`
// Define request struct
type OrderRequest struct {
UserID typeid.TypeID `json:"user_id"`
ProductID typeid.TypeID `json:"product_id"`
Quantity int `json:"quantity"`
}
// Parse the request
var req OrderRequest
err := json.Unmarshal([]byte(jsonPayload), &req)
if err != nil {
panic(err)
}
fmt.Printf("Order from user %s for product %s\n", req.UserID.Prefix(), req.ProductID.Prefix())
// Output:
// Order from user user for product product
}
// ExampleFromUUID demonstrates migrating from UUIDs to TypeIDs
func ExampleFromUUID() {
// Existing UUID from legacy system
existingUUID := "550e8400-e29b-41d4-a716-446655440000"
// Convert to TypeID with appropriate prefix
customerID, err := typeid.FromUUID("customer", existingUUID)
if err != nil {
panic(err)
}
fmt.Printf("Migrated customer ID: %s\n", customerID.String())
// Output:
// Migrated customer ID: customer_2n1t201rmv87aae5j4csam8000
}
// ExampleTypeID_Scan demonstrates reading TypeIDs from a database
func ExampleTypeID_Scan() {
// Simulate database row scan
var userID typeid.TypeID
// In real code, this would come from sql.Row.Scan()
dbValue := "user_00041061050r3gg28a1c60t3gf"
err := userID.Scan(dbValue)
if err != nil {
panic(err)
}
fmt.Printf("Retrieved user %s from database\n", userID.String())
// Output:
// Retrieved user user_00041061050r3gg28a1c60t3gf from database
}
// Example_nullableColumns demonstrates using sql.Null[TypeID] for nullable database columns.
// This is the recommended approach for handling nullable TypeID columns in Go applications.
func Example_nullableColumns() {
var managerID sql.Null[typeid.TypeID]
// Scan NULL value from database
err := managerID.Scan(nil)
if err != nil {
panic(err)
}
fmt.Printf("Is valid: %v\n", managerID.Valid)
// Scan actual TypeID value
err = managerID.Scan("user_00041061050r3gg28a1c60t3gf")
if err != nil {
panic(err)
}
fmt.Printf("Is valid: %v\n", managerID.Valid)
fmt.Printf("Manager: %s\n", managerID.V.String())
// Output:
// Is valid: false
// Is valid: true
// Manager: user_00041061050r3gg28a1c60t3gf
}