Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace character slice with regex replace #250

Merged
merged 9 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -113,9 +113,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