-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (125 loc) · 4.47 KB
/
main.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
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
//go:generate entgen
package main
import (
"fmt"
"github.com/rsms/ent"
"github.com/rsms/ent/redis"
"github.com/rsms/go-uuid"
)
type Account struct {
ent.EntBase `account`
name string `ent:",index"`
email string `ent:",unique"`
emailVerified bool `ent:"email_verified"`
deleted bool
passwordHash string `ent:"pwhash" json:"-"` // omit from json (never leak)
}
type Building int32
const (
BuildingF1 = iota
BuildingF2
BuildingF3
BuildingGymnasium = 100
BuildingObservatory
)
type Department struct {
ent.EntBase `dept`
name string
building Building `ent:",index"`
}
func main() {
r := &redis.Redis{}
panicOnError(r.Open("127.0.0.1:6379", "", 1))
estore := redis.NewEntStorage(r)
// create
fmt.Printf("\n-- create --\n")
a1 := &Account{}
a1.SetName("bob")
a1.SetEmail(uuid.MustGen().String() + "@bob.com")
panicOnError(a1.Create(estore))
fmt.Printf("created account #%d: %+v\n", a1.Id(), a1)
// create a second account with the same email
fmt.Printf("\n-- create with duplicate email --\n")
a2 := &Account{}
a2.SetEmail(a1.Email())
fmt.Printf("create w same email [should fail] => error: %v\n", a2.Create(estore))
// load
fmt.Printf("\n-- load --\n")
a3, err := LoadAccountById(estore, a1.Id())
panicOnError(err)
fmt.Printf("loaded account #%d: %+v\n", a1.Id(), a3)
// save
fmt.Printf("\n-- save --\n")
a1.SetName("Bobby")
a1.SetEmail(uuid.MustGen().String() + "@bob.com")
panicOnError(a1.Save())
fmt.Printf("saved account #%d\n", a1.Id())
// save outdated should fail
fmt.Printf("\n-- save outdated --\n")
a3.SetName("Bo")
fmt.Printf("outdated.Save() [should fail] => error: %v\n", a3.Save())
// load by unique index
fmt.Printf("\n-- load by unique index 'email' --\n")
a4, err := LoadAccountByEmail(estore, a1.Email())
panicOnError(err)
fmt.Printf("loaded account by email %q: %+v\n", a1.Email(), a4)
// create another account with the same name
fmt.Printf("\n-- create --\n")
a5 := &Account{}
a5.SetName(a1.Name())
panicOnError(a5.Create(estore))
// find by non-unique index
fmt.Printf("\n-- find by non-unique index 'name' --\n")
ids, err := FindAccountByName(estore, a1.Name(), 10)
panicOnError(err)
fmt.Printf("loaded account IDs with name %q: %+v\n", a1.Name(), ids)
// delete an account
fmt.Printf("\n-- delete and then load (now non-existing) ent --\n")
panicOnError(a1.PermanentlyDelete())
_, err = LoadAccountById(estore, a1.Id())
fmt.Printf("LoadAccountById(%d) [should fail] => error: %v\n", a1.Id(), err)
// -----
// iteration and loading by limit & ent.Reverse
fmt.Printf("\n-- delete all department ents & recreate them --\n")
// remove any and all preexisting departments from a previous run
panicOnError(ent.DeleteAllEntsOfType(estore, &Department{}))
// create some departments
panicOnError((&Department{name: "Astronomy", building: BuildingF2}).Create(estore))
panicOnError((&Department{name: "Computer Science", building: BuildingF2}).Create(estore))
panicOnError((&Department{name: "Mathematical Sciences", building: BuildingF2}).Create(estore))
panicOnError((&Department{name: "Art History", building: BuildingF2}).Create(estore))
panicOnError((&Department{name: "Physics", building: BuildingF2}).Create(estore))
panicOnError((&Department{name: "Psychology", building: BuildingF2}).Create(estore))
panicOnError((&Department{name: "Religious Mythology", building: BuildingF3}).Create(estore))
panicOnError((&Department{name: "Bookstore", building: BuildingF3}).Create(estore))
// list all departments using an iterator. Iteration order is undefined; varies by storage.
fmt.Printf("\n-- list all departments --\n")
var d Department
for it := d.Iterator(estore); it.Next(&d); {
fmt.Printf("#%d %s\n", d.Id(), d.Name())
}
departments, err := LoadDepartmentByBuilding(estore, BuildingF2, 0)
panicOnError(err)
fmt.Printf("\nLoadDepartmentByBuilding(BuildingF2, limit=0, flags=0) =>\n")
for _, d := range departments {
fmt.Printf(" %v\n", d.Name())
}
departments, _ = LoadDepartmentByBuilding(estore, BuildingF2, 3)
fmt.Printf("\nLoadDepartmentByBuilding(BuildingF2, limit=3, flags=0) =>\n")
for _, d := range departments {
fmt.Printf(" %v\n", d.Name())
}
departments, _ = LoadDepartmentByBuilding(estore, BuildingF2, 3, ent.Reverse)
fmt.Printf("\nLoadDepartmentByBuilding(BuildingF2, limit=3, flags=ent.Reverse) =>\n")
for _, d := range departments {
fmt.Printf(" %v\n", d.Name())
}
// --- end
estore.Close()
r.Close()
}
func panicOnError(err error) {
if err != nil {
panic(err)
}
}