Skip to content
This repository was archived by the owner on Sep 1, 2025. It is now read-only.

Commit 51b59a0

Browse files
csabatuz-chessoktalz
authored andcommitted
FEATURE/MINOR: add GetResult next to GetPreComments to expose the Result output for individual parsers
1 parent 8eea621 commit 51b59a0

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

fetch.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ func (p *configParser) Get(sectionType Section, sectionName string, attribute st
5252
return section.Get(attribute, createNew)
5353
}
5454

55+
// GetResult get attribute lines from section
56+
func (p *configParser) GetResult(sectionType Section, sectionName string, attribute string) ([]common.ReturnResultLine, error) {
57+
p.lock()
58+
defer p.unLock()
59+
st, ok := p.Parsers[sectionType]
60+
if !ok {
61+
return nil, errors.ErrSectionMissing
62+
}
63+
section, ok := st[sectionName]
64+
if !ok {
65+
return nil, errors.ErrSectionMissing
66+
}
67+
return section.GetResult(attribute)
68+
}
69+
5570
// GetPreComments get attribute from section
5671
func (p *configParser) GetPreComments(sectionType Section, sectionName string, attribute string) ([]string, error) {
5772
p.lock()

parser-type.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ func (p *Parsers) Get(attribute string, createIfNotExist ...bool) (common.Parser
5656
return nil, errors.ErrParserMissing
5757
}
5858

59+
func (p *Parsers) GetResult(attribute string) ([]common.ReturnResultLine, error) {
60+
if parser, ok := p.Parsers[attribute]; ok {
61+
lines, _, err := parser.ResultAll()
62+
if err != nil {
63+
return nil, errors.ErrParserMissing
64+
}
65+
return lines, nil
66+
}
67+
return nil, errors.ErrParserMissing
68+
}
69+
5970
func (p *Parsers) GetPreComments(attribute string) ([]string, error) {
6071
if parser, ok := p.Parsers[attribute]; ok {
6172
return parser.GetPreComments()

parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type Parser interface {
6666
Save(filename string) error
6767
StringWithHash() (string, error)
6868
Get(sectionType Section, sectionName string, attribute string, createIfNotExist ...bool) (common.ParserData, error)
69+
GetResult(sectionType Section, sectionName string, attribute string) ([]common.ReturnResultLine, error)
6970
GetPreComments(sectionType Section, sectionName string, attribute string) ([]string, error)
7071
GetOne(sectionType Section, sectionName string, attribute string, index ...int) (common.ParserData, error)
7172
SectionsGet(sectionType Section) ([]string, error)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
"testing"
20+
21+
parser "github.com/haproxytech/config-parser/v5"
22+
"github.com/haproxytech/config-parser/v5/options"
23+
)
24+
25+
func TestParseFecthResultLines(t *testing.T) { //nolint:gocognit
26+
tests := []struct {
27+
Name, Config string
28+
}{
29+
{"configBasic1", configBasic1},
30+
}
31+
for _, config := range tests {
32+
t.Run(config.Name, func(t *testing.T) {
33+
p, err := parser.New(options.String(config.Config))
34+
if err != nil {
35+
t.Fatalf(err.Error())
36+
}
37+
lines, err := p.GetResult(parser.Frontends, "http", "bind")
38+
if err != nil {
39+
t.Fatalf(err.Error())
40+
}
41+
if lines[0].Data != "bind 0.0.0.0:80 name bind_1" {
42+
t.Fatalf("Unexpected line: %s", lines[0].Data)
43+
}
44+
if lines[1].Data != "bind :::80 v4v6 name bind_2" {
45+
t.Fatalf("Unexpected line: %s", lines[1].Data)
46+
}
47+
})
48+
}
49+
}

0 commit comments

Comments
 (0)