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

Resolves #32 - Add support for URL Encoded filters #33

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; https://editorconfig.org/
root = true
; Primarily to fix the tab width of GitHub

[{go.mod,go.sum,*.go,.gitmodules}]
indent_style = tab
indent_size = 4
18 changes: 16 additions & 2 deletions external/epsearchast/v3/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package epsearchast_v3
import (
"encoding/json"
"fmt"
"net/url"
"strings"
)

Expand All @@ -21,8 +22,21 @@ func GetAst(jsonTxt string) (*AstNode, error) {
err := json.Unmarshal([]byte(jsonTxt), astNode)

if err != nil {
return nil, fmt.Errorf("could not parse filter:%w", err)
} else if err := astNode.checkValid(); err != nil {
// url decode jsonTxt
decoded, urlDecodingError := url.QueryUnescape(jsonTxt)

if urlDecodingError == nil {
urlDecodingError = json.Unmarshal([]byte(decoded), astNode)

if urlDecodingError != nil {
return nil, fmt.Errorf("could not parse filter:%w, error parsing decoded filter: %v", err, urlDecodingError)
}
} else {
return nil, fmt.Errorf("could not parse filter:%w, error decoding: %v", err, urlDecodingError)
}
}

if err := astNode.checkValid(); err != nil {
return nil, fmt.Errorf("error validating filter:%w", err)
} else {
return astNode, nil
Expand Down
45 changes: 45 additions & 0 deletions external/epsearchast/v3/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package epsearchast_v3

import (
"github.com/stretchr/testify/require"
"net/url"
"testing"
)

Expand Down Expand Up @@ -283,3 +284,47 @@ func TestAndReturnsErrorWithAnInvalidChild(t *testing.T) {
require.ErrorContains(t, err, "unknown operator FOO")
require.Nil(t, astNode)
}

func TestValidObjectThatIsUrlEncodedReturnsAst(t *testing.T) {
// Fixture Setup
// language=JSON
jsonTxt := `
{
"type": "EQ",
"args": [ "status", "paid"]
}
`
// Execute SUT
astNode, err := GetAst(url.QueryEscape(jsonTxt))

// Verify
require.NoError(t, err)
require.NotNil(t, astNode)
}

func TestInValidObjectThatIsUrlEncodedReturnsError(t *testing.T) {
// Fixture Setup
jsonTxt := `
{
"type": "EQ",
"args": [ "status", "paid"]
`
// Execute SUT
astNode, err := GetAst(url.QueryEscape(jsonTxt))

// Verify
require.Error(t, err)
require.ErrorContains(t, err, "unexpected end of JSON input")
require.Nil(t, astNode)
}

func TestInValidUrlEncodingReturnsError(t *testing.T) {
// Fixture Setup
// Execute SUT
astNode, err := GetAst("%4")

// Verify
require.Error(t, err)
require.ErrorContains(t, err, "invalid URL escape")
require.Nil(t, astNode)
}
Loading