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(selection): handle multiple objects inner parenthesis #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 22 additions & 7 deletions selection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jsonmask

import (
"errors"
"fmt"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -32,7 +33,6 @@ func (s Selection) equal(other Selection) bool {
// a(b,c) sub-selection will select many fields from a parent
// a/*/c the star * wildcard will select all items in a field
// a,b/c(d,e(f,g/h)),i
//
func Compile(str string) (Selection, error) {
if !utf8.ValidString(str) {
return nil, fmt.Errorf("invalid fields")
Expand Down Expand Up @@ -103,6 +103,7 @@ func Compile(str string) (Selection, error) {

node := make(Selection)
err := buildSelection(tokens, node)

return node, err
}

Expand All @@ -120,7 +121,10 @@ func buildSelection(tokens []string, root Selection) error {
case "/":
node = child
case "(":
end := findCloseIndex(tokens, i+1)
end, err := findCloseIndex(tokens, i)
if err != nil {
return err
}
if end == -1 {
return fmt.Errorf("sub-selector not close")
}
Expand All @@ -138,11 +142,22 @@ func buildSelection(tokens []string, root Selection) error {
return nil
}

func findCloseIndex(tokens []string, start int) int {
for i := len(tokens) - 1; i >= start; i-- {
if tokens[i] == ")" {
return i
func findCloseIndex(tokens []string, start int) (int, error) {
closeIdx := start
counter := 1
for {
if counter == 0 {
return closeIdx, nil
}
closeIdx++
if closeIdx > (len(tokens) - 1) {
return 0, errors.New("unbalanced parenthesis")
}
switch tokens[closeIdx] {
case "(":
counter++
case ")":
counter--
}
}
return -1
}
96 changes: 86 additions & 10 deletions selection_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package jsonmask

import "testing"
import (
"fmt"
"testing"
)

type selectionCase struct {
fields string
Expand Down Expand Up @@ -133,19 +136,92 @@ var selectionCases = []selectionCase{
"i": Selection{},
},
},
{
fields: "a,b/c(d(e,f),g(h,i/j)),k",
shouldErr: false,
res: Selection{
"a": Selection{},
"b": Selection{"c": Selection{
"d": Selection{
"e": Selection{},
"f": Selection{},
},
"g": Selection{
"h": Selection{},
"i": Selection{"j": Selection{}},
},
}},
"k": Selection{},
},
},
{
fields: "a,b/c(d(e,f),g(h,i/j(k,l))),m",
shouldErr: false,
res: Selection{
"a": Selection{},
"b": Selection{
"c": Selection{
"d": Selection{
"e": Selection{},
"f": Selection{},
},
"g": Selection{
"h": Selection{},
"i": Selection{"j": Selection{
"k": Selection{},
"l": Selection{},
}},
},
},
},
"m": Selection{},
},
},
{
fields: "a,b/c(d(e,f),g(h,i/j(k(l),m(n,o/p)))),q",
shouldErr: false,
res: Selection{
"a": Selection{},
"b": Selection{
"c": Selection{
"d": Selection{
"e": Selection{},
"f": Selection{},
},
"g": Selection{
"h": Selection{},
"i": Selection{"j": Selection{
"k": Selection{
"l": Selection{},
},
"m": Selection{
"n": Selection{},
"o": Selection{
"p": Selection{},
},
},
}},
},
},
},
"q": Selection{},
},
},
}

func TestCompile(t *testing.T) {
for _, c := range selectionCases {
res, err := Compile(c.fields)
if c.shouldErr {
if err == nil {
t.Errorf("Testing case[%s] failed: should error but got: %#v", c.fields, res)
t.Run(fmt.Sprint("fields=[", c.fields, "]"), func(t *testing.T) {
res, err := Compile(c.fields)
if c.shouldErr {
if err == nil {
t.Errorf("Testing case[%s] failed: should error but got: %#v", c.fields, res)
}
} else if err != nil {
t.Errorf("Testing case[%s] failed: %s", c.fields, err)
} else if !c.res.equal(res) {
t.Errorf("Testing case[%s] failed, expected: %#v, got: %#v", c.fields, c.res, res)
}
} else if err != nil {
t.Errorf("Testing case[%s] failed: %s", c.fields, err)
} else if !c.res.equal(res) {
t.Errorf("Testing case[%s] failed, expected: %#v, got: %#v", c.fields, c.res, res)
}
})
}
}