From bd5c13c84b6c03d59e05a5bd61c7a6a23c1b452d Mon Sep 17 00:00:00 2001 From: Helene Durand Date: Tue, 20 Feb 2024 11:09:01 +0100 Subject: [PATCH] BUG/MINOR: compression direction both not supported --- parsers/compression-direction.go | 53 ++++++++++ parsers/compression-direction_generated.go | 99 +++++++++++++++++++ section-parsers.go | 3 + tests/compression-direction_generated_test.go | 82 +++++++++++++++ types/types-generic.go | 6 +- 5 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 parsers/compression-direction.go create mode 100644 parsers/compression-direction_generated.go create mode 100644 tests/compression-direction_generated_test.go diff --git a/parsers/compression-direction.go b/parsers/compression-direction.go new file mode 100644 index 0000000..01dde13 --- /dev/null +++ b/parsers/compression-direction.go @@ -0,0 +1,53 @@ +/* +Copyright 2022 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 parsers + +import ( + "fmt" + + "github.com/haproxytech/config-parser/v5/common" + "github.com/haproxytech/config-parser/v5/errors" + "github.com/haproxytech/config-parser/v5/types" +) + +type CompressionDirection struct { + data *types.StringC + preComments []string // comments that appear before the actual line +} + +func (c *CompressionDirection) Parse(line string, parts []string, comment string) (string, error) { + if len(parts) < 3 { + return "", &errors.ParseError{Parser: "CompressionDirection", Line: line, Message: "Parse error"} + } + c.data = &types.StringC{ + Value: parts[2], + Comment: comment, + } + return "", nil +} + +func (c *CompressionDirection) Result() ([]common.ReturnResultLine, error) { + if c.data == nil { + return nil, errors.ErrFetch + } + return []common.ReturnResultLine{ + { + Data: fmt.Sprintf("compression direction %s", c.data.Value), + Comment: c.data.Comment, + }, + }, nil +} diff --git a/parsers/compression-direction_generated.go b/parsers/compression-direction_generated.go new file mode 100644 index 0000000..cb01fef --- /dev/null +++ b/parsers/compression-direction_generated.go @@ -0,0 +1,99 @@ +// Code generated by go generate; DO NOT EDIT. +/* +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 parsers + +import ( + "github.com/haproxytech/config-parser/v5/common" + "github.com/haproxytech/config-parser/v5/errors" + "github.com/haproxytech/config-parser/v5/types" +) + +func (p *CompressionDirection) Init() { + p.data = nil + p.preComments = []string{} +} + +func (p *CompressionDirection) GetParserName() string { + return "compression direction" +} + +func (p *CompressionDirection) Get(createIfNotExist bool) (common.ParserData, error) { + if p.data == nil { + if createIfNotExist { + p.data = &types.StringC{} + return p.data, nil + } + return nil, errors.ErrFetch + } + return p.data, nil +} + +func (p *CompressionDirection) GetPreComments() ([]string, error) { + return p.preComments, nil +} + +func (p *CompressionDirection) SetPreComments(preComments []string) { + p.preComments = preComments +} + +func (p *CompressionDirection) GetOne(index int) (common.ParserData, error) { + if index > 0 { + return nil, errors.ErrFetch + } + if p.data == nil { + return nil, errors.ErrFetch + } + return p.data, nil +} + +func (p *CompressionDirection) Delete(index int) error { + p.Init() + return nil +} + +func (p *CompressionDirection) Insert(data common.ParserData, index int) error { + return p.Set(data, index) +} + +func (p *CompressionDirection) Set(data common.ParserData, index int) error { + if data == nil { + p.Init() + return nil + } + switch newValue := data.(type) { + case *types.StringC: + p.data = newValue + case types.StringC: + p.data = &newValue + default: + return errors.ErrInvalidData + } + return nil +} + +func (p *CompressionDirection) PreParse(line string, parts []string, preComments []string, comment string) (string, error) { + changeState, err := p.Parse(line, parts, comment) + if err == nil && preComments != nil { + p.preComments = append(p.preComments, preComments...) + } + return changeState, err +} + +func (p *CompressionDirection) ResultAll() ([]common.ReturnResultLine, []string, error) { + res, err := p.Result() + return res, p.preComments, err +} diff --git a/section-parsers.go b/section-parsers.go index 75e1afc..3302b4b 100644 --- a/section-parsers.go +++ b/section-parsers.go @@ -177,6 +177,7 @@ func (p *configParser) getDefaultParser() *Parsers { addParser(parser, &sequence, &parsers.CompressionAlgo{}) addParser(parser, &sequence, &parsers.CompressionType{}) addParser(parser, &sequence, &parsers.CompressionOffload{}) + addParser(parser, &sequence, &parsers.CompressionDirection{}) addParser(parser, &sequence, &parsers.DefaultServer{}) addParser(parser, &sequence, &parsers.LoadServerStateFromFile{}) addParser(parser, &sequence, &parsers.ErrorFile{}) @@ -566,6 +567,7 @@ func (p *configParser) getBackendParser() *Parsers { addParser(parser, &sequence, &parsers.CompressionAlgo{}) addParser(parser, &sequence, &parsers.CompressionType{}) addParser(parser, &sequence, &parsers.CompressionOffload{}) + addParser(parser, &sequence, &parsers.CompressionDirection{}) addParser(parser, &sequence, &tcp.Requests{}) addParser(parser, &sequence, &stats.Stats{Mode: "backend"}) addParser(parser, &sequence, &parsers.HTTPReuse{}) @@ -706,6 +708,7 @@ func (p *configParser) getListenParser() *Parsers { addParser(parser, &sequence, &parsers.CompressionAlgo{}) addParser(parser, &sequence, &parsers.CompressionType{}) addParser(parser, &sequence, &parsers.CompressionOffload{}) + addParser(parser, &sequence, &parsers.CompressionType{}) addParser(parser, &sequence, &tcp.Requests{}) addParser(parser, &sequence, &stats.Stats{Mode: "listen"}) addParser(parser, &sequence, &parsers.HTTPReuse{}) diff --git a/tests/compression-direction_generated_test.go b/tests/compression-direction_generated_test.go new file mode 100644 index 0000000..011991f --- /dev/null +++ b/tests/compression-direction_generated_test.go @@ -0,0 +1,82 @@ +// Code generated by go generate; DO NOT EDIT. +/* +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 tests + +import ( + "fmt" + "strings" + "testing" + + "github.com/haproxytech/config-parser/v5/parsers" +) + +func TestCompressionDirection(t *testing.T) { + tests := map[string]bool{ + "compression direction both": true, + "compression direction": false, + "---": false, + "--- ---": false, + } + parser := &parsers.CompressionDirection{} + for command, shouldPass := range tests { + t.Run(command, func(t *testing.T) { + line := strings.TrimSpace(command) + lines := strings.SplitN(line, "\n", -1) + var err error + parser.Init() + if len(lines) > 1 { + for _, line = range lines { + line = strings.TrimSpace(line) + if err = ProcessLine(line, parser); err != nil { + break + } + } + } else { + err = ProcessLine(line, parser) + } + if shouldPass { + if err != nil { + t.Errorf(err.Error()) + return + } + result, err := parser.Result() + if err != nil { + t.Errorf(err.Error()) + return + } + var returnLine string + if result[0].Comment == "" { + returnLine = result[0].Data + } else { + returnLine = fmt.Sprintf("%s # %s", result[0].Data, result[0].Comment) + } + if command != returnLine { + t.Errorf(fmt.Sprintf("error: has [%s] expects [%s]", returnLine, command)) + } + } else { + if err == nil { + t.Errorf(fmt.Sprintf("error: did not throw error for line [%s]", line)) + } + _, parseErr := parser.Result() + if parseErr == nil { + t.Errorf(fmt.Sprintf("error: did not throw error on result for line [%s]", line)) + } + } + }) + } +} diff --git a/types/types-generic.go b/types/types-generic.go index 83a57b5..7f84db2 100644 --- a/types/types-generic.go +++ b/types/types-generic.go @@ -77,7 +77,7 @@ type Int64C struct { Comment string } -// String is used by parsers Mode, DefaultBackend, SimpleTimeTwoWords, StatsTimeout +// String is used by parsers Mode, DefaultBackend, SimpleTimeTwoWords, StatsTimeout, CompressionDirection // //generate:type:Mode //name:mode @@ -100,6 +100,10 @@ type Int64C struct { //name:log-send-hostname //test:ok:log-send-hostname //test:ok:log-send-hostname something +//generate:type:CompressionDirection +//name:compression direction +//test:ok:compression direction both +//test:fail:compression direction type StringC struct { Value string Comment string