-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_categories.go
147 lines (131 loc) · 3.42 KB
/
generate_categories.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
//go:build ignore
// +build ignore
package main
import (
"bytes"
"encoding/json"
"go/format"
"os"
"sort"
"strings"
"text/template"
)
const categoryTemplate = `// {{ .StructName }} is an auto-generated struct which is used to allow for simple categorisation of the APIs.
// It is public since it may be desired to store a reference to this somewhere, however, do NOT create a instance of this
// directly. Instead, call NewClient and then go to the field {{ .FieldPath }}.
type {{ .StructName }} struct {
c clientDoer{{ .AdditionalFields }}
}`
func buildStruct(name, fieldPath, fields string) string {
if fields != "" {
fields = "\n\n" + fields
}
tpl, err := template.New("tpl").Parse(categoryTemplate)
if err != nil {
panic(err)
}
buf := &bytes.Buffer{}
err = tpl.Execute(buf, map[string]string{
"StructName": name,
"FieldPath": fieldPath,
"AdditionalFields": fields,
})
if err != nil {
panic(err)
}
return buf.String()
}
type field struct {
key, value, code string
}
func main() {
// Read categories.json.
b, err := os.ReadFile("categories.json")
if err != nil {
panic(err)
}
var categories map[string][]string
err = json.Unmarshal(b, &categories)
if err != nil {
panic(err)
}
// Get the root categories and sort them.
rootCats := make([]string, len(categories))
i := 0
for rootCat := range categories {
rootCats[i] = rootCat
i++
}
sort.Strings(rootCats)
// Turn the categories into code.
goCode := []string{}
for _, rootCat := range rootCats {
// Get the sub-categories.
subCats := categories[rootCat]
// Get the fields for this category.
fields := []field{}
for _, subCat := range subCats {
structName := subCat
goName := subCat
if strings.HasSuffix(goName, ":no-prefix") {
// Remove this suffix.
goName = goName[:len(goName)-10]
structName = goName
goName = "ClientCategory" + goName
} else {
// Add the prefix.
goName = rootCat + goName
}
fields = append(fields, field{
key: structName,
value: goName,
code: buildStruct("ClientCategory"+goName, rootCat+"."+structName, ""),
})
}
// Build the root struct.
fieldsS := ""
if len(fields) != 0 {
// Start with 2 blank lines.
fieldsS += "\n\n"
// Handle each field.
for _, v := range fields {
fieldsS += "\t" + v.key + " *ClientCategory" + v.value + "\n"
}
}
rootStruct := buildStruct("ClientCategory"+rootCat, rootCat, fieldsS)
// Append all the structs.
for _, v := range fields {
goCode = append(goCode, v.code)
}
goCode = append(goCode, rootStruct)
// Build the root initializer.
initFunc := "func new" + rootCat + "(c clientDoer) *ClientCategory" + rootCat + " {"
if len(fields) == 0 {
// Do a simple inline init here.
initFunc += "\n\treturn &ClientCategory" + rootCat + "{c}\n}"
} else {
// Handle each field.
initFunc += "\n\treturn &ClientCategory" + rootCat + "{\n\t\tc: c,"
for _, v := range fields {
initFunc += "\n\t\t" + v.key + ": &ClientCategory" + v.value + "{c},"
}
initFunc += "\n\t}\n}"
}
goCode = append(goCode, initFunc)
}
// Make the go file.
autogen := `package hop
// Code generated by generate_categories.go; DO NOT EDIT.
//go:generate go run generate_categories.go`
for _, code := range goCode {
autogen += "\n\n" + code
}
b, err = format.Source([]byte(autogen))
if err != nil {
panic(err)
}
err = os.WriteFile("categories_gen.go", b, 0o666)
if err != nil {
panic(err)
}
}