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
270 replacer casts #275
270 replacer casts #275
Conversation
@jd I suspect you might have some input on this if you can cast (ha!) your mind back to gnocchi time. |
I think we mostly used Now I understand why you'd need this feature in data. I did not check the implementation, but the principle LGTM. |
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.
The concept seems sound, though I'm not sure I can grok all the implications (also WRT the implementation).
As mentioned before, I'm a little wary because this feels like special-casing, but that's quite possibly unjustified or simply irrelevant (as you're solving an actual problem here).
if value.lower() == "true": | ||
return True | ||
if value.lower() == "null": | ||
return None |
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.
minor nit:
return {
"true": True,
"false": False,
"null": None
}.get(value.lower(), value)
seems more readable/idiomatic?
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.
Perhaps, but code was already there in the previous way and changing it now would be an unrelated change.
Also that looks icky.
@taget have you had a chance to try this out? |
@taget I'm still hoping you can verify that this solves the problems you were trying to solve |
@cdent I tried in my test environment, works for me when cast a integer to string yaml:
VS_HOSTIP="101" VS_UUID="100" PAUSE_TIME=122 gabbi-run http://127.0.0.1:8000 < a.yaml result
thanks for your help :) |
$ENVIRON:cast is now a workable match, but it does nothing, yet. casting.yaml sets some basic (still failing) tests
Because of the way re.sub works with functions, we need to do casting outside of the repl function passed to re.sub. Those functions are only allowed to return strings, and if they don't the results are ignored. Therefore we manage a 'cast' attribute on the test itself which contains the value of any 'cast' group from the the most recent matched regex. This is then used (via APPROVED_CASTS) to do the actual casting to the proper value. Interesting, this work points out some problems that already existed in any casting done with replacers: the cast is done on the entire message, not just the replaced bit, which means for cases like foo$ENVIRON:int['12']bar the results will be not quite as expected. I'll work this out as this series of patches progresses, but for now, consider it a known issue.
If the replacement token is the whole of the message, then an include :cast will be done. Otherwise there will be a RuntimeError. This provides the precendent for the previous behavior on $ENVIRON replacement: atoms can be cast becuase there is no context to create a dependency problem.
1efcd24
to
f284fc9
Compare
This is an effort to provide the functionality described in #270 by implementing optional "casts" on the $ENVIRON and $REPLACER replacers by appending a :cast (int, str, bool, float) to the replacer name (e.g.,
$ENVIRON:int['FOOBAR']
).This only works when the replacer is the entirety of the message being replaced:
not mixed in:
We can't do this for two reasons:
repl
function inre.sub
works, they can only return strings, thus the casting has to happen after the re.subWhile the code is present to make it possible to do casts of other replacers, for the moment any cast will be ignored, mostly because it doesn't make sense for most of them.
It might, however, make sense for
$COOKIE
so input on that desired.This isn't done, it needs to be confirmed as having grammatical sense, and it also needs doc update, but I'm putting it up here for some initial feedback on the format and implementation.