-
Notifications
You must be signed in to change notification settings - Fork 31
/
database.go
53 lines (49 loc) · 948 Bytes
/
database.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 data
// Model structs
type User struct {
Id string `json:"id"`
Name string `json:"name"`
Widgets []*Widget `json:"widgets"`
}
type Widget struct {
Id string `json:"id"`
Name string `json:"name"`
}
// Mock data
var viewer = &User{
Id: "1",
Name: "Anonymous",
}
var widgets = []*Widget{
&Widget{"0", "What's-it"},
&Widget{"1", "Who's-it"},
&Widget{"2", "How's-it"},
}
// Data accessors
func GetUser(id string) *User {
if id == viewer.Id {
return viewer
}
return nil
}
func GetViewer() *User {
return viewer
}
func GetWidget(id string) *Widget {
for _, widget := range widgets {
if widget.Id == id {
return widget
}
}
return nil
}
func GetWidgets() []*Widget {
return widgets
}
func WidgetsToInterfaceSlice(widgets ...*Widget) []interface{} {
var interfaceSlice []interface{} = make([]interface{}, len(widgets))
for i, d := range widgets {
interfaceSlice[i] = d
}
return interfaceSlice
}