in samcli/cli/types.py [0:0]
def _parse_key_value_pair(self, result: dict, key_value_string: str):
"""
This method processes a string in the format "'key1'='value1','key2'='value2'",
where spaces may exist within keys or values.
To optimize performance, the parsing is divided into two stages:
Stage 1: Optimized Parsing
1. Identify quoted strings containing spaces within values.
2. Temporarily replace spaces in these strings with a placeholder (e.g., "_").
3. Use a fast, standard parser to extract key-value pairs, as no spaces are expected.
4. Restore original spaces in the extracted key-value pairs.
Stage 2: Fallback Parsing
If Stage 1 fails to parse the string correctly,run against a comprehensive regex pattern
{tag}={tag}) to parse the entire string.
Parameters
----------
result: result dict
key_value_string: string to parse
Returns
-------
boolean - parse result
"""
parse_result = True
# Unquote an entire string
modified_val = _unquote_wrapped_quotes(key_value_string)
# Looking for a quote strings that contain spaces and proceed to replace them
quoted_strings_with_spaces = re.findall(self._quoted_pattern, modified_val)
quoted_strings_with_spaces_objects = [
TextWithSpaces(str_with_spaces) for str_with_spaces in quoted_strings_with_spaces
]
for s, replacement in zip(quoted_strings_with_spaces, quoted_strings_with_spaces_objects):
modified_val = modified_val.replace(s, replacement.replace_spaces())
# Use default parser to parse key=value
tags = self._multiple_space_separated_key_value_parser(modified_val)
if tags is not None:
for key, value in tags.items():
new_value = value
text_objects = [obj for obj in quoted_strings_with_spaces_objects if obj.modified_text == value]
if len(text_objects) > 0:
new_value = text_objects[0].restore_spaces()
self._add_value(result, _unquote_wrapped_quotes(key), _unquote_wrapped_quotes(new_value))
else:
# Otherwise, fall back to the original mechanism.
groups = re.findall(self._pattern, key_value_string)
if not groups:
parse_result = False
for group in groups:
key, v = group
self._add_value(result, _unquote_wrapped_quotes(key), _unquote_wrapped_quotes(v))
return parse_result