Skip to content

Commit 24c0f3f

Browse files
committed
MAJOR: acl: use same type in config parser as in rest of the module
1 parent f6b2315 commit 24c0f3f

11 files changed

Lines changed: 98 additions & 73 deletions

File tree

.aspell.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
mode: commit
33
min_length: 3
44
allowed:
5+
- acl
56
- acls
67
- adfs
78
- addons

config-parser/common/metadata.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2019 HAProxy Technologies
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package common
18+
19+
import "github.com/haproxytech/client-native/v6/config-parser/errors"
20+
21+
// ExtractComment extracts the comment from the metadata map.
22+
func ExtractComment(metadata map[string]interface{}) string {
23+
var comment string
24+
if metadata == nil {
25+
return comment
26+
}
27+
if cmt, ok := metadata["comment"]; ok && cmt != "" {
28+
comment, _ = cmt.(string)
29+
}
30+
return comment
31+
}
32+
33+
// ExtractCommentWithErr extracts the comment from the metadata map and returns an error if the comment is not a string.
34+
func ExtractCommentWithErr(metadata map[string]interface{}) (string, error) {
35+
var comment string
36+
if metadata == nil {
37+
return comment, nil
38+
}
39+
if cmt, ok := metadata["comment"]; ok && cmt != "" {
40+
comment, ok = cmt.(string)
41+
if !ok {
42+
return "", errors.ErrInvalidData
43+
}
44+
}
45+
return comment, nil
46+
}

config-parser/parsers/acl.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@ import (
2121

2222
"github.com/haproxytech/client-native/v6/config-parser/common"
2323
"github.com/haproxytech/client-native/v6/config-parser/errors"
24-
"github.com/haproxytech/client-native/v6/config-parser/types"
24+
"github.com/haproxytech/client-native/v6/models"
2525
)
2626

2727
type ACL struct {
28-
data []types.ACL
28+
data []models.ACL
2929
preComments []string // comments that appear before the actual line
3030
}
3131

