Skip to content

Commit

Permalink
FEATURE/MINOR: add GetResult next to GetPreComments to expose the Res…
Browse files Browse the repository at this point in the history
…ult output for individual parsers
  • Loading branch information
csabatuz-chess authored and oktalz committed May 11, 2024
1 parent 8eea621 commit 51b59a0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
15 changes: 15 additions & 0 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ func (p *configParser) Get(sectionType Section, sectionName string, attribute st
return section.Get(attribute, createNew)
}

// GetResult get attribute lines from section
func (p *configParser) GetResult(sectionType Section, sectionName string, attribute string) ([]common.ReturnResultLine, error) {
p.lock()
defer p.unLock()
st, ok := p.Parsers[sectionType]
if !ok {
return nil, errors.ErrSectionMissing
}
section, ok := st[sectionName]
if !ok {
return nil, errors.ErrSectionMissing
}
return section.GetResult(attribute)
}

// GetPreComments get attribute from section
func (p *configParser) GetPreComments(sectionType Section, sectionName string, attribute string) ([]string, error) {
p.lock()
Expand Down
11 changes: 11 additions & 0 deletions parser-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ func (p *Parsers) Get(attribute string, createIfNotExist ...bool) (common.Parser
return nil, errors.ErrParserMissing
}

func (p *Parsers) GetResult(attribute string) ([]common.ReturnResultLine, error) {
if parser, ok := p.Parsers[attribute]; ok {
lines, _, err := parser.ResultAll()
if err != nil {
return nil, errors.ErrParserMissing
}
return lines, nil
}
return nil, errors.ErrParserMissing
}

func (p *Parsers) GetPreComments(attribute string) ([]string, error) {
if parser, ok := p.Parsers[attribute]; ok {
return parser.GetPreComments()
Expand Down
1 change: 1 addition & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Parser interface {
Save(filename string) error
StringWithHash() (string, error)
Get(sectionType Section, sectionName string, attribute string, createIfNotExist ...bool) (common.ParserData, error)
GetResult(sectionType Section, sectionName string, attribute string) ([]common.ReturnResultLine, error)
GetPreComments(sectionType Section, sectionName string, attribute string) ([]string, error)
GetOne(sectionType Section, sectionName string, attribute string, index ...int) (common.ParserData, error)
SectionsGet(sectionType Section) ([]string, error)
Expand Down
49 changes: 49 additions & 0 deletions tests/configs/parse_fetch_lines_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2019 HAProxy Technologies
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package configs //nolint:testpackage

import (
"testing"

parser "github.com/haproxytech/config-parser/v5"
"github.com/haproxytech/config-parser/v5/options"
)

func TestParseFecthResultLines(t *testing.T) { //nolint:gocognit
tests := []struct {
Name, Config string
}{
{"configBasic1", configBasic1},
}
for _, config := range tests {
t.Run(config.Name, func(t *testing.T) {
p, err := parser.New(options.String(config.Config))
if err != nil {
t.Fatalf(err.Error())
}
lines, err := p.GetResult(parser.Frontends, "http", "bind")
if err != nil {
t.Fatalf(err.Error())
}
if lines[0].Data != "bind 0.0.0.0:80 name bind_1" {
t.Fatalf("Unexpected line: %s", lines[0].Data)
}
if lines[1].Data != "bind :::80 v4v6 name bind_2" {
t.Fatalf("Unexpected line: %s", lines[1].Data)
}
})
}
}

0 comments on commit 51b59a0

Please sign in to comment.