Skip to content

Commit 32e9bca

Browse files
csabatuz-chessoktalz
authored andcommitted
FEATURE/MINOR: add SectionAttributesGet to Parser interface to be able to list available / existing attributes in a section
1 parent f03e520 commit 32e9bca

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

fetch.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ func (p *configParser) SectionsGet(sectionType Section) ([]string, error) {
118118
return result, nil
119119
}
120120

121+
// SectionAttributesGet lists all attributes in the section of certain type and name
122+
func (p *configParser) SectionAttributesGet(sectionType Section, sectionName string, existingOnly bool) ([]string, error) {
123+
p.lock()
124+
defer p.unLock()
125+
st, ok := p.Parsers[sectionType]
126+
if !ok {
127+
return nil, errors.ErrSectionMissing
128+
}
129+
section, ok := st[sectionName]
130+
if !ok {
131+
return nil, errors.ErrSectionMissing
132+
}
133+
return section.GetAttributes(existingOnly), nil
134+
}
135+
121136
// SectionsDelete deletes one section of sectionType
122137
func (p *configParser) SectionsDelete(sectionType Section, sectionName string) error {
123138
p.lock()

parser-type.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package parser
1919
import (
2020
"github.com/haproxytech/config-parser/v5/common"
2121
"github.com/haproxytech/config-parser/v5/errors"
22+
"github.com/haproxytech/config-parser/v5/parsers/extra"
2223
)
2324

2425
type ParserInterface interface { //nolint:revive
@@ -44,6 +45,29 @@ type Parsers struct {
4445
DefaultSectionName string
4546
}
4647

48+
func (p *Parsers) GetAttributes(existingOnly bool) []string {
49+
keys := make([]string, len(p.Parsers))
50+
i := 0
51+
for _, section := range p.ParserSequence {
52+
attribute := string(section)
53+
if attribute == "" {
54+
continue
55+
}
56+
if _, ok := p.Parsers[attribute].(*extra.Section); ok {
57+
continue
58+
}
59+
if existingOnly {
60+
val, err := p.Parsers[attribute].Get(false)
61+
if err != nil || val == nil {
62+
continue
63+
}
64+
}
65+
keys[i] = attribute
66+
i++
67+
}
68+
return keys[:i]
69+
}
70+
4771
func (p *Parsers) Get(attribute string, createIfNotExist ...bool) (common.ParserData, error) {
4872
createNew := false
4973
if len(createIfNotExist) > 0 && createIfNotExist[0] {

parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type Parser interface {
7171
GetPreComments(sectionType Section, sectionName string, attribute string) ([]string, error)
7272
GetOne(sectionType Section, sectionName string, attribute string, index ...int) (common.ParserData, error)
7373
SectionsGet(sectionType Section) ([]string, error)
74+
SectionAttributesGet(sectionType Section, sectionName string, existingOnly bool) ([]string, error)
7475
SectionsDelete(sectionType Section, sectionName string) error
7576
SectionsCreate(sectionType Section, sectionName string) error
7677
SectionsDefaultsFromGet(sectionType Section, sectionName string) (string, error)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
package configs //nolint:testpackage
17+
18+
import (
19+
"slices"
20+
"strings"
21+
"testing"
22+
23+
parser "github.com/haproxytech/config-parser/v5"
24+
"github.com/haproxytech/config-parser/v5/options"
25+
)
26+
27+
func TestParseGetAttributesExistingOnly(t *testing.T) {
28+
tests := []struct {
29+
Name, Config string
30+
}{
31+
{"configBasic1", configBasic1},
32+
}
33+
for _, config := range tests {
34+
t.Run(config.Name, func(t *testing.T) {
35+
p, err := parser.New(options.String(config.Config))
36+
if err != nil {
37+
t.Fatalf(err.Error())
38+
}
39+
result, err := p.SectionAttributesGet(parser.Frontends, "http", true)
40+
if err != nil {
41+
t.Fatalf("err should be nil %v", err)
42+
}
43+
if 0 != slices.Compare(result, []string{
44+
"mode", "bind", "default_backend",
45+
}) {
46+
t.Fatalf("retrieved attributes do not match %s", strings.Join(result, ", "))
47+
}
48+
})
49+
}
50+
}
51+
52+
func TestParseGetAttributesComplete(t *testing.T) {
53+
tests := []struct {
54+
Name, Config string
55+
}{
56+
{"configBasic1", configBasic1},
57+
}
58+
for _, config := range tests {
59+
t.Run(config.Name, func(t *testing.T) {
60+
p, err := parser.New(options.String(config.Config))
61+
if err != nil {
62+
t.Fatalf(err.Error())
63+
}
64+
result, err := p.SectionAttributesGet(parser.Backends, "default_backend", false)
65+
if err != nil {
66+
t.Fatalf("err should be nil %v", err)
67+
}
68+
if !slices.Contains(result, "option tcp-smart-connect") {
69+
t.Fatalf("should contain registered attribute 'option tcp-smart-connect'")
70+
}
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)