-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2
314 lines (277 loc) · 8.54 KB
/
2
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2015 Ian Dawes. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"strconv"
"strings"
)
/*
attribute definition:
<id> <name> <type> [<persistenceType>]
*/
func parseAttribute(t *typedef, r *reader) (attrDef, error) {
fields, err := r.read()
if len(fields) < 3 || len(fields) > 4 {
return nil, fmt.Errorf("invalid attribute specification, line %d of file %s", r.line, r.f.Name())
}
id, err := strconv.Atoi(fields[0])
if err != nil {
return nil, fmt.Errorf("expecting an integer attribute id, found \"%s\", line %d of file %s", fields[0], r.line, r.f.Name())
}
name := fields[1]
p := persistenceClassPersistent
if len(fields) > 3 {
switch fields[3] {
case "persistent":
p = persistenceClassPersistent
case "non-persistent":
p = persistenceClassNonPersistent
case "ephemeral":
p = persistenceClassEphemeral
default:
return nil, fmt.Errorf("unrecognized persistence type \"%s\", line %d of file %s", fields[3], r.line, r.f.Name())
}
}
return newAttrDef(&baseAttrDef{parentType: t, attributeID: id, name: name, attrType: fields[2], persistence: p, srcline: r.line, srcfile: r.f.Name()})
}
func newAttrDef(b *baseAttrDef) (attrDef, error) {
c, err := getClass(b.attrType)
if err != nil {
return nil, fmt.Errorf("unknown attribute class %s, line %d of file %s", b.attrType, b.srcline, b.srcfile)
}
switch c {
case attrClassBuiltin:
return b, nil
case attrClassTime:
return &timeAttrDef{baseAttrDef: *b}, nil
case attrClassMap:
return newMapAttrDef(b)
case attrClassSlice:
return newSliceAttrDef(b)
case attrClassStruct:
return newStructAttrDef(b)
}
return nil, fmt.Errorf("Unknown attribute class %s, line %d of file %s", c, b.srcline, b.srcfile)
}
func getClass(n string) (attrClass, error) {
switch n {
case "byte", "uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64", "float32", "float64", "string":
return attrClassBuiltin, nil
case "time.Time":
return attrClassTime, nil
}
switch {
case strings.HasPrefix(n, "[]"):
return attrClassSlice, nil
case strings.HasPrefix(n, "map["):
return attrClassMap, nil
default:
return attrClassStruct, nil
}
}
//go:generate stringer -type=attrClass
type attrClass int
const (
attrClassBuiltin attrClass = iota
attrClassTime
attrClassSlice
attrClassMap
attrClassStruct
)
//go:generate stringer -type=persistenceClass
type persistenceClass int
const (
persistenceClassPersistent persistenceClass = iota // serialized to disk and wire
persistenceClassNonPersistent // serialized to wire
persistenceClassEphemeral // computed or temporary storage - not serialized
)
type attrDef interface {
AttributeID() int
Srcline() int
Srcfile() string
Name() string
Type() string
GenerateEquals(w *writer, levelID string)
GenerateDiff(w *writer, levelID string)
}
type baseAttrDef struct {
parentType *typedef
attributeID int
name string
attrType string
persistence persistenceClass
srcline int
srcfile string
}
func (a *baseAttrDef) AttributeID() int {
return a.attributeID
}
func (a *baseAttrDef) Srcline() int {
return a.srcline
}
func (a *baseAttrDef) Srcfile() string {
return a.srcfile
}
func (a *baseAttrDef) Name() string {
return a.name
}
func (a *baseAttrDef) Type() string {
return a.attrType
}
func (a *baseAttrDef) GenerateEquals(w *writer, levelID string) {
if levelID == "" {
w.printf(" {\n")
w.printf(" va, vb := o1.%[1]s, o2.%[1]s\n", a.name)
}
w.printf(" if va%[1]s != vb%[1]s {\n return false\n }\n", a.name)
if levelID == "" {
w.printf(" }\n")
}
}
const baseAttrDiffTemplate = ` if va%[1]s != vb%[1]s {
d.Add(New%[2]sChg(%[3]s%[4]s, %[5]t, vb%[1]s, va%[1]s))
}
`
func (a *baseAttrDef) GenerateDiff(w *writer, levelID string) {
if levelID == "" {
w.printf(" {\n")
w.printf(" va, vb := o1.%[1]s, o2.%[1]s\n", a.name)
}
w.printf(baseAttrDiffTemplate, levelID, a.Type(), a.parentType.name, a.name, a.persistence == persistenceClassPersistent)
if levelID == "" {
w.printf(" }\n")
}
}
/************************************************************************/
/************************** Time Attribute ******************************/
type timeAttrDef struct {
baseAttrDef
}
func (a *timeAttrDef) Type() string {
return "Time"
}
func (a *timeAttrDef) GenerateEquals(w *writer, levelID string) {
if levelID == "" {
w.printf(" {\n")
w.printf(" va, vb := o1.%[1]s, o2.%[1]s\n", a.name)
}
w.printf(" if va%[1]s.Equal(vb%[1]s) {\n return false\n }\n", a.name)
if levelID == "" {
w.printf(" }\n")
}
}
const TimeDiffTemplate = ` if va%[1]s.Equal(vb%[1]s) {
d.Add(NewTimeChg(%[2]s%[3]s, %[4]t, vb%[1]s, va%[1]s))
}
`
func (a *timeAttrDef) GenerateDiff(w *writer, levelID string) {
if levelID == "" {
w.printf(" {\n")
w.printf(" va, vb := o1.%[1]s, o2.%[1]s\n", a.name)
}
w.printf(TimeDiffTemplate, a.parentType.name, a.name, strings.Title(a.attrType), a.persistence == persistenceClassPersistent)
if levelID == "" {
w.printf(" }\n")
}
}
/************************************************************************/
/**************************** Slice Attribute ***************************/
type sliceAttrDef struct {
baseAttrDef
valType string
valAttr attrDef
}
func newSliceAttrDef(b *baseAttrDef) (*sliceAttrDef, error) {
valType := b.attrType[2:]
valAttr, err := newAttrDef(&baseAttrDef{parentType: b.parentType, attrType: valType})
if err != nil {
return nil, fmt.Errorf("invalid slice attribute specification %s, line %d of file %s", b.attrType, b.srcline, b.srcfile)
}
return &sliceAttrDef{baseAttrDef: *b, valType: valType, valAttr: valAttr}, nil
}
// parameters:
// 1: current level id
// 2: next level id
const sliceAttrEquals = ` if len(va%[1]s) != len(vb%[1]s) {
return false
}
for idx%[1]s, va%[2]s := range va%[1]s {
vb%[2]s := vb%[1]s[idx%[1]s]
`
func (a *sliceAttrDef) GenerateEquals(w *writer, levelID string) {
nextLevelID := fmt.Sprintf("%s1", levelID)
if levelID == "" {
w.printf(" {\n")
w.printf(" va, vb := o1.%[1]s, o2.%[1]s\n", a.name)
}
w.printf(sliceAttrEquals, levelID, nextLevelID)
a.valAttr.GenerateEquals(w, nextLevelID)
w.printf(" }\n")
if levelID == "" {
w.printf(" }\n")
}
}
// parameters:
// 1: current level id
// 2: next level id
const sliceModHeader = ` for idx%[1]s, va%[2]s := range va%[1]s {
if idx%[1]s < len(vb%[1]s) {
vb%[2]s := vb%[1]s[idx%[1]s]
d%[2]s := metago.Diff{}
`
const sliceModFooter = ` d%[2]s.Changes = append(d%[1]s.Changes, metago.NewSliceChg(%[3]s, idx%[1]s, metago.ChangeTypeModify, &d%[1]s))
}
}
`
func (a *sliceAttrDef) GenerateDiff(w *writer, levelID string) {
nextLevelID := fmt.Sprintf("%s1", levelID)
if levelID == "" {
w.printf(" {\n")
w.printf(" va, vb := o1.%[1]s, o2.%[1]s\n", a.name)
}
w.printf(sliceModHeader, levelID, nextLevelID)
a.valAttr.GenerateDiff(w, nextLevelID)
w.printf(sliceModFooter, levelID, nextLevelID, fmt.Sprintf("%s%sAID", a.parentType.name, a.name))
if levelID == "" {
w.printf(" }\n")
}
}
/************************************************************************/
/**************************** Map Attribute *****************************/
type mapAttrDef struct {
baseAttrDef
keyType string
keyAttr attrDef
valType string
valAttr attrDef
}
func newMapAttrDef(b *baseAttrDef) (*mapAttrDef, error) {
// map[int]string
i := strings.Index(b.attrType, "[")
j := strings.Index(b.attrType, "]")
if i == -1 || j == -1 {
return nil, fmt.Errorf("invalid map attribute specification %s, line %d of file %s", b.attrType, b.srcline, b.srcfile)
}
keyType := b.attrType[i+1 : j]
keyAttr, err := newAttrDef(&baseAttrDef{attrType: keyType})
if err != nil {
return nil, fmt.Errorf("invalid map attribute specification %s, line %d of file %s", b.attrType, b.srcline, b.srcfile)
}
valType := b.attrType[j+1:]
valAttr, err := newAttrDef(&baseAttrDef{attrType: valType})
if err != nil {
return nil, fmt.Errorf("invalid map attribute specification %s, line %d of file %s", b.attrType, b.srcline, b.srcfile)
}
return &mapAttrDef{baseAttrDef: *b, keyType: keyType, keyAttr: keyAttr, valType: valType, valAttr: valAttr}, nil
}
/************************************************************************/
/**************************** Diff Obj Attribute ************************/
type structAttrDef struct {
baseAttrDef
packageName string
}
func newStructAttrDef(b *baseAttrDef) (*structAttrDef, error) {
return &structAttrDef{baseAttrDef: *b}, nil
}