Skip to content

Commit e2b529a

Browse files
committed
refactor: rename annotation specifc code parts
1 parent 1d9d81f commit e2b529a

4 files changed

Lines changed: 40 additions & 54 deletions

File tree

docs/reference/load_balancer_envs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Some environment variables define global defaults. These defaults can be overrid
66

77
Enums are depicted in the `Type` column and possible options are separated via the pipe symbol `|`.
88

9-
| Annotation | Type | Default | Description |
9+
| Environment Variable | Type | Default | Description |
1010
| --- | --- | --- | --- |
1111
| `HCLOUD_LOAD_BALANCERS_ALGORITHM_TYPE` | `round_robin \| least_connections` | `round_robin` | Configures the default Load Balancer algorithm. |
1212
| `HCLOUD_LOAD_BALANCERS_DISABLE_IPV6` | `bool` | `false` | Disables the use of IPv6 for the Load Balancer by default. |

tools/doc_generation.go

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
type Template struct {
18-
AnnotationsTable string
18+
ConstTable string
1919
}
2020

2121
type Table struct {
@@ -39,27 +39,27 @@ func NewTable() *Table {
3939
}
4040
}
4141

42-
func (t *Table) AddEntry(annotation, constName string) {
43-
t.table[annotation] = &TableEntry{
42+
func (t *Table) AddEntry(constValue, constName string) {
43+
t.table[constValue] = &TableEntry{
4444
commentLines: make([]string, 0),
4545
constName: constName,
4646
}
4747
}
4848

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

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

5959
func (t *Table) FromAST(node ast.Node) (*Table, error) {
6060
ast.Inspect(node, t.visitFunc())
6161

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

6565
for i, line := range entry.commentLines {
@@ -98,15 +98,15 @@ func (t *Table) FromAST(node ast.Node) (*Table, error) {
9898
}
9999

100100
if entry.Type == "" {
101-
return nil, fmt.Errorf("missing Type for annotation %s", annotation)
101+
return nil, fmt.Errorf("missing Type for %s", constValue)
102102
}
103103

104104
comment := strings.Trim(commentBuilder.String(), " ")
105105
for _, entryInner := range t.table {
106106
comment = strings.ReplaceAll(
107107
comment,
108108
fmt.Sprintf("%s ", entryInner.constName),
109-
fmt.Sprintf("`%s` ", annotation),
109+
fmt.Sprintf("`%s` ", constValue),
110110
)
111111
}
112112
entry.Description = comment
@@ -115,60 +115,46 @@ func (t *Table) FromAST(node ast.Node) (*Table, error) {
115115
return t, nil
116116
}
117117

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

121121
if hasReadOnlyColumn {
122-
tableStr.WriteString("| Annotation | Type | Default | Read-only | Description |\n")
122+
tableStr.WriteString(fmt.Sprintf("| %s | Type | Default | Read-only | Description |\n", tableHeaderName))
123123
tableStr.WriteString("| --- | --- | --- | --- | --- |\n")
124124
} else {
125-
tableStr.WriteString("| Annotation | Type | Default | Description |\n")
125+
tableStr.WriteString(fmt.Sprintf("| %s | Type | Default | Description |\n", tableHeaderName))
126126
tableStr.WriteString("| --- | --- | --- | --- |\n")
127127
}
128128

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

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

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

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

145-
readOnlyVal := "No"
146-
if t.table[annotation].ReadOnly {
147-
readOnlyVal = "Yes"
148-
}
145+
tableStr.WriteString(fmt.Sprintf("| `%s` ", constValue))
146+
tableStr.WriteString(fmt.Sprintf("| `%s` ", typeVal))
147+
tableStr.WriteString(fmt.Sprintf("| `%s` ", defaultVal))
149148

150149
if hasReadOnlyColumn {
151-
tableStr.WriteString(
152-
fmt.Sprintf(
153-
"| `%s` | `%s` | `%s` | `%s` | %s |\n",
154-
annotation,
155-
typeVal,
156-
defaultVal,
157-
readOnlyVal,
158-
t.table[annotation].Description,
159-
),
160-
)
161-
} else {
162-
tableStr.WriteString(
163-
fmt.Sprintf(
164-
"| `%s` | `%s` | `%s` | %s |\n",
165-
annotation,
166-
typeVal,
167-
defaultVal,
168-
t.table[annotation].Description,
169-
),
170-
)
150+
readOnlyVal := "No"
151+
if t.table[constValue].ReadOnly {
152+
readOnlyVal = "Yes"
153+
}
154+
tableStr.WriteString(fmt.Sprintf("| `%s` ", readOnlyVal))
171155
}
156+
157+
tableStr.WriteString(fmt.Sprintf("| %s |\n", t.table[constValue].Description))
172158
}
173159

174160
return tableStr.String()
@@ -207,12 +193,12 @@ func (t *Table) visitFunc() func(n ast.Node) bool {
207193
}
208194

209195
constName := valueSpec.Names[0].Name
210-
annotation := strings.ReplaceAll(literal.Value, "\"", "")
196+
value := strings.ReplaceAll(literal.Value, "\"", "")
211197

212-
t.AddEntry(annotation, constName)
198+
t.AddEntry(value, constName)
213199

214200
for _, comment := range valueSpec.Doc.List {
215-
t.AppendComment(annotation, comment.Text)
201+
t.AppendComment(value, comment.Text)
216202
}
217203
}
218204

@@ -228,15 +214,15 @@ func getValueFromLine(line, key string) string {
228214
return ""
229215
}
230216

231-
func run(templatePath string, annotationsPath string, outputPath string, hasReadOnlyColumn bool) error {
217+
func run(templatePath string, constFilePath string, outputPath string, tableHeaderName string, hasReadOnlyColumn bool) error {
232218
// Read template file
233219
tmplStr, err := os.ReadFile(templatePath)
234220
if err != nil {
235221
return err
236222
}
237223

238224
// Parse AST
239-
node, err := parser.ParseFile(&token.FileSet{}, annotationsPath, nil, parser.ParseComments)
225+
node, err := parser.ParseFile(&token.FileSet{}, constFilePath, nil, parser.ParseComments)
240226
if err != nil {
241227
return fmt.Errorf("error parsing file: %w", err)
242228
}
@@ -248,12 +234,12 @@ func run(templatePath string, annotationsPath string, outputPath string, hasRead
248234
}
249235

250236
// Create Markdown file from template
251-
tmpl, err := template.New("annotations").Parse(string(tmplStr))
237+
tmpl, err := template.New("consts").Parse(string(tmplStr))
252238
if err != nil {
253239
return fmt.Errorf("error parsing template: %w", err)
254240
}
255241

256-
tmplData := Template{AnnotationsTable: table.String(hasReadOnlyColumn)}
242+
tmplData := Template{ConstTable: table.String(tableHeaderName, hasReadOnlyColumn)}
257243
var buf bytes.Buffer
258244
if err := tmpl.Execute(&buf, tmplData); err != nil {
259245
return fmt.Errorf("error executing template: %w", err)
@@ -287,12 +273,12 @@ func main() {
287273
lbEnvPath := "../internal/config/load_balancer_envs.go"
288274
lbEnvOutputPath := "../docs/reference/load_balancer_envs.md"
289275

290-
if err := run(lbTemplatePath, lbAnnotationsPath, lbOutputPath, true); err != nil {
276+
if err := run(lbTemplatePath, lbAnnotationsPath, lbOutputPath, "Annotation", true); err != nil {
291277
fmt.Fprintf(os.Stderr, "error: %v\n", err)
292278
os.Exit(1)
293279
}
294280

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

tools/load_balancer_annotations.md.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ This page contains all annotations, which can be specified at a Service of type
55
- Read-only annotations are set by the Cloud Controller Manager.
66
- Enums are depicted in the `Type` column and possible options are separated via the pipe symbol `|`.
77

8-
{{.AnnotationsTable}}
8+
{{.ConstTable}}

tools/load_balancer_envs.md.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ Some environment variables define global defaults. These defaults can be overrid
66

77
Enums are depicted in the `Type` column and possible options are separated via the pipe symbol `|`.
88

9-
{{.AnnotationsTable}}
9+
{{.ConstTable}}

0 commit comments

Comments
 (0)