Skip to content

Commit

Permalink
MINOR: add support for sc-set-gpt(sc-id,idx)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliwer committed Mar 26, 2024
1 parent a6f26e4 commit 81a75a8
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 0 deletions.
118 changes: 118 additions & 0 deletions parsers/actions/sc-set-gpt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
Copyright 2024 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 actions

import (
"fmt"
"strconv"
"strings"

"github.com/haproxytech/config-parser/v5/common"
"github.com/haproxytech/config-parser/v5/errors"
"github.com/haproxytech/config-parser/v5/types"
)

type ScSetGpt struct {
ScID string
Idx int64
Int *int64
Expr common.Expression
Cond string
CondTest string
Comment string
}

func (f *ScSetGpt) Parse(parts []string, parserType types.ParserType, comment string) error {
if comment != "" {
f.Comment = comment
}
if len(parts) < 3 {
return fmt.Errorf("not enough params")
}
var data string
var command []string
switch parserType {
case types.HTTP:
data = parts[1]
command = parts[2:]
case types.TCP:
data = parts[2]
command = parts[3:]
}

// sc-get-gpt(sc-id,idx)
start := len("sc-set-gpt(")
end := len(data) - 1 // ignore ")"
idIdx := strings.Split(data[start:end], ",")
if len(idIdx) != 2 {
return fmt.Errorf("missing sc-id and/or idx")
}
var err error
f.ScID = idIdx[0]
f.Idx, err = strconv.ParseInt(idIdx[1], 10, 64)
if err != nil {
return fmt.Errorf("failed to parse idx: %w", err)
}

// { <int> | <expr> } [ { if | unless } <condition> ]
command, condition := common.SplitRequest(command)
if len(command) < 1 {
return errors.ErrInvalidData
}
i, err := strconv.ParseInt(command[0], 10, 64)
if err == nil {
f.Int = &i
} else {
expr := common.Expression{}
err := expr.Parse(command)
if err != nil {
return fmt.Errorf("not enough params")
}
f.Expr = expr
}
if len(condition) > 1 {
f.Cond = condition[0]
f.CondTest = strings.Join(condition[1:], " ")
}
return nil
}

func (f *ScSetGpt) String() string {
var result strings.Builder
result.Grow(64)
result.WriteString("sc-set-gpt(")
result.WriteString(f.ScID)
result.WriteByte(',')
result.WriteString(strconv.FormatInt(f.Idx, 10))
result.WriteString(") ")
if f.Int != nil {
result.WriteString(strconv.FormatInt(*f.Int, 10))
} else {
result.WriteString(f.Expr.String())
}
if f.Cond != "" {
result.WriteString(" ")
result.WriteString(f.Cond)
result.WriteString(" ")
result.WriteString(f.CondTest)
}
return result.String()
}

func (f *ScSetGpt) GetComment() string {
return f.Comment
}
2 changes: 2 additions & 0 deletions parsers/http/http-after-response.go

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

2 changes: 2 additions & 0 deletions parsers/http/http-request.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ func (h *Requests) Parse(line string, parts []string, comment string) (string, e
err = h.ParseHTTPRequest(&actions.ScIncGpc0{}, parts, comment)
case strings.HasPrefix(parts[1], "sc-inc-gpc1("):
err = h.ParseHTTPRequest(&actions.ScIncGpc1{}, parts, comment)
case strings.HasPrefix(parts[1], "sc-set-gpt("):
err = h.ParseHTTPRequest(&actions.ScSetGpt{}, parts, comment)
case strings.HasPrefix(parts[1], "sc-set-gpt0("):
err = h.ParseHTTPRequest(&actions.ScSetGpt0{}, parts, comment)
case strings.HasPrefix(parts[1], "set-var("):
Expand Down
2 changes: 2 additions & 0 deletions parsers/http/http-response.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func (h *Responses) Parse(line string, parts []string, comment string) (string,
err = h.ParseHTTPResponse(&actions.ScIncGpc0{}, parts, comment)
case strings.HasPrefix(parts[1], "sc-inc-gpc1("):
err = h.ParseHTTPResponse(&actions.ScIncGpc1{}, parts, comment)
case strings.HasPrefix(parts[1], "sc-set-gpt("):
err = h.ParseHTTPResponse(&actions.ScSetGpt{}, parts, comment)
case strings.HasPrefix(parts[1], "sc-set-gpt0("):
err = h.ParseHTTPResponse(&actions.ScSetGpt0{}, parts, comment)
case strings.HasPrefix(parts[1], "set-map("):
Expand Down
2 changes: 2 additions & 0 deletions parsers/tcp/types/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func (f *Connection) Parse(parts []string, comment string) error {
err = f.ParseAction(&actions.ScIncGpc0{}, parts)
case strings.HasPrefix(parts[2], "sc-inc-gpc1("):
err = f.ParseAction(&actions.ScIncGpc1{}, parts)
case strings.HasPrefix(parts[2], "sc-set-gpt("):
err = f.ParseAction(&actions.ScSetGpt{}, parts)
case strings.HasPrefix(parts[2], "sc-set-gpt0"):
err = f.ParseAction(&actions.ScSetGpt0{}, parts)
case strings.HasPrefix(parts[2], "unset-var"):
Expand Down
2 changes: 2 additions & 0 deletions parsers/tcp/types/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func (f *Content) Parse(parts []string, comment string) error { //nolint:gocyclo
err = f.ParseAction(&actions.ScIncGpc0{}, parts)
case strings.HasPrefix(parts[2], "sc-inc-gpc1("):
err = f.ParseAction(&actions.ScIncGpc1{}, parts)
case strings.HasPrefix(parts[2], "sc-set-gpt("):
err = f.ParseAction(&actions.ScSetGpt{}, parts)
case strings.HasPrefix(parts[2], "sc-set-gpt0"):
err = f.ParseAction(&actions.ScSetGpt0{}, parts)
case strings.HasPrefix(parts[2], "set-var("):
Expand Down
2 changes: 2 additions & 0 deletions parsers/tcp/types/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func (f *Session) Parse(parts []string, comment string) error {
err = f.ParseAction(&actions.ScIncGpc0{}, parts)
case strings.HasPrefix(parts[2], "sc-inc-gpc1("):
err = f.ParseAction(&actions.ScIncGpc1{}, parts)
case strings.HasPrefix(parts[2], "sc-set-gpt("):
err = f.ParseAction(&actions.ScSetGpt{}, parts)
case strings.HasPrefix(parts[2], "sc-set-gpt0"):
err = f.ParseAction(&actions.ScSetGpt0{}, parts)
case strings.HasPrefix(parts[2], "set-var("):
Expand Down
24 changes: 24 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 tests/http-after-response_generated_test.go

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

1 change: 1 addition & 0 deletions tests/http-request_generated_test.go

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

1 change: 1 addition & 0 deletions tests/http-response_generated_test.go

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

Loading

0 comments on commit 81a75a8

Please sign in to comment.