forked from 99designs/gqlgen
/
model.go
71 lines (59 loc) · 1.17 KB
/
model.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
package models
import (
"encoding/base64"
"fmt"
"time"
)
type CharacterFields struct {
ID string
Name string
FriendIds []string
AppearsIn []Episode
}
type Human struct {
CharacterFields
StarshipIds []string
HeightMeters float64
Mass float64
}
func (h *Human) Height(unit LengthUnit) float64 {
switch unit {
case "METER", "":
return h.HeightMeters
case "FOOT":
return h.HeightMeters * 3.28084
default:
panic("invalid unit")
}
}
func (Human) IsCharacter() {}
func (Human) IsSearchResult() {}
type Review struct {
Stars int
Commentary *string
Time time.Time
}
type Droid struct {
CharacterFields
PrimaryFunction string
}
func (Droid) IsCharacter() {}
func (Droid) IsSearchResult() {}
type FriendsConnection struct {
Ids []string
From int
To int
}
func (f *FriendsConnection) TotalCount() int {
return len(f.Ids)
}
func (f *FriendsConnection) PageInfo() PageInfo {
return PageInfo{
StartCursor: EncodeCursor(f.From),
EndCursor: EncodeCursor(f.To - 1),
HasNextPage: f.To < len(f.Ids),
}
}
func EncodeCursor(i int) string {
return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("cursor%d", i+1)))
}