Skip to content

Commit

Permalink
Optimize error handling when loading nested parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
namsonx committed Mar 15, 2024
1 parent 8839f92 commit e963c02
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
61 changes: 35 additions & 26 deletions JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,6 @@ def __loadNestedValue(initValue: str, sInputStr: str, bKey=False, key=''):
initValue = initValue.replace(CNameMangling.STRINGCONVERT.value, '')
elif re.match(r"^\s*" + pattern + r"\s*$", sInputStr, re.UNICODE):
sInputStr = re.sub("\$", "$$", sInputStr)
if sInputStr.count("${") > sInputStr.count("}"):
self.__reset()
raise Exception(f"Invalid syntax! One or more than one opened or closed curly bracket is missing in expression '{initValue}'.\n \
Please check the configuration file of the executed test!")
sInputStr = self.__checkParamName(sInputStr)
handledValue = self.__nestedParamHandler(sInputStr) if not bValueConvertString else \
self.__nestedParamHandler(sInputStr, bKey=bKey, bConvertToStr=bValueConvertString)
Expand Down Expand Up @@ -912,12 +908,12 @@ def __recursiveNestedHandling(sInputStr: str, lNestedParam: list) -> str:
elif ((re.match(r"[\s{\[]*\".+\"\s*", sInputStr) and sInputStr.count("\"")==2) \
or (re.match(r"^\s*\${.+}[,\s]*$", sInputStr) and sInputStr.count("{")==sInputStr.count("}") \
and not re.search(r"(?<!^)(?<!\.)[^\.]\${", sInputStr.strip()) and not nestedKey)) \
and re.search("(" + nestedPattern + ")*", sInputStr, re.UNICODE):
and re.search("(" + nestedPattern + ")", sInputStr, re.UNICODE):
if sInputStr.strip()[-1] == ",":
sInputStr = sInputStr.strip()[:-2] + CNameMangling.STRINGCONVERT.value + "\","
else:
sInputStr = sInputStr.strip()[:-1] + CNameMangling.STRINGCONVERT.value + "\""
elif "," in sInputStr:
elif "," in sInputStr.strip()[:-1]:
if not re.match(r"^\s*\".+\"\s*$", sInputStr):
self.__reset()
raise Exception(f"Invalid nested parameter format: {sInputStr} - The double quotes are missing!!!")
Expand Down Expand Up @@ -947,16 +943,13 @@ def __recursiveNestedHandling(sInputStr: str, lNestedParam: list) -> str:
self.lNestedParams.append(nestedParam)
newInputStr = newInputStr + item if tmpItem==items[len(items)-1] else newInputStr + item + ","
sInputStr = newInputStr
elif re.search(r"\${\s*}", sInputStr) \
or (nestedKey and (sInputStr.count("{") != sInputStr.count("}") or sInputStr.count("[") != sInputStr.count("]"))):
self.__reset()
raise Exception(f"Invalid parameter format: {sInputStr}")
elif nestedKey and re.match(r"^\s*\${[^\(\)\!@#%\^\&\-\+\/\\\=`~\?]+[}\[\]]+\s*$", sInputStr):
sInputStr = re.sub(r"^\s*(\${[^\(\)\!@#%\^\&\-\+\/\\\=`~\?]+[}\[\]]+)\s*$", "\"\\1\"", sInputStr)
else:
self.__reset()
raise Exception(f"Invalid nested parameter format: {sInputStr} - The double quotes are missing!!!")

elif not re.match(r"^\s*\".+\"[,\s]*$", sInputStr):
if not re.match(r"^.+,\s*$", sInputStr):
sInputStr = re.sub(r"^\s*(.+)\s*$", "\"\\1\"", sInputStr)
else:
sInputStr = re.sub(r"^\s*(.+)\s*,\s*$", "\"\\1\",", sInputStr)
sOutput = sInputStr
return sOutput

