-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
index.go
133 lines (112 loc) · 3.63 KB
/
index.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
package codegen
import (
"fmt"
"strings"
"github.com/iancoleman/strcase"
"google.golang.org/protobuf/reflect/protoreflect"
)
func (t tableGen) genIndexKeys() {
// interface that all keys must adhere to
t.P("type ", t.indexKeyInterfaceName(), " interface {")
t.P("id() uint32")
t.P("values() []interface{}")
t.P(t.param(t.indexKeyInterfaceName()), "()")
t.P("}")
t.P()
// start with primary key..
t.P("// primary key starting index..")
t.genIndex(t.table.PrimaryKey.Fields, t.ormTable.ID())
for _, idx := range t.table.Index {
t.genIndex(idx.Fields, idx.Id)
}
}
func (t tableGen) genIterator() {
t.P("type ", t.iteratorName(), " struct {")
t.P(tablePkg.Ident("Iterator"))
t.P("}")
t.P()
t.genValueFunc()
t.P()
}
func (t tableGen) genValueFunc() {
varName := t.param(t.msg.GoIdent.GoName)
t.P("func (i ", t.iteratorName(), ") Value() (*", t.QualifiedGoIdent(t.msg.GoIdent), ", error) {")
t.P("var ", varName, " ", t.QualifiedGoIdent(t.msg.GoIdent))
t.P("err := i.UnmarshalMessage(&", varName, ")")
t.P("return &", varName, ", err")
t.P("}")
}
func (t tableGen) genIndexMethods(idxKeyName string) {
receiverFunc := fmt.Sprintf("func (x %s) ", idxKeyName)
t.P(receiverFunc, "id() uint32 { return ", t.table.Id, " /* primary key */ }")
t.P(receiverFunc, "values() []interface{} { return x.vs }")
t.P(receiverFunc, t.param(t.indexKeyInterfaceName()), "() {}")
t.P()
}
func (t tableGen) genIndexInterfaceGuard(idxKeyName string) {
t.P("var _ ", t.indexKeyInterfaceName(), " = ", idxKeyName, "{}")
t.P()
}
func (t tableGen) indexKeyInterfaceName() string {
return t.msg.GoIdent.GoName + "IndexKey"
}
func (t tableGen) genIndexKey(idxKeyName string) {
t.P("type ", idxKeyName, " struct {")
t.P("vs []interface{}")
t.P("}")
t.P()
}
func (t tableGen) indexKeyParts(names []protoreflect.Name) string {
cnames := make([]string, len(names))
for i, name := range names {
cnames[i] = strcase.ToCamel(string(name))
}
return strings.Join(cnames, "")
}
func (t tableGen) indexKeyName(names []protoreflect.Name) string {
cnames := make([]string, len(names))
for i, name := range names {
cnames[i] = strcase.ToCamel(string(name))
}
joinedNames := strings.Join(cnames, "")
return t.msg.GoIdent.GoName + joinedNames + "IndexKey"
}
func (t tableGen) indexStructName(fields []string) string {
names := make([]string, len(fields))
for i, field := range fields {
names[i] = strcase.ToCamel(field)
}
joinedNames := strings.Join(names, "")
return t.msg.GoIdent.GoName + joinedNames + "IndexKey"
}
func (t tableGen) genIndex(fields string, id uint32) {
fieldsSlc := strings.Split(fields, ",")
idxKeyName := t.indexStructName(fieldsSlc)
t.P("type ", idxKeyName, " struct {")
t.P("vs []interface{}")
t.P("}")
t.genIndexInterfaceMethods(id, idxKeyName)
for i := 1; i < len(fieldsSlc)+1; i++ {
t.genWithMethods(idxKeyName, fieldsSlc[:i])
}
}
func (t tableGen) genIndexInterfaceMethods(id uint32, indexStructName string) {
funPrefix := fmt.Sprintf("func (x %s) ", indexStructName)
t.P(funPrefix, "id() uint32 {return ", id, "}")
t.P(funPrefix, "values() []interface{} {return x.vs}")
t.P(funPrefix, t.param(t.indexKeyInterfaceName()), "() {}")
t.P()
}
func (t tableGen) genWithMethods(indexStructName string, parts []string) {
funcPrefix := fmt.Sprintf("func (this %s) ", indexStructName)
camelParts := make([]string, len(parts))
for i, part := range parts {
camelParts[i] = strcase.ToCamel(part)
}
funcName := "With" + strings.Join(camelParts, "")
t.P(funcPrefix, funcName, "(", t.fieldArgsFromStringSlice(parts), ") ", indexStructName, "{")
t.P("this.vs = []interface{}{", strings.Join(parts, ","), "}")
t.P("return this")
t.P("}")
t.P()
}