-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_versions_fields.generator.go
166 lines (135 loc) · 3.6 KB
/
models_versions_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 (
"html/template"
"os"
"reflect"
"strings"
"time"
"github.com/DevAlone/parse_pikabu/models"
"github.com/iancoleman/strcase"
)
func main() {
f, err := os.Create("models/models_versions_fields.generated.go")
handleErr(err)
defer f.Close()
err = headerTemplate.Execute(f, struct {
Timestamp time.Time
}{
Timestamp: time.Now(),
})
handleErr(err)
generatedTablesNames := []string{}
for _, table := range models.Tables {
versionsFields := []reflect.StructField{}
v := reflect.TypeOf(table).Elem()
typeName := v.Name()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
_, found := field.Tag.Lookup("gen_versions")
if found {
versionsFields = append(versionsFields, field)
}
}
if len(versionsFields) > 0 {
for _, table := range getVersionsTables(typeName, versionsFields) {
generatedTablesNames = append(generatedTablesNames, table.tableName)
_, err := f.WriteString(table.tableCode)
handleErr(err)
}
}
}
fieldsVersionTablesMapCode := `
var FieldsVersionTablesMap = map[string]interface{}{
`
for _, tableName := range generatedTablesNames {
fieldsVersionTablesMapCode += `"` + tableName + `": &` + tableName + "{},\n"
}
fieldsVersionTablesMapCode += `}
`
_, err = f.WriteString(fieldsVersionTablesMapCode)
handleErr(err)
fieldsVersionAPITablesMapCode := `
var FieldsVersionAPITablesMap = map[string]interface{}{
`
for _, tableName := range generatedTablesNames {
fieldsVersionAPITablesMapCode += `"` + tableName + `": []` + tableName + "{},\n"
}
fieldsVersionAPITablesMapCode += `}
`
_, err = f.WriteString(fieldsVersionAPITablesMapCode)
handleErr(err)
// generate init
_, err = f.WriteString(getInit(generatedTablesNames))
handleErr(err)
}
func getVersionsTables(typeName string, versionsFields []reflect.StructField) []struct {
tableName string
tableCode string
} {
result := []struct {
tableName string
tableCode string
}{}
for _, field := range versionsFields {
fieldName := field.Name
var fieldTypeName string
switch field.Type.Kind() {
case reflect.Slice, reflect.Array:
fieldTypeName = field.Type.String()
default:
fieldTypeName = field.Type.Name()
}
if strings.HasPrefix(fieldTypeName, "[]models.") {
fieldTypeName = "[]" + fieldTypeName[9:]
}
name := typeName + fieldName + "Version"
result = append(result, struct {
tableName string
tableCode string
}{
tableName: name,
tableCode: getModelCode(name, fieldTypeName) + "\n",
})
}
return result
}
func handleErr(err error) {
if err != nil {
panic(err)
}
}
func getStructMemberCode(memberName string, memberType string, isPk bool) string {
result := "\t" + memberName + " " + memberType + " `sql:\""
if isPk {
result += ",pk"
}
apiName := strcase.ToSnake(memberName)
result += ",notnull\" json:\"" + apiName + "\" api:\"" + "order,filter" + "\"`\n"
return result
}
func getModelCode(tableName string, fieldType string) string {
result := "type " + tableName + " struct {\n"
result += getStructMemberCode("ItemId", "uint64", true)
result += getStructMemberCode("Timestamp", "TimestampType", true)
result += getStructMemberCode("Value", fieldType, false)
result += "}\n"
return result
}
var headerTemplate = template.Must(template.New("").Parse(`package models
// generated code, do not touch!
// generated at timestamp {{ .Timestamp }}
`))
func getInit(tableNames []string) string {
result := `func init() {
for _, item := range []interface{}{
`
for _, tableName := range tableNames {
result += "&" + tableName + "{},\n"
}
result += `} {
Tables = append(Tables, item)
}
}`
return result
}