32-
func (h *ACL) parse(line string, parts []string, comment string) (*types.ACL, error) {
32+
func (h *ACL) parse(line string, parts []string, comment string) (*models.ACL, error) {
3333
if len(parts) >= 3 {
34-
data := &types.ACL{
35-
Name: parts[1],
34+
data := &models.ACL{
35+
ACLName: parts[1],
3636
Criterion: parts[2],
3737
Value: strings.Join(parts[3:], " "),
38-
Comment: comment,
38+
Metadata: map[string]interface{}{},
39+
}
40+
if comment != "" {
41+
data.Metadata = map[string]interface{}{}
42+
data.Metadata["comment"] = comment
3943
}
4044
return data, nil
4145
}
@@ -50,14 +54,14 @@ func (h *ACL) Result() ([]common.ReturnResultLine, error) {
5054
for index, req := range h.data {
5155
var sb strings.Builder
5256
sb.WriteString("acl ")
53-
sb.WriteString(req.Name)
57+
sb.WriteString(req.ACLName)
5458
sb.WriteString(" ")
5559
sb.WriteString(req.Criterion)
5660
sb.WriteString(" ")
5761
sb.WriteString(req.Value)
5862
result[index] = common.ReturnResultLine{
5963
Data: sb.String(),
60-
Comment: req.Comment,
64+
Comment: common.ExtractComment(req.Metadata),
6165
}
6266
}
6367
return result, nil

config-parser/parsers/acl_generated.go

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config-parser/types/types.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,12 @@ type LogStdErr struct {
102102
Comment string
103103
}
104104

105-
//sections:frontend,backend,defaults
105+
// ACL
106+
// model:ACL
107+
//
106108
//name:acl
109+
//doc:https://docs.haproxy.org/dev/configuration.html#acl%20(Alphabetically%20sorted%20keywords%20reference)
110+
//sections:frontend,backend,defaults
107111
//is:multiple
108112
//test:ok:acl url_stats path_beg /stats
109113
//test:ok:acl url_static path_beg -i /static /images /javascript /stylesheets
@@ -122,12 +126,7 @@ type LogStdErr struct {
122126
//test:ok:acl cookie_set hdr_sub(cookie) SEEN=1
123127
//test:fail:acl cookie
124128
//test:fail:acl
125-
type ACL struct {
126-
Name string
127-
Criterion string
128-
Value string
129-
Comment string
130-
}
129+
type ACL struct{}
131130

132131
//sections:frontend
133132
//name:bind

configuration/acl.go

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/go-openapi/strfmt"
2323
parser "github.com/haproxytech/client-native/v6/config-parser"
2424
parser_errors "github.com/haproxytech/client-native/v6/config-parser/errors"
25-
"github.com/haproxytech/client-native/v6/config-parser/types"
2625

2726
"github.com/haproxytech/client-native/v6/models"
2827
)
@@ -85,9 +84,9 @@ func (c *client) GetACL(id int64, parentType, parentName string, transactionID s
8584
return v, nil, c.HandleError(strconv.FormatInt(id, 10), parentType, parentName, "", false, err)
8685
}
8786

88-
acl := ParseACL(data.(types.ACL))
87+
acl := data.(models.ACL)
8988

90-
return v, acl, nil
89+
return v, &acl, nil
9190
}
9291

9392
// DeleteACL deletes a ACL line in configuration. One of version or transactionID is
@@ -130,7 +129,7 @@ func (c *client) CreateACL(id int64, parentType string, parentName string, data
130129
return err
131130
}
132131

133-
if err := p.Insert(section, parentName, "acl", SerializeACL(*data), int(id)); err != nil {
132+
if err := p.Insert(section, parentName, "acl", *data, int(id)); err != nil {
134133
return c.HandleError(strconv.FormatInt(id, 10), parentType, parentName, t, transactionID == "", err)
135134
}
136135

@@ -160,7 +159,7 @@ func (c *client) EditACL(id int64, parentType string, parentName string, data *m
160159
return c.HandleError(strconv.FormatInt(id, 10), parentType, parentName, t, transactionID == "", err)
161160
}
162161

163-
if err := p.Set(section, parentName, "acl", SerializeACL(*data), int(id)); err != nil {
162+
if err := p.Set(section, parentName, "acl", *data, int(id)); err != nil {
164163
return c.HandleError(strconv.FormatInt(id, 10), parentType, parentName, t, transactionID == "", err)
165164
}
166165

@@ -200,7 +199,7 @@ func (c *client) ReplaceAcls(parentType string, parentName string, data models.A
200199
}
201200

202201
for i, newACL := range data {
203-
if err := p.Insert(section, parentName, "acl", SerializeACL(*newACL), i); err != nil {
202+
if err := p.Insert(section, parentName, "acl", *newACL, i); err != nil {
204203
return c.HandleError(strconv.FormatInt(int64(i), 10), parentType, parentName, t, transactionID == "", err)
205204
}
206205
}
@@ -218,42 +217,17 @@ func ParseACLs(section parser.Section, name string, p parser.Parser, aclName ...
218217
return nil, err
219218
}
220219

221-
aclLines, ok := data.([]types.ACL)
220+
aclLines, ok := data.([]models.ACL)
222221
if !ok {
223222
return nil, errors.New("type assert error []types.ACL")
224223
}
225224
lACL := len(aclName)
226-
for _, r := range aclLines {
227-
acl := ParseACL(r)
228-
if acl != nil {
229-
if lACL > 0 && aclName[0] == acl.ACLName {
230-
acls = append(acls, acl)
231-
} else if lACL == 0 {
232-
acls = append(acls, acl)
233-
}
225+
for _, acl := range aclLines {
226+
if lACL > 0 && aclName[0] == acl.ACLName {
227+
acls = append(acls, &acl)
228+
} else if lACL == 0 {
229+
acls = append(acls, &acl)
234230
}
235231
}
236232
return acls, nil
237233
}
238-
239-
func ParseACL(f types.ACL) *models.ACL {
240-
return &models.ACL{
241-
ACLName: f.Name,
242-
Criterion: f.Criterion,
243-
Value: f.Value,
244-
Metadata: parseMetadata(f.Comment),
245-
}
246-
}
247-
248-
func SerializeACL(f models.ACL) types.ACL {
249-
comment, err := serializeMetadata(f.Metadata)
250-
if err != nil {
251-
comment = ""
252-
}
253-
return types.ACL{
254-
Name: f.ACLName,
255-
Criterion: f.Criterion,
256-
Value: f.Value,
257-
Comment: comment,
258-
}
259-
}

configuration/structured_backends.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func serializeBackendSection(a StructuredToParserArgs, b *models.Backend) error
300300
}
301301
}
302302
for i, acl := range b.ACLList {
303-
if err = p.Insert(parser.Backends, b.Name, "acl", SerializeACL(*acl), i); err != nil {
303+
if err = p.Insert(parser.Backends, b.Name, "acl", *acl, i); err != nil {
304304
return a.HandleError(strconv.FormatInt(int64(i), 10), BackendParentName, b.Name, a.TID, a.TID == "", err)
305305
}
306306
}

configuration/structured_defaults.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,10 @@ func serializeDefaultsSection(a StructuredToParserArgs, d *models.Defaults) erro
380380
}
381381
}
382382
for i, acl := range d.ACLList {
383-
s := SerializeACL(*acl)
384383
if err != nil {
385384
return err
386385
}
387-
if err = p.Insert(parser.Defaults, d.Name, "acl", s, i); err != nil {
386+
if err = p.Insert(parser.Defaults, d.Name, "acl", *acl, i); err != nil {
388387
return a.HandleError(strconv.FormatInt(int64(i), 10), DefaultsParentName, d.Name, a.TID, a.TID == "", err)
389388
}
390389
}

configuration/structured_fcgiapps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func serializeFCGIAppSection(a StructuredToParserArgs, f *models.FCGIApp) error
180180
return err
181181
}
182182
for i, acl := range f.ACLList {
183-
if err = p.Insert(parser.FCGIApp, f.Name, "acl", SerializeACL(*acl), i); err != nil {
183+
if err = p.Insert(parser.FCGIApp, f.Name, "acl", *acl, i); err != nil {
184184
return a.HandleError(strconv.FormatInt(int64(i), 10), "fcgi", f.Name, a.TID, a.TID == "", err)
185185
}
186186
}

configuration/structured_frontends.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func serializeFrontendSection(a StructuredToParserArgs, f *models.Frontend, opt
282282
}
283283
}
284284
for i, acl := range f.ACLList {
285-
if err = p.Insert(parser.Frontends, f.Name, "acl", SerializeACL(*acl), i); err != nil {
285+
if err = p.Insert(parser.Frontends, f.Name, "acl", *acl, i); err != nil {
286286
return a.HandleError(strconv.FormatInt(int64(i), 10), FrontendParentName, f.Name, a.TID, a.TID == "", err)
287287
}
288288
}

0 commit comments

Comments
 (0)