Skip to content

Commit f6b2315

Browse files
committed
MAJOR: config-parser: allow using models for types
1 parent 19abe15 commit f6b2315

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

config-parser/generate/config-file.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ type Data struct { //nolint:maligned
7676
HasAlias bool
7777
HasDefault bool
7878
HasTable bool
79+
Model string // model name for swagger
80+
Doc string // url to docs
7981
}
8082

8183
type ConfigFile struct {

config-parser/generate/normal.tmpl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,26 @@ package parsers
77
import (
88
"github.com/haproxytech/client-native/v6/config-parser/common"
99
"github.com/haproxytech/client-native/v6/config-parser/errors"
10+
{{- if not (eq .Model "") }}
11+
"github.com/haproxytech/client-native/v6/models"
12+
{{- else }}
1013
"github.com/haproxytech/client-native/v6/config-parser/{{ .DataDir }}types"
14+
{{- end }}
1115
)
1216

17+
{{- if not (eq .Doc "") }}
18+
// {{ .Doc }}
19+
{{- end }}
20+
1321
{{- if not .NoInit }}
1422

1523
func (p *{{ .StructName }}) Init() {
1624
{{- if .ParserMultiple }}
25+
{{- if not (eq .Model "") }}
26+
p.data = make([]models.{{ .Model }}, 0)
27+
{{- else }}
1728
p.data = []types.{{ .ParserType }}{}
29+
{{- end }}
1830
{{- else }}
1931
p.data = nil
2032
{{- end }}
@@ -52,7 +64,11 @@ func (p *{{ .StructName }}) Get(createIfNotExist bool) (common.ParserData, error
5264
{{- else }}
5365
if p.data == nil {
5466
if createIfNotExist {
67+
{{- if not (eq .Model "") }}
68+
p.data = &models.{{ .Model }}{}
69+
{{- else }}
5570
p.data = &types.{{ .ParserType }}{}
71+
{{- end }}
5672
return p.data, nil
5773
}
5874
return nil, errors.ErrFetch
@@ -96,7 +112,11 @@ func (p *{{ .StructName }}) Delete(index int) error {
96112
{{- if .IsInterface }}
97113
p.data[len(p.data)-1] = nil
98114
{{- else }}
115+
{{- if not (eq .Model "") }}
116+
p.data[len(p.data)-1] = models.{{ .Model }}{}
117+
{{- else }}
99118
p.data[len(p.data)-1] = types.{{ .ParserType }}{}
119+
{{- end }}
100120
{{- end }}
101121
p.data = p.data[:len(p.data)-1]
102122
return nil
@@ -112,32 +132,52 @@ func (p *{{ .StructName }}) Insert(data common.ParserData, index int) error {
112132
return errors.ErrInvalidData
113133
}
114134
switch newValue := data.(type) {
135+
{{- if not (eq .Model "") }}
136+
case []models.{{ .Model }}:
137+
{{- else }}
115138
case []types.{{ .ParserType }}:
139+
{{- end }}
116140
p.data = newValue
141+
{{- if not (eq .Model "") }}
142+
case *models.{{ .Model }}:
143+
{{- else }}
117144
case *types.{{ .ParserType }}:
145+
{{- end }}
118146
if index > -1 {
119147
if index > len(p.data) {
120148
return errors.ErrIndexOutOfRange
121149
}
122150
{{- if .IsInterface }}
123151
p.data = append(p.data, nil)
124152
{{- else }}
153+
{{- if not (eq .Model "") }}
154+
p.data = append(p.data, models.{{ .Model }}{})
155+
{{- else }}
125156
p.data = append(p.data, types.{{ .ParserType }}{})
157+
{{- end }}
126158
{{- end }}
127159
copy(p.data[index+1:], p.data[index:])
128160
p.data[index] = *newValue
129161
} else {
130162
p.data = append(p.data, *newValue)
131163
}
164+
{{- if not (eq .Model "") }}
165+
case models.{{ .Model }}:
166+
{{- else }}
132167
case types.{{ .ParserType }}:
168+
{{- end }}
133169
if index > -1 {
134170
if index > len(p.data) {
135171
return errors.ErrIndexOutOfRange
136172
}
137173
{{- if .IsInterface }}
138174
p.data = append(p.data, nil)
139175
{{- else }}
176+
{{- if not (eq .Model "") }}
177+
p.data = append(p.data, models.{{ .Model }}{})
178+
{{- else }}
140179
p.data = append(p.data, types.{{ .ParserType }}{})
180+
{{- end }}
141181
{{- end }}
142182
copy(p.data[index+1:], p.data[index:])
143183
p.data[index] = newValue
@@ -160,17 +200,29 @@ func (p *{{ .StructName }}) Set(data common.ParserData, index int) error {
160200
}
161201
{{- if .ParserMultiple }}
162202
switch newValue := data.(type) {
203+
{{- if not (eq .Model "") }}
204+
case []models.{{ .Model }}:
205+
{{- else }}
163206
case []types.{{ .ParserType }}:
207+
{{- end }}
164208
p.data = newValue
209+
{{- if not (eq .Model "") }}
210+
case *models.{{ .Model }}:
211+
{{- else }}
165212
case *types.{{ .ParserType }}:
213+
{{- end }}
166214
if index > -1 && index < len(p.data) {
167215
p.data[index] = *newValue
168216
} else if index == -1 {
169217
p.data = append(p.data, *newValue)
170218
} else {
171219
return errors.ErrIndexOutOfRange
172220
}
221+
{{- if not (eq .Model "") }}
222+
case models.{{ .Model }}:
223+
{{- else }}
173224
case types.{{ .ParserType }}:
225+
{{- end }}
174226
if index > -1 && index < len(p.data) {
175227
p.data[index] = newValue
176228
} else if index == -1 {
@@ -183,9 +235,17 @@ func (p *{{ .StructName }}) Set(data common.ParserData, index int) error {
183235
}
184236
{{- else }}
185237
switch newValue := data.(type) {
238+
{{- if not (eq .Model "") }}
239+
case *models.{{ .Model }}:
240+
{{- else }}
186241
case *types.{{ .ParserType }}:
242+
{{- end }}
187243
p.data = newValue
244+
{{- if not (eq .Model "") }}
245+
case models.{{ .Model }}:
246+
{{- else }}
188247
case types.{{ .ParserType }}:
248+
{{- end }}
189249
p.data = &newValue
190250
default:
191251
return errors.ErrInvalidData

config-parser/generate/types-normal.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ func generateTypes(dir string, dataDir string) { //nolint:gocognit
3737

3838
for _, line := range lines {
3939
parserData.DataDir = dataDir
40+
if strings.HasPrefix(line, "// model:") {
41+
parserData.Model = strings.TrimSpace(strings.TrimPrefix(line, "// model:"))
42+
}
43+
if strings.HasPrefix(line, "//doc:") {
44+
parserData.Doc = strings.TrimSpace(strings.TrimPrefix(line, "//doc:"))
45+
}
4046
if strings.HasPrefix(line, "//deprecated:") {
4147
parserData.Deprecated = true
4248
}
@@ -56,6 +62,15 @@ func generateTypes(dir string, dataDir string) { //nolint:gocognit
5662
parserData.ParserSecondName = items[1]
5763
}
5864
}
65+
if strings.HasPrefix(line, "// name:") {
66+
data := common.StringSplitIgnoreEmpty(line, ':')
67+
items := common.StringSplitIgnoreEmpty(data[1], ' ')
68+
parserData.ParserName = data[1]
69+
if len(items) > 1 {
70+
parserData.ParserName = items[0]
71+
parserData.ParserSecondName = items[1]
72+
}
73+
}
5974
if strings.HasPrefix(line, "//is:multiple") {
6075
parserData.ParserMultiple = true
6176
}

0 commit comments

Comments
 (0)