Skip to content

Commit

Permalink
BUG/MEDIUM: support multiple force-persist statements in backends
Browse files Browse the repository at this point in the history
  • Loading branch information
George Vine committed May 2, 2024
1 parent ab29ace commit cd272a0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 29 deletions.
30 changes: 16 additions & 14 deletions parsers/force-persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,38 @@ import (
)

type ForcePersist struct {
data *types.ForcePersist
data []types.ForcePersist
preComments []string // comments that appear before the actual line
}

func (m *ForcePersist) Parse(line string, parts []string, comment string) (string, error) {
func (m *ForcePersist) parse(line string, parts []string, comment string) (*types.ForcePersist, error) {
if len(parts) != 3 {
return "", &errors.ParseError{Parser: "ForcePersist", Line: line}
return nil, &errors.ParseError{Parser: "ForcePersist", Line: line}
}
if parts[0] == "force-persist" {
if parts[1] != "if" && parts[1] != "unless" {
return "", &errors.ParseError{Parser: "ForcePersist", Line: line}
return nil, &errors.ParseError{Parser: "ForcePersist", Line: line}
}
m.data = &types.ForcePersist{
data := &types.ForcePersist{
Cond: parts[1],
CondTest: parts[2],
Comment: comment,
}
return "", nil
return data, nil
}
return "", &errors.ParseError{Parser: "ForcePersist", Line: line}
return nil, &errors.ParseError{Parser: "ForcePersist", Line: line}
}

func (m *ForcePersist) Result() ([]common.ReturnResultLine, error) {
if m.data == nil {
if len(m.data) == 0 {
return nil, errors.ErrFetch
}
return []common.ReturnResultLine{
{
Data: fmt.Sprintf("force-persist %s %s", m.data.Cond, m.data.CondTest),
Comment: m.data.Comment,
},
}, nil
result := make([]common.ReturnResultLine, len(m.data))
for i := range m.data {
result[i] = common.ReturnResultLine{
Data: fmt.Sprintf("force-persist %s %s", m.data[i].Cond, m.data[i].CondTest),
Comment: m.data[i].Comment,
}
}
return result, nil
}
88 changes: 73 additions & 15 deletions parsers/force-persist_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/configs/haproxy_generated.cfg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,7 @@ type ProcessVary struct {

//sections:backend
//name:force-persist
//is:multiple
//test:ok:force-persist if acl-name
//test:ok:force-persist unless acl-name
//test:fail:force-persist
Expand Down

0 comments on commit cd272a0

Please sign in to comment.