test-fullautomation / python-jsonpreprocessor

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

Error messages (30) #328

Open HolQue opened 3 weeks ago

HolQue commented 3 weeks ago

Previously this code

{
   "dictP"  : {"A" : 1, "B" : 2},
   "newparam" : ${dictP}['${dictP}']
}

caused newparam to have string value '{'A': 1, 'B': 2}['{'A': 1, 'B': 2}']'.

With latest changes of the JsonPreprocessor this changes to newparam having dictionary value {'A': 1, 'B': 2}.

Both is wrong.

The meaning of ${dictP}['${dictP}'] is: A dictionary is converted to a string; the result is used as key of a dictionary.

Is this possible? Yes. Does this makes sense? Not really.

I checked in pure Python:

dictparam     = {'A': 1, 'B': 2}
str_dictparam = str(dictparam)
dictparam2    = {}
dictparam2["{'A': 1, 'B': 2}"] = 123
value = dictparam2[str_dictparam]
print(f"value = {value}")

Outcome

value = 123

Conclusion: An expression like this "{'A': 1, 'B': 2}" can be used as key.

I am not sure if we shall allow this. It's strange. But keys can be of type str. This is allowed. And if a user decides to convert a dictionary to a string and use this string as key, like in ['${dictP}'], then it is like it is.

It has to be possible to take a key name out of a dictionary and use this key as key for another dictionary. Therefore we cannot suppress using composite data types inside square brackets.

Example:

"dictP1"  : {"A" : 1  , "B" : 2},
"dictP2"  : {"1" : "C", "2" : "D"},
"newparam" : ${dictP2}['${dictP1}['A']']

The result is correct:

'newparam': 'C'

Finally this is my proposal: We follow the Python philosophy. Users get a high degree of freedom, but must be aware of what they do.

Coming back now to the first example from above:

{
   "dictP"  : {"A" : 1, "B" : 2},
   "newparam" : ${dictP}['${dictP}']
}

This code has to cause a "key error". Because of a key with name '{"A" : 1, "B" : 2}'does not exist inside dictP.

HolQue commented 3 weeks ago

Similar issue with:

"newparam" : ${listP}['${listP}']

test-fullautomation commented 1 week ago

Hi @namsonx , @HolQue , expectation is:

This deviates from the python behavior. The assumption is that we hand-write the JSONP files, therefore it is not meaningful to allow stringified data-structures as key-name. This is valid on both, left and right side as well as standard- and dotdict notation.

Thank you, Thomas