diff --git a/parsers/ignore-persist.go b/parsers/ignore-persist.go index c676656..b7bdde2 100644 --- a/parsers/ignore-persist.go +++ b/parsers/ignore-persist.go @@ -24,36 +24,38 @@ import ( ) type IgnorePersist struct { - data *types.IgnorePersist + data []types.IgnorePersist preComments []string // comments that appear before the actual line } -func (m *IgnorePersist) Parse(line string, parts []string, comment string) (string, error) { +func (m *IgnorePersist) parse(line string, parts []string, comment string) (*types.IgnorePersist, error) { if len(parts) != 3 { - return "", &errors.ParseError{Parser: "IgnorePersist", Line: line} + return nil, &errors.ParseError{Parser: "IgnorePersist", Line: line} } if parts[0] == "ignore-persist" { if parts[1] != "if" && parts[1] != "unless" { - return "", &errors.ParseError{Parser: "IgnorePersist", Line: line} + return nil, &errors.ParseError{Parser: "IgnorePersist", Line: line} } - m.data = &types.IgnorePersist{ + data := &types.IgnorePersist{ Cond: parts[1], CondTest: parts[2], Comment: comment, } - return "", nil + return data, nil } - return "", &errors.ParseError{Parser: "IgnorePersist", Line: line} + return nil, &errors.ParseError{Parser: "IgnorePersist", Line: line} } func (m *IgnorePersist) Result() ([]common.ReturnResultLine, error) { - if m.data == nil { + if len(m.data) == 0 { return nil, errors.ErrFetch } - return []common.ReturnResultLine{ - { - Data: fmt.Sprintf("ignore-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("ignore-persist %s %s", m.data[i].Cond, m.data[i].CondTest), + Comment: m.data[i].Comment, + } + } + return result, nil } diff --git a/parsers/ignore-persist_generated.go b/parsers/ignore-persist_generated.go index badf873..fb2743d 100644 --- a/parsers/ignore-persist_generated.go +++ b/parsers/ignore-persist_generated.go @@ -23,7 +23,7 @@ import ( ) func (p *IgnorePersist) Init() { - p.data = nil + p.data = []types.IgnorePersist{} p.preComments = []string{} } @@ -32,11 +32,7 @@ func (p *IgnorePersist) GetParserName() string { } func (p *IgnorePersist) Get(createIfNotExist bool) (common.ParserData, error) { - if p.data == nil { - if createIfNotExist { - p.data = &types.IgnorePersist{} - return p.data, nil - } + if len(p.data) == 0 && !createIfNotExist { return nil, errors.ErrFetch } return p.data, nil @@ -51,22 +47,55 @@ func (p *IgnorePersist) SetPreComments(preComments []string) { } func (p *IgnorePersist) GetOne(index int) (common.ParserData, error) { - if index > 0 { - return nil, errors.ErrFetch - } - if p.data == nil { + if index < 0 || index >= len(p.data) { return nil, errors.ErrFetch } - return p.data, nil + return p.data[index], nil } func (p *IgnorePersist) Delete(index int) error { - p.Init() + if index < 0 || index >= len(p.data) { + return errors.ErrFetch + } + copy(p.data[index:], p.data[index+1:]) + p.data[len(p.data)-1] = types.IgnorePersist{} + p.data = p.data[:len(p.data)-1] return nil } func (p *IgnorePersist) Insert(data common.ParserData, index int) error { - return p.Set(data, index) + if data == nil { + return errors.ErrInvalidData + } + switch newValue := data.(type) { + case []types.IgnorePersist: + p.data = newValue + case *types.IgnorePersist: + if index > -1 { + if index > len(p.data) { + return errors.ErrIndexOutOfRange + } + p.data = append(p.data, types.IgnorePersist{}) + copy(p.data[index+1:], p.data[index:]) + p.data[index] = *newValue + } else { + p.data = append(p.data, *newValue) + } + case types.IgnorePersist: + if index > -1 { + if index > len(p.data) { + return errors.ErrIndexOutOfRange + } + p.data = append(p.data, types.IgnorePersist{}) + copy(p.data[index+1:], p.data[index:]) + p.data[index] = newValue + } else { + p.data = append(p.data, newValue) + } + default: + return errors.ErrInvalidData + } + return nil } func (p *IgnorePersist) Set(data common.ParserData, index int) error { @@ -75,10 +104,24 @@ func (p *IgnorePersist) Set(data common.ParserData, index int) error { return nil } switch newValue := data.(type) { - case *types.IgnorePersist: + case []types.IgnorePersist: p.data = newValue + case *types.IgnorePersist: + if index > -1 && index < len(p.data) { + p.data[index] = *newValue + } else if index == -1 { + p.data = append(p.data, *newValue) + } else { + return errors.ErrIndexOutOfRange + } case types.IgnorePersist: - p.data = &newValue + if index > -1 && index < len(p.data) { + p.data[index] = newValue + } else if index == -1 { + p.data = append(p.data, newValue) + } else { + return errors.ErrIndexOutOfRange + } default: return errors.ErrInvalidData } @@ -93,6 +136,21 @@ func (p *IgnorePersist) PreParse(line string, parts []string, preComments []stri return changeState, err } +func (p *IgnorePersist) Parse(line string, parts []string, comment string) (string, error) { + if parts[0] == "ignore-persist" { + data, err := p.parse(line, parts, comment) + if err != nil { + if _, ok := err.(*errors.ParseError); ok { + return "", err + } + return "", &errors.ParseError{Parser: "IgnorePersist", Line: line} + } + p.data = append(p.data, *data) + return "", nil + } + return "", &errors.ParseError{Parser: "IgnorePersist", Line: line} +} + func (p *IgnorePersist) ResultAll() ([]common.ReturnResultLine, []string, error) { res, err := p.Result() return res, p.preComments, err diff --git a/tests/configs/haproxy_generated.cfg.go b/tests/configs/haproxy_generated.cfg.go index 99a79ca..4bd020b 100644 --- a/tests/configs/haproxy_generated.cfg.go +++ b/tests/configs/haproxy_generated.cfg.go @@ -415,6 +415,7 @@ backend test server-template srv 3 google.com force-persist if acl-name ignore-persist if acl-name + ignore-persist unless acl-name http-send-name-header option http-restrict-req-hdr-names preserve source 192.168.1.200 @@ -3328,6 +3329,8 @@ var configTests = []configTest{{` command spoa-mirror --runtime 0 --mirror-url {` force-persist if acl-name `, 1}, {` ignore-persist if acl-name +`, 1}, + {` ignore-persist unless acl-name `, 1}, {` unix-bind prefix pre `, 1}, diff --git a/types/types.go b/types/types.go index 63ce68e..ad8fe31 100644 --- a/types/types.go +++ b/types/types.go @@ -1368,6 +1368,7 @@ type ForcePersist struct { //sections:backend //name:ignore-persist +//is:multiple //test:ok:ignore-persist if acl-name //test:ok:ignore-persist unless acl-name //test:fail:ignore-persist