From e9641154736d70dd87ed42a8898b9aa9589cd9cc Mon Sep 17 00:00:00 2001 From: Saddam H Date: Wed, 20 Feb 2019 20:33:53 +0600 Subject: [PATCH] Fix separator --- helper.go | 8 ++++---- helper_test.go | 45 ++++++++++++++++++++++++++++----------------- jsonq.go | 2 +- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/helper.go b/helper.go index cf19aa9..7de6634 100644 --- a/helper.go +++ b/helper.go @@ -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) @@ -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) @@ -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] } diff --git a/helper_test.go b/helper_test.go index 6aa66d3..31cc1a2 100644 --- a/helper_test.go +++ b/helper_test.go @@ -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) } diff --git a/jsonq.go b/jsonq.go index 67b0b13..7a9a98f 100644 --- a/jsonq.go +++ b/jsonq.go @@ -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)