@@ -44,18 +44,71 @@ func (token *expressionToken) Apply(root, current interface{}, next []Token) (in
4444 return value , nil
4545}
4646
47+ func getRootTokenIndex (expression string ) int {
48+ idx := strings .Index (expression , "$" )
49+ if idx < 0 {
50+ return - 1
51+ }
52+ if idx + 1 >= len (expression ) {
53+ // there is no next character
54+ return idx
55+ }
56+
57+ // TODO: this is not accurate enough, we need to properly parse expressions
58+ nextRune := expression [idx + 1 ]
59+ switch nextRune {
60+ case '.' , '[' , ' ' , '-' , '+' , '/' , '%' , '>' , '<' , '=' , '!' :
61+ // valid root token
62+ return idx
63+ default :
64+ // not a root token
65+ // need to check if there are other $ tokens
66+ subCheck := getRootTokenIndex (expression [idx + 1 :])
67+ if subCheck > 0 {
68+ return idx + 1 + subCheck
69+ }
70+ return - 1
71+ }
72+ }
73+
74+ func getCurrentTokenIndex (expression string ) int {
75+ idx := strings .Index (expression , "@" )
76+ if idx < 0 {
77+ return - 1
78+ }
79+ if idx + 1 >= len (expression ) {
80+ // there is no next character
81+ return idx
82+ }
83+
84+ // TODO: this is not accurate enough, we need to properly parse expressions
85+ nextRune := expression [idx + 1 ]
86+ switch nextRune {
87+ case '.' , '[' , ' ' , '-' , '+' , '/' , '%' , '>' , '<' , '=' , '!' :
88+ // valid current token
89+ return idx
90+ default :
91+ // not a current token
92+ // need to check if there are other @ tokens
93+ subCheck := getCurrentTokenIndex (expression [idx + 1 :])
94+ if subCheck > 0 {
95+ return idx + 1 + subCheck
96+ }
97+ return - 1
98+ }
99+ }
100+
47101// TODO : add extra support
48102/*
491031. regex
50104*/
51-
52105func evaluateExpression (root , current interface {}, expression string , options * Options ) (interface {}, error ) {
53106 if expression == "" {
54107 return nil , getInvalidExpressionEmptyError ()
55108 }
56109
57- rootIndex := strings . Index (expression , "$" )
58- currentIndex := strings . Index (expression , "@" )
110+ rootIndex := getRootTokenIndex (expression )
111+ currentIndex := getCurrentTokenIndex (expression )
59112
60113 for rootIndex > - 1 || currentIndex > - 1 {
61114
@@ -103,8 +156,8 @@ func evaluateExpression(root, current interface{}, expression string, options *O
103156 expression = strings .ReplaceAll (expression , query , new )
104157 }
105158
106- rootIndex = strings . Index (expression , "$" )
107- currentIndex = strings . Index (expression , "@" )
159+ rootIndex = getRootTokenIndex (expression )
160+ currentIndex = getCurrentTokenIndex (expression )
108161 }
109162
110163 expression = strings .TrimSpace (expression )
0 commit comments