-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated pagination for jmespath result_key
s.
#232
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
|
||
from .exceptions import InvalidExpressionError | ||
|
||
|
||
def normalize_url_path(path): | ||
if not path: | ||
|
@@ -57,3 +59,47 @@ def remove_dot_segments(url): | |
output.append(url[:next_slash]) | ||
url = url[next_slash:] | ||
return ''.join(output) | ||
|
||
|
||
def validate_jmespath_for_set(expression): | ||
# Validates a limited jmespath expression to determine if we can set a value | ||
# based on it. Only works with dotted paths. | ||
if not expression or expression == '.': | ||
raise InvalidExpressionError(expression=expression) | ||
|
||
for invalid in ['[', ']', '*']: | ||
if invalid in expression: | ||
raise InvalidExpressionError(expression=expression) | ||
|
||
|
||
def set_value_from_jmespath(source, expression, value, is_first=True): | ||
# This takes a (limited) jmespath-like expression & can set a value based | ||
# on it. | ||
# Limitations: | ||
# * Only handles dotted lookups | ||
# * No offsets/wildcards/slices/etc. | ||
if is_first: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. I'm pulling it out & shuffling tests now. |
||
validate_jmespath_for_set(expression) | ||
|
||
bits = expression.split('.', 1) | ||
current_key, remainder = bits[0], bits[1] if len(bits) > 1 else '' | ||
|
||
if not current_key: | ||
raise InvalidExpressionError(expression=expression) | ||
|
||
if remainder: | ||
if not current_key in source: | ||
# We've got something in the expression that's not present in the | ||
# source (new key). If there's any more bits, we'll set the key with | ||
# an empty dictionary. | ||
source[current_key] = {} | ||
|
||
return set_value_from_jmespath( | ||
source[current_key], | ||
remainder, | ||
value, | ||
is_first=False | ||
) | ||
|
||
# If we're down to a single key, set it. | ||
source[current_key] = value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, this was a miss on my part, we should not be setting any key names to
k.expression
, we should be usingset_value_from_jmespath
. For example if you runaws rds describe-engine-default-parameters --db-parameter-group-family mysql5.1
you'll see that the result is:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gah, sorry. :( I could get to this over the weekend, if that helps. The test I added is probably wrong too.