Skip to content

Commit

Permalink
fix: replace character slice with regex replace (#250)
Browse files Browse the repository at this point in the history
Signed-off-by: James Milligan <james@omnant.co.uk>

<!-- Please use this template for your pull request. -->
<!-- Please use the sections that you need and delete other sections -->

## This PR
<!-- add the description of the PR here -->

- fixes transpose evaluator state parsing when state contains trailing
characters

### Related Issues
<!-- add here the GitHub issue that this PR resolves if applicable -->

#244

### Notes
<!-- any additional notes for this PR -->
switches from a hard coded `1:n-2` slice to use regex to match and
replace the first `{` and last `}` in the string.
### Follow-up Tasks
<!-- anything that is related to this PR but not done here should be
noted under this section -->
<!-- if there is a need for a new issue, please link it here -->

### How to test
<!-- if applicable, add testing instructions under this section -->

Signed-off-by: James Milligan <james@omnant.co.uk>
Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
  • Loading branch information
james-milligan and beeme1mr authored Jan 12, 2023
1 parent ca22541 commit c92d101
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
9 changes: 7 additions & 2 deletions pkg/eval/json_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import (
"google.golang.org/protobuf/types/known/structpb"
)

var regBrace *regexp.Regexp

func init() {
regBrace = regexp.MustCompile("^[^{]*{|}[^}]*$")
}

type JSONEvaluator struct {
state Flags
Logger *logger.Logger
Expand Down Expand Up @@ -252,8 +258,7 @@ func (je *JSONEvaluator) transposeEvaluators(state string) (string, error) {
if len(evalValue) < 3 {
return "", errors.New("evaluator object is empty")
}
evalValue = evalValue[1 : len(evalValue)-2] // remove first { and last }

evalValue = regBrace.ReplaceAllString(evalValue, "")
state = regex.ReplaceAllString(state, evalValue)
}

Expand Down
58 changes: 58 additions & 0 deletions pkg/eval/json_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,64 @@ func TestState_Evaluator(t *testing.T) {
}
`,
},
"no-indentation": {
inputState: `
{
"flags": {
"fibAlgo": {
"variants": {
"recursive": "recursive",
"memo": "memo",
"loop": "loop",
"binet": "binet"
},
"defaultVariant": "recursive",
"state": "ENABLED",
"targeting": {
"if": [
{
"$ref": "emailWithFaas"
}, "binet", null
]
}
}
},
"$evaluators": {
"emailWithFaas": {
"in": ["@faas.com", {
"var": ["email"]
}]
}
}
}
`,
expectedOutputState: `
{
"flags": {
"fibAlgo": {
"variants": {
"recursive": "recursive",
"memo": "memo",
"loop": "loop",
"binet": "binet"
},
"defaultVariant": "recursive",
"state": "ENABLED",
"source":"",
"targeting": {
"if": [
{
"in": ["@faas.com", {
"var": ["email"]
}]
}, "binet", null
]
}
}
}
}
`,
},
"invalid evaluator json": {
inputState: `
{
Expand Down
4 changes: 1 addition & 3 deletions pkg/sync/filepath_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ func yamlToJSON(rawFile []byte) (string, error) {
return "", fmt.Errorf("unmarshal yaml: %w", err)
}

// Adding spaces here because our evaluator transposer function
// doesn't understand json without indentations quite well
r, err := json.MarshalIndent(ms, "", " ")
r, err := json.Marshal(ms)
if err != nil {
return "", fmt.Errorf("convert yaml to json: %w", err)
}
Expand Down

0 comments on commit c92d101

Please sign in to comment.