test-fullautomation / python-jsonpreprocessor

A preprocessor for json files
Apache License 2.0
2 stars 2 forks source link

Usage of '+' and '-' operator in indices #291

Open HolQue opened 1 month ago

HolQue commented 1 month ago

I wanted to know what happens if I use multiple '+' and '-' operators in indices

In pure Python it's like this:

listvalues = [1,2,3]
element = listvalues[1]    # -> result: 2
element = listvalues[+1]   # -> result: 2
element = listvalues[+++1] # -> result: 2
element = listvalues[-1]   # -> result: 3
element = listvalues[--1]  # -> result: 2
element = listvalues[---1] # -> result: 3
element = listvalues[+-1]  # -> result: 3
element = listvalues[-+1]  # -> result: 3
element = listvalues[+-+1] # -> result: 3

The same in JsonPreprocessor (with hard coded index and with parameter as index)

"index"      : 1,
"listvalues" : [1, 2, 3],

"param" : ${listvalues}[+1]          // -> result: 2
"param" : ${listvalues}[+${index}]   // -> result: '[1, 2, 3][+1]'
"param" : ${listvalues}[+++1]        // -> result: '[1, 2, 3][+++1]'
"param" : ${listvalues}[+++${index}] // -> result: '[1, 2, 3][+++1]'
"param" : ${listvalues}[-1]          // -> result: Error: 'Slicing is not supported!
"param" : ${listvalues}[-${index}]   // -> result: Error: 'Slicing is not supported!
"param" : ${listvalues}[--1]         // -> result: Error: 'invalid literal for int() with base 10: '--1''!
"param" : ${listvalues}[--${index}]  // -> result: Error: 'invalid literal for int() with base 10: '--1''!
"param" : ${listvalues}[---1]        // -> result: Error: 'invalid literal for int() with base 10: '---1''!
"param" : ${listvalues}[---${index}] // -> result: Error: 'invalid literal for int() with base 10: '---1''!
"param" : ${listvalues}[+-1]         // -> result: '[1, 2, 3][+-1]'
"param" : ${listvalues}[+-${index}]  // -> result: '[1, 2, 3][+-1]'
"param" : ${listvalues}[-+1]         // -> result: '[1, 2, 3][-+1]'
"param" : ${listvalues}[-+${index}]  // -> result: '[1, 2, 3][-+1]'
"param" : ${listvalues}[+-+1]        // -> result: '[1, 2, 3][+-+1]'
"param" : ${listvalues}[+-+${index}] // -> result: '[1, 2, 3][+-+1]'

In my opinion it's not a must for the JsonPreprocessor to react in the same way as Python in these cases. But in a lot of cases the index expressions are not resolved completely.

This should be fixed.

Suggestion:

  1. Remove all '+' from expressions inside []. They must have no effect.
  2. Either more than one '-' is handled as invalid expression or simply consider only the first '-' on the left hand side of the index (ignoring all further ones).
  3. Or do it exactly like Python: 3.1 --1 is +1 (- * - = +) 3.2 ---1 is -1 (- * - * - = -)
HolQue commented 1 month ago

Addendum:

Python:

negindex   = -1
listvalues = [1,2,3]
element    = listvalues[negindex]  # -> result: 3
element    = listvalues[-negindex] # -> result: 2

JsonPreprocessor:

"negindex"   : -1,
"listvalues" : [1, 2, 3],
"param"      : ${listvalues}[${negindex}]  // -> result: Error: 'Slicing is not supported!
"param"      : ${listvalues}[-${negindex}] // -> result: Error: 'invalid literal for int() with base 10: '--1''!