Skip to content

Commit 55ffd0e

Browse files
committed
refactor: naming
1 parent e2b529a commit 55ffd0e

1 file changed

Lines changed: 41 additions & 41 deletions

File tree

tools/doc_generation.go

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ import (
1414
"text/template"
1515
)
1616

17-
type Template struct {
17+
type TemplateData struct {
1818
ConstTable string
1919
}
2020

21-
type Table struct {
22-
table map[string]*TableEntry
21+
type ConstantDocTable struct {
22+
entries map[string]*DocEntry
2323
}
2424

25-
type TableEntry struct {
26-
commentLines []string
27-
constName string
25+
type DocEntry struct {
26+
rawCommentLines []string
27+
constName string
2828

2929
Description string
3030
Type string
@@ -33,54 +33,54 @@ type TableEntry struct {
3333
Internal bool
3434
}
3535

36-
func NewTable() *Table {
37-
return &Table{
38-
table: make(map[string]*TableEntry),
36+
func NewDocTable() *ConstantDocTable {
37+
return &ConstantDocTable{
38+
entries: make(map[string]*DocEntry),
3939
}
4040
}
4141

42-
func (t *Table) AddEntry(constValue, constName string) {
43-
t.table[constValue] = &TableEntry{
44-
commentLines: make([]string, 0),
45-
constName: constName,
42+
func (t *ConstantDocTable) AddEntry(constValue, constName string) {
43+
t.entries[constValue] = &DocEntry{
44+
rawCommentLines: make([]string, 0),
45+
constName: constName,
4646
}
4747
}
4848

49-
func (t *Table) AppendComment(constValue, value string) {
49+
func (t *ConstantDocTable) AppendComment(constValue, value string) {
5050
// trim comment artifacts
5151
comment := strings.Trim(value, "/ \n")
5252
if comment == "" {
5353
return
5454
}
5555

56-
t.table[constValue].commentLines = append(t.table[constValue].commentLines, comment)
56+
t.entries[constValue].rawCommentLines = append(t.entries[constValue].rawCommentLines, comment)
5757
}
5858

59-
func (t *Table) FromAST(node ast.Node) (*Table, error) {
59+
func (t *ConstantDocTable) FromAST(node ast.Node) (*ConstantDocTable, error) {
6060
ast.Inspect(node, t.visitFunc())
6161

62-
for constValue, entry := range t.table {
62+
for constValue, entry := range t.entries {
6363
commentBuilder := strings.Builder{}
6464

65-
for i, line := range entry.commentLines {
66-
if val := getValueFromLine(line, "Type: "); val != "" {
65+
for i, line := range entry.rawCommentLines {
66+
if val := parseMetadataValue(line, "Type: "); val != "" {
6767
entry.Type = val
6868
continue
6969
}
7070

71-
if val := getValueFromLine(line, "Default: "); val != "" {
71+
if val := parseMetadataValue(line, "Default: "); val != "" {
7272
entry.Default = val
7373
continue
7474
}
7575

76-
if val := getValueFromLine(line, "Read-only: "); val != "" {
76+
if val := parseMetadataValue(line, "Read-only: "); val != "" {
7777
if readOnly, err := strconv.ParseBool(val); err == nil {
7878
entry.ReadOnly = readOnly
7979
}
8080
continue
8181
}
8282

83-
if val := getValueFromLine(line, "Internal: "); val != "" {
83+
if val := parseMetadataValue(line, "Internal: "); val != "" {
8484
if internal, err := strconv.ParseBool(val); err == nil {
8585
entry.Internal = internal
8686
}
@@ -102,7 +102,7 @@ func (t *Table) FromAST(node ast.Node) (*Table, error) {
102102
}
103103

104104
comment := strings.Trim(commentBuilder.String(), " ")
105-
for _, entryInner := range t.table {
105+
for _, entryInner := range t.entries {
106106
comment = strings.ReplaceAll(
107107
comment,
108108
fmt.Sprintf("%s ", entryInner.constName),
@@ -115,7 +115,7 @@ func (t *Table) FromAST(node ast.Node) (*Table, error) {
115115
return t, nil
116116
}
117117

118-
func (t *Table) String(tableHeaderName string, hasReadOnlyColumn bool) string {
118+
func (t *ConstantDocTable) String(tableHeaderName string, hasReadOnlyColumn bool) string {
119119
tableStr := strings.Builder{}
120120

121121
if hasReadOnlyColumn {
@@ -126,20 +126,20 @@ func (t *Table) String(tableHeaderName string, hasReadOnlyColumn bool) string {
126126
tableStr.WriteString("| --- | --- | --- | --- |\n")
127127
}
128128

129-
constValues := slices.Sorted(maps.Keys(t.table))
129+
constValues := slices.Sorted(maps.Keys(t.entries))
130130

131131
for _, constValue := range constValues {
132132
// Skip internal const values
133-
if t.table[constValue].Internal {
133+
if t.entries[constValue].Internal {
134134
continue
135135
}
136136

137137
// Escape pipe characters in Type to avoid breaking the table
138-
typeVal := strings.ReplaceAll(t.table[constValue].Type, "|", "\\|")
138+
typeVal := strings.ReplaceAll(t.entries[constValue].Type, "|", "\\|")
139139

140140
defaultVal := "-"
141-
if t.table[constValue].Default != "" {
142-
defaultVal = t.table[constValue].Default
141+
if t.entries[constValue].Default != "" {
142+
defaultVal = t.entries[constValue].Default
143143
}
144144

145145
tableStr.WriteString(fmt.Sprintf("| `%s` ", constValue))
@@ -148,19 +148,19 @@ func (t *Table) String(tableHeaderName string, hasReadOnlyColumn bool) string {
148148

149149
if hasReadOnlyColumn {
150150
readOnlyVal := "No"
151-
if t.table[constValue].ReadOnly {
151+
if t.entries[constValue].ReadOnly {
152152
readOnlyVal = "Yes"
153153
}
154154
tableStr.WriteString(fmt.Sprintf("| `%s` ", readOnlyVal))
155155
}
156156

157-
tableStr.WriteString(fmt.Sprintf("| %s |\n", t.table[constValue].Description))
157+
tableStr.WriteString(fmt.Sprintf("| %s |\n", t.entries[constValue].Description))
158158
}
159159

160160
return tableStr.String()
161161
}
162162

163-
func (t *Table) visitFunc() func(n ast.Node) bool {
163+
func (t *ConstantDocTable) visitFunc() func(n ast.Node) bool {
164164
return func(n ast.Node) bool {
165165
genDecl, ok := n.(*ast.GenDecl)
166166
if !ok {
@@ -206,40 +206,40 @@ func (t *Table) visitFunc() func(n ast.Node) bool {
206206
}
207207
}
208208

209-
func getValueFromLine(line, key string) string {
209+
func parseMetadataValue(line, key string) string {
210210
parts := strings.Split(line, key)
211211
if len(parts) > 1 {
212212
return strings.Trim(parts[1], " \n.")
213213
}
214214
return ""
215215
}
216216

217-
func run(templatePath string, constFilePath string, outputPath string, tableHeaderName string, hasReadOnlyColumn bool) error {
217+
func generateDocs(templatePath string, constFilePath string, outputPath string, tableHeaderName string, hasReadOnlyColumn bool) error {
218218
// Read template file
219-
tmplStr, err := os.ReadFile(templatePath)
219+
templateContent, err := os.ReadFile(templatePath)
220220
if err != nil {
221221
return err
222222
}
223223

224224
// Parse AST
225-
node, err := parser.ParseFile(&token.FileSet{}, constFilePath, nil, parser.ParseComments)
225+
astNode, err := parser.ParseFile(&token.FileSet{}, constFilePath, nil, parser.ParseComments)
226226
if err != nil {
227227
return fmt.Errorf("error parsing file: %w", err)
228228
}
229229

230230
// Create table from AST
231-
table, err := NewTable().FromAST(node)
231+
docTable, err := NewDocTable().FromAST(astNode)
232232
if err != nil {
233233
return err
234234
}
235235

236236
// Create Markdown file from template
237-
tmpl, err := template.New("consts").Parse(string(tmplStr))
237+
tmpl, err := template.New("consts").Parse(string(templateContent))
238238
if err != nil {
239239
return fmt.Errorf("error parsing template: %w", err)
240240
}
241241

242-
tmplData := Template{ConstTable: table.String(tableHeaderName, hasReadOnlyColumn)}
242+
tmplData := TemplateData{ConstTable: docTable.String(tableHeaderName, hasReadOnlyColumn)}
243243
var buf bytes.Buffer
244244
if err := tmpl.Execute(&buf, tmplData); err != nil {
245245
return fmt.Errorf("error executing template: %w", err)
@@ -273,12 +273,12 @@ func main() {
273273
lbEnvPath := "../internal/config/load_balancer_envs.go"
274274
lbEnvOutputPath := "../docs/reference/load_balancer_envs.md"
275275

276-
if err := run(lbTemplatePath, lbAnnotationsPath, lbOutputPath, "Annotation", true); err != nil {
276+
if err := generateDocs(lbTemplatePath, lbAnnotationsPath, lbOutputPath, "Annotation", true); err != nil {
277277
fmt.Fprintf(os.Stderr, "error: %v\n", err)
278278
os.Exit(1)
279279
}
280280

281-
if err := run(lbEnvTemplatePath, lbEnvPath, lbEnvOutputPath, "Environment Variable", false); err != nil {
281+
if err := generateDocs(lbEnvTemplatePath, lbEnvPath, lbEnvOutputPath, "Environment Variable", false); err != nil {
282282
fmt.Fprintf(os.Stderr, "error: %v\n", err)
283283
os.Exit(1)
284284
}

0 commit comments

Comments
 (0)