forked from src-d/proteus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
173 lines (142 loc) · 3.31 KB
/
models.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package example
import (
"context"
"fmt"
"time"
"gopkg.in/src-d/proteus.v1/example/categories"
)
//go:generate proteus -p gopkg.in/src-d/proteus.v1/example -f $GOPATH/src/gopkg.in/src-d/proteus.v1/example/protos
//proteus:generate
type Product struct {
Model
Name string
Price Prices
Tags Tags
CategoryID int64
PrimaryCategoryID int8
// Category will not be generated because we explicitly said so.
Category Category `proteus:"-"`
}
type Prices map[string]Price
type Price struct {
Currency string
Amount int64
}
//proteus:generate
type Category struct {
Model
Name string
Type Type
Color Color
Options categories.CategoryOptions
}
func (c *Category) String() string {
return c.Name
}
type Tags []string
// Type will be transformed into an enum.
//proteus:generate
type Type byte
const (
Public Type = iota
Private
Custom
)
func (t Type) String() string {
switch t {
case Public:
return "Public"
case Private:
return "Private"
case Custom:
return "Custom"
}
return "UnknownType"
}
// Color does not have proteus:generate in a comment, so all fields of
// type Color will be treated as just string, not as an enum.
type Color string
const (
Blue Color = "blue"
Red Color = "red"
Yellow Color = "yellow"
)
// Model is not marked for generation, so it won't be generated.
type Model struct {
ID int64
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt time.Time
}
// User will not be generated.
type User struct {
Model
Username string
Password string
Email string
}
type MyTime struct {
Time time.Time
Name string
}
//proteus:generate
type MyDuration struct {
Duration time.Duration
Name string
}
//proteus:generate
func RandomNumber(mean, std float64) float64 {
// Related documentation: https://xkcd.com/221/
return 4*std + mean // 4 was chosen using the XKCD RNG
}
//proteus:generate
func RandomCategory() categories.CategoryOptions {
return categories.CategoryOptions{
ShowPrices: RandomBool(),
CanBuy: RandomBool(),
}
}
//proteus:generate
func GetAlphaTime() MyTime {
return MyTime{Time: time.Unix(0, 0), Name: "alpha"}
}
//proteus:generate
func GetOmegaTime() (*MyTime, error) {
t, err := time.Parse("Jan 2, 2006 at 3:04pm", "Dec 12, 2012 at 10:30am")
if err != nil {
return nil, err
}
return &MyTime{Time: t, Name: "omega"}, nil
}
//proteus:generate
func GetDurationForLength(meters int64) *MyDuration {
return &MyDuration{
Duration: time.Second * time.Duration(meters/299792458),
Name: fmt.Sprintf("The light takes this duration to travel %dm", meters),
}
}
//proteus:generate
func GetDurationForLengthCtx(ctx context.Context, meters int64) (*MyDuration, error) {
select {
case <-ctx.Done(): // timeout
return nil, ctx.Err()
case <-time.After(time.Second): // it's a complex computation that takes time, right?
}
return &MyDuration{
Duration: time.Second * time.Duration(meters/299792458),
Name: fmt.Sprintf("The light takes this duration to travel %dm", meters),
}, nil
}
//proteus:generate
func GetPhone() *Product {
return &Product{
Name: "MiPhone",
Price: map[string]Price{"EUR": Price{"EUR", 12300}},
Tags: Tags{"cool", "mi", "phone"},
CategoryID: 1,
Category: Category{},
}
}
func RandomBool() bool {
return true // Truly random. Selected by flipping a coin... once.
}