Skip to content

Commit

Permalink
Merge pull request #40 from thedevsaddam/fix/separator
Browse files Browse the repository at this point in the history
Fix separator
  • Loading branch information
thedevsaddam authored Feb 20, 2019
2 parents d9e26eb + e964115 commit 5ac255c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
8 changes: 4 additions & 4 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *sortMap) Less(i, j int) (res bool) {
y := list.Index(j).Interface()

// compare nested values
if strings.Contains(s.key, ".") {
if strings.Contains(s.key, s.separator) {
xv, errX := getNestedValue(x, s.key, s.separator)
if errX != nil {
s.errs = append(s.errs, errX)
Expand Down Expand Up @@ -232,7 +232,7 @@ func getNestedValue(input interface{}, node, separator string) (interface{}, err

// makeAlias provide syntactic suger. when provide Property name as "user.name as userName"
// it return userName as output and pure node name like: "user.name". If "user.name" does not use "as" clause then it'll return "user.name", "user.name"
func makeAlias(in string) (string, string) {
func makeAlias(in, separator string) (string, string) {
const alias = " as "
in = strings.Replace(in, " As ", alias, -1)
in = strings.Replace(in, " AS ", alias, -1)
Expand All @@ -242,8 +242,8 @@ func makeAlias(in string) (string, string) {
return strings.TrimSpace(ss[0]), strings.TrimSpace(ss[1])
}

if strings.Contains(in, ".") {
ss := strings.Split(in, ".")
if strings.Contains(in, separator) {
ss := strings.Split(in, separator)
return in, ss[len(ss)-1]
}

Expand Down
45 changes: 28 additions & 17 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,33 +387,44 @@ func Test_getNestedValue(t *testing.T) {

func Test_makeAlias(t *testing.T) {
testCases := []struct {
tag string
input string
node string
alias string
tag string
input string
node string
alias string
separator string
}{
{
tag: "scenario 1",
input: "user.name as uname",
node: "user.name",
alias: "uname",
tag: "scenario 1",
input: "user.name as uname",
node: "user.name",
alias: "uname",
separator: ".",
},
{
tag: "scenario 2",
input: "post.title",
node: "post.title",
alias: "title",
tag: "scenario 2",
input: "post.title",
node: "post.title",
alias: "title",
separator: ".",
},
{
tag: "scenario 3",
input: "name",
node: "name",
alias: "name",
tag: "scenario 3",
input: "name",
node: "name",
alias: "name",
separator: ".",
},
{
tag: "scenario 4",
input: "post->title",
node: "post->title",
alias: "title",
separator: "->",
},
}

for _, tc := range testCases {
n, a := makeAlias(tc.input)
n, a := makeAlias(tc.input, tc.separator)
if tc.node != n || tc.alias != a {
t.Errorf("Tag: %v\nExpected: %v %v \nGot: %v %v\n", tc.tag, tc.node, tc.alias, n, a)
}
Expand Down
2 changes: 1 addition & 1 deletion jsonq.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func (j *JSONQ) only(properties ...string) interface{} {
for _, am := range aa {
tmap := map[string]interface{}{}
for _, prop := range properties {
node, alias := makeAlias(prop)
node, alias := makeAlias(prop, j.option.separator)
rv, errV := getNestedValue(am, node, j.option.separator)
if errV != nil {
j.addError(errV)
Expand Down

0 comments on commit 5ac255c

Please sign in to comment.