Expand Down Expand Up @@ -996,17 +989,22 @@ def __checkNestedParam(self, sInput : str, bKey=False) -> bool:
*raise exception if nested parameter format invalid*
"""
pattern = rf"^\${{\s*[^{re.escape(self.specialCharacters)}]+}}(\[.*\])+$"
pattern = rf"^\${{\s*[^{re.escape(self.specialCharacters)}]+\s*}}(\[.*\])+$"
pattern1 = rf"\${{.+}}(\[.+\])*[^\[]*\${{"
pattern2 = r"\${.*\${.*}"
if "${" not in sInput:
return True
if re.search(rf"\${{\s*[^{re.escape(self.specialCharacters)}]+\['*.+'*\].*}}", sInput, re.UNICODE):
if CNameMangling.STRINGCONVERT.value in sInput:
sInput = sInput.replace(CNameMangling.STRINGCONVERT.value, "")
errorMsg = f"Invalid syntax: Found index or sub-element inside curly brackets in the parameter '{sInput}'"
raise Exception(errorMsg)
elif re.search(r"\[[0-9\s]*[A-Za-z_]+[0-9\s]*\]", sInput, re.UNICODE):
elif re.search(r"\[[0-9\s]*[A-Za-z_]+[0-9\s]*\]", sInput, re.UNICODE) and \
CNameMangling.STRINGCONVERT.value in sInput:
errorMsg = f"Invalid syntax! A sub-element in {sInput.strip()} has to enclosed in quotes."
self.__reset()
raise Exception(errorMsg)
elif re.search(r'\[\s*\]', sInput):
elif re.search(r'\[\s*\]', sInput) and CNameMangling.STRINGCONVERT.value in sInput:
if CNameMangling.STRINGCONVERT.value not in sInput or \
re.match(pattern, sInput.replace(CNameMangling.STRINGCONVERT.value, "")):
errorMsg = f"Expression '{sInput.replace(CNameMangling.STRINGCONVERT.value, '')}' cannot be evaluated. \
Expand All @@ -1015,6 +1013,25 @@ def __checkNestedParam(self, sInput : str, bKey=False) -> bool:
raise Exception(errorMsg)
else:
return True
elif CNameMangling.STRINGCONVERT.value in sInput:
if sInput.count("${") > sInput.count("}"):
sInput = re.sub(CNameMangling.STRINGCONVERT.value, "", sInput)
errorMsg = f"Invalid nested parameter format: {sInput.strip()}"
self.__reset()
raise Exception(errorMsg)
else:
return True
elif CNameMangling.STRINGCONVERT.value not in sInput and \
CNameMangling.DUPLICATEDKEY_01.value not in sInput:
if not re.match(r"^\${.+[}\]]+$", sInput) or (re.search(pattern1, sInput) and not bKey):
errorMsg = f"Invalid nested parameter format: {sInput} - The double quotes are missing!!!"
else:
if sInput.count("{") != sInput.count("}") or sInput.count("[") != sInput.count("]"):
errorMsg = f"Invalid nested parameter format: {sInput.strip()}"
else:
return True
self.__reset()
raise Exception(errorMsg)
else:
return True

Expand Down Expand Up @@ -1150,7 +1167,7 @@ def __checkKeynameFormat(oJson : dict):
self.__reset()
raise Exception(f"\n{str(error)} in line: '{line}'")

if re.search(pattern, line, re.UNICODE):
if "${" in line:
lNestedVar = re.findall(rf"\${{\s*([^{re.escape(self.specialCharacters)}]+)\s*}}", line, re.UNICODE)
for nestedVar in lNestedVar:
if nestedVar[0].isdigit():
Expand Down Expand Up @@ -1221,14 +1238,6 @@ def __checkKeynameFormat(oJson : dict):
self.lNestedParams.remove(nestedParam)
sJsonDataUpdated = sJsonDataUpdated + newLine + "\n"
else:
if "${" in line:
self.__reset()
invalidPattern1 = r"\${\s*[0-9A-Za-z\._]*\[.+\][0-9A-Za-z\._]*\s*}"
if re.search(invalidPattern1, line):
raise Exception(f"Invalid syntax: Found index inside curly brackets in line '{line.strip()}'. \
Indices in square brackets have to be placed outside the curly brackets.")
else:
raise Exception(f"Invalid parameter format in line: {line.strip()}")
sJsonDataUpdated = sJsonDataUpdated + line + "\n"

CJSONDecoder = None
Expand Down
10 changes: 5 additions & 5 deletions test/testconfig/TestConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,7 @@
dictUsecase['HINT'] = "Checklist rule 2 / pattern 1"
dictUsecase['COMMENT'] = None
dictUsecase['JSONFILE'] = r"..\testfiles\jpp-test_config_0353.jsonp"
dictUsecase['EXPECTEDEXCEPTION'] = "Invalid parameter format"
dictUsecase['EXPECTEDEXCEPTION'] = "Invalid nested parameter format"
dictUsecase['EXPECTEDRETURN'] = None
listofdictUsecases.append(dictUsecase)
del dictUsecase
Expand All @@ -1482,7 +1482,7 @@
dictUsecase['HINT'] = "Checklist rule 2 / pattern 2"
dictUsecase['COMMENT'] = None
dictUsecase['JSONFILE'] = r"..\testfiles\jpp-test_config_0354.jsonp"
dictUsecase['EXPECTEDEXCEPTION'] = "Invalid parameter format"
dictUsecase['EXPECTEDEXCEPTION'] = "Invalid nested parameter format"
dictUsecase['EXPECTEDRETURN'] = None
listofdictUsecases.append(dictUsecase)
del dictUsecase
Expand Down Expand Up @@ -1510,7 +1510,7 @@
dictUsecase['HINT'] = "Checklist rule 2 / pattern 4"
dictUsecase['COMMENT'] = None
dictUsecase['JSONFILE'] = r"..\testfiles\jpp-test_config_0356.jsonp"
dictUsecase['EXPECTEDEXCEPTION'] = "One or more than one opened or closed curly bracket is missing in expression"
dictUsecase['EXPECTEDEXCEPTION'] = "Invalid nested parameter format"
dictUsecase['EXPECTEDRETURN'] = None
listofdictUsecases.append(dictUsecase)
del dictUsecase
Expand Down Expand Up @@ -1538,7 +1538,7 @@
dictUsecase['HINT'] = "Checklist rule 2 / pattern 6"
dictUsecase['COMMENT'] = None
dictUsecase['JSONFILE'] = r"..\testfiles\jpp-test_config_0358.jsonp"
dictUsecase['EXPECTEDEXCEPTION'] = "One or more than one opened or closed curly bracket is missing in expression" # wording to be improved (issues/109)
dictUsecase['EXPECTEDEXCEPTION'] = "Invalid nested parameter format" # wording to be improved (issues/109)
dictUsecase['EXPECTEDRETURN'] = None
listofdictUsecases.append(dictUsecase)
del dictUsecase
Expand Down Expand Up @@ -1566,7 +1566,7 @@
dictUsecase['HINT'] = "Checklist rule 2 / pattern 8"
dictUsecase['COMMENT'] = None
dictUsecase['JSONFILE'] = r"..\testfiles\jpp-test_config_0360.jsonp"
dictUsecase['EXPECTEDEXCEPTION'] = "One or more than one opened or closed curly bracket is missing in expression"
dictUsecase['EXPECTEDEXCEPTION'] = "Invalid nested parameter format"
dictUsecase['EXPECTEDRETURN'] = None
listofdictUsecases.append(dictUsecase)
del dictUsecase
Expand Down

0 comments on commit e963c02

Please sign in to comment.