-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_distributions_fields.generator.go
166 lines (142 loc) · 4.08 KB
/
models_distributions_fields.generator.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
//+build ignore
package main
import (
"fmt"
"html/template"
"os"
"reflect"
"strconv"
"time"
"github.com/DevAlone/parse_pikabu/models"
)
type FieldT struct {
Field reflect.StructField
BucketSize int
}
var headerTemplate = template.Must(template.New("").Parse(`package models
// generated code, do not touch!
// generated at timestamp {{ .Timestamp }}
`))
func main() {
f, err := os.Create("models/models_distributions_fields.generated.go")
handleErr(err)
defer func() { _ = f.Close() }()
err = headerTemplate.Execute(f, struct {
Timestamp time.Time
}{
Timestamp: time.Now(),
})
handleErr(err)
generatedTables := []struct {
BaseTableName string
BaseColumnName string
GeneratedTableName string
BucketSize int
}{}
for _, table := range models.Tables {
fields := []FieldT{}
v := reflect.TypeOf(table).Elem()
typeName := v.Name()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
tag, found := field.Tag.Lookup("gen_distributions")
if !found {
continue
}
if len(tag) == 0 {
fmt.Printf("%v %v\n", table, field.Name)
panic(0)
}
bucketSize, err := strconv.Atoi(tag)
handleErr(err)
fields = append(fields, FieldT{
Field: field,
BucketSize: bucketSize,
})
}
if len(fields) > 0 {
for _, field := range fields {
baseTableNameCamelCase := typeName
// baseTableNameSnakeCase := strcase.ToSnake(baseTableNameCamelCase) + "s"
baseColumnName := field.Field.Name
generatedTableNameCamelCase := baseTableNameCamelCase + baseColumnName + "Distribution_" + fmt.Sprint(field.BucketSize)
err := renderModelCode(f, generatedTableNameCamelCase, "int64")
handleErr(err)
generatedTables = append(generatedTables, struct {
BaseTableName string
BaseColumnName string
GeneratedTableName string
BucketSize int
}{
BaseTableName: baseTableNameCamelCase,
BaseColumnName: baseColumnName,
GeneratedTableName: generatedTableNameCamelCase,
BucketSize: field.BucketSize,
})
}
}
}
generatedDistributionFieldsCode := `
// distribution table name: base table name, base column name, distribution table
var GeneratedDistributionFields = map[string]struct{BaseTableName, BaseColumnName string; DistributionTable interface{}; BucketSize int}{
`
for _, table := range generatedTables {
generatedDistributionFieldsCode += `"` + table.GeneratedTableName + `": {"` +
table.BaseTableName + `", "` + table.BaseColumnName + `", &` + table.GeneratedTableName + "{}, " + fmt.Sprint(table.BucketSize) + "},\n"
}
generatedDistributionFieldsCode += "}"
_, err = f.WriteString(generatedDistributionFieldsCode)
handleErr(err)
// for api
fieldsDistributionAPITablesMapCode := `
var GeneratedDistributionFieldsAPI = map[string]interface{}{
`
for _, table := range generatedTables {
fieldsDistributionAPITablesMapCode += `"` + table.GeneratedTableName + `": []` + table.GeneratedTableName + "{},\n"
}
fieldsDistributionAPITablesMapCode += `}
`
_, err = f.WriteString(fieldsDistributionAPITablesMapCode)
handleErr(err)
// generate init
_, err = f.WriteString(getInit(generatedTables))
handleErr(err)
}
func handleErr(err error) {
if err != nil {
panic(err)
}
}
func renderModelCode(file *os.File, modelName string, valueFieldType string) error {
return template.Must(template.New("").Parse(`
{{ $backtick := "`+"`"+`" }}
type {{.ModelName}} struct {
Timestamp TimestampType {{ $backtick }}sql:",pk,notnull" json:"timestamp" api:"order,filter"{{ $backtick }}
Value {{.ValueType}} {{ $backtick }}sql:",notnull" json:"value"{{ $backtick }}
}
`)).Execute(file, struct {
ModelName string
ValueType string
}{
ModelName: modelName,
ValueType: valueFieldType,
})
}
func getInit(tableNames []struct {
BaseTableName string
BaseColumnName string
GeneratedTableName string
BucketSize int
}) string {
result := `func init() {
for _, item := range []interface{}{
`
for _, table := range tableNames {
result += "&" + table.GeneratedTableName + "{},\n"
}
result += `} {
Tables = append(Tables, item)
}
}`
return result
}