forked from relops/cqlc
/
template.go
164 lines (137 loc) · 4.48 KB
/
template.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
package generator
import (
"bytes"
"fmt"
"github.com/gocql/gocql"
"regexp"
"strings"
"text/template"
)
var bindingTemplate *template.Template
var camelRegex = regexp.MustCompile("[0-9A-Za-z]+")
func init() {
m := template.FuncMap{
"toUpper": strings.ToUpper,
"sprint": fmt.Sprint,
"snakeToCamel": snakeToCamel,
"columnType": columnType,
"valueType": valueType,
"isCounterColumn": isCounterColumn,
"supportsClustering": supportsClustering,
"supportsPartitioning": supportsPartitioning,
"isListType": isListType,
"hasSecondaryIndex": hasSecondaryIndex,
"isLastComponent": isLastComponent,
"isCounterColumnFamily": isCounterColumnFamily,
}
temp, _ := generator_tmpl_binding_tmpl()
bindingTemplate = template.Must(template.New("binding.tmpl").Funcs(m).Parse(string(temp)))
}
// TODO This is metadata specific to the column family that should be cached at compilation
// rather than being post-processed like this
func isCounterColumnFamily(t gocql.TableMetadata) bool {
for _, col := range t.Columns {
if isCounterColumn(*col) {
return true
}
}
return false
}
func isCounterColumn(c gocql.ColumnMetadata) bool {
return c.Type.Type() == gocql.TypeCounter
}
func supportsClustering(c gocql.ColumnMetadata) bool {
return c.Kind == gocql.CLUSTERING_KEY
}
func supportsPartitioning(c gocql.ColumnMetadata) bool {
return c.Kind == gocql.PARTITION_KEY
}
func isLastComponent(c gocql.ColumnMetadata, t *gocql.TableMetadata) bool {
switch c.Kind {
case gocql.PARTITION_KEY:
lastPartitionKeyColumn := t.PartitionKey[len(t.PartitionKey)-1]
return c.Name == lastPartitionKeyColumn.Name
case gocql.CLUSTERING_KEY:
lastClusteringColumn := t.ClusteringColumns[len(t.ClusteringColumns)-1]
return c.Name == lastClusteringColumn.Name
default:
return false
}
}
func isListType(c gocql.ColumnMetadata) bool {
return c.Type.Type() == gocql.TypeList || c.Type.Type() == gocql.TypeSet
}
func hasSecondaryIndex(c gocql.ColumnMetadata) bool {
return c.Index.Name != ""
}
func columnType(c gocql.ColumnMetadata, table *gocql.TableMetadata) string {
t := c.Type
baseType := columnTypes[t.Type()]
// TODO The Kind field should be an enum, not a string
if c.Kind == gocql.CLUSTERING_KEY {
replacement := ".Clustered"
if isLastComponent(c, table) {
replacement = ".LastClustered"
}
baseType = strings.Replace(baseType, ".", replacement, 1)
} else if c.Kind == gocql.PARTITION_KEY {
replacement := ".Partitioned"
if isLastComponent(c, table) {
replacement = ".LastPartitioned"
}
baseType = strings.Replace(baseType, ".", replacement, 1)
} else if c.Index.Name != "" {
replacement := ".Equality"
baseType = strings.Replace(baseType, ".", replacement, 1)
}
switch t.Type() {
case gocql.TypeMap:
// TODO This is very hacky - basically the types need to to be strings
// in order to template out properly
// Resolving these to integer enums is not helpful, as this example shows
// HACK UPDATE (04/03/2015): The domain and range types have been
// superseded by gocql.TypeInfo.{Key,Elem}, but this still needs to get pulled through
// TODO should probably not swallow this
ct, _ := t.(gocql.CollectionType)
key := columnTypes[ct.Key.Type()]
elem := columnTypes[ct.Elem.Type()]
key = strings.Replace(key, "_Column", "", 1)
elem = strings.Replace(elem, "_", "Map", 1)
elem = strings.Replace(elem, "cqlc.", "", 1)
return fmt.Sprintf("%s%s", key, elem)
case gocql.TypeList, gocql.TypeSet:
// TODO should probably not swallow this
ct, _ := t.(gocql.CollectionType)
elem := columnTypes[ct.Elem.Type()]
return strings.Replace(elem, "_", "Slice", 1)
default:
return strings.Replace(baseType, "_", "", 1)
}
return baseType
}
func valueType(c gocql.ColumnMetadata) string {
t := c.Type
switch t.Type() {
case gocql.TypeList, gocql.TypeSet:
// TODO should probably not swallow this
ct, _ := t.(gocql.CollectionType)
literal := literalTypes[ct.Elem.Type()]
return fmt.Sprintf("[]%s", literal)
case gocql.TypeMap:
// TODO should probably not swallow this
ct, _ := t.(gocql.CollectionType)
key := literalTypes[ct.Key.Type()]
elem := literalTypes[ct.Elem.Type()]
return fmt.Sprintf("map[%s]%s", key, elem)
default:
return literalTypes[t.Type()]
}
}
func snakeToCamel(src string) string {
byteSrc := []byte(src)
chunks := camelRegex.FindAll(byteSrc, -1)
for i, val := range chunks {
chunks[i] = bytes.Title(val)
}
return string(bytes.Join(chunks, nil))
}