-
Notifications
You must be signed in to change notification settings - Fork 624
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
Update keys() to be able to iterate over a dict by updating the loop … #1492
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
|
@@ -260,7 +260,60 @@ def get( | |
return decoded | ||
|
||
def keys(self, carrier: dict) -> typing.List[str]: | ||
return [_key.decode("utf8") for (_key, _value) in carrier] | ||
|
||
""" | ||
In most examples of propogators, they attempt to get a header key with .get() but when that fails they seem to | ||
want to search all keys within carrier. There is not a prescribed structure for carrier in the specs | ||
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/api-propagators.md | ||
So we need to evaluate the carrier and peer into any child lists or dicts within carrier and return all of the keys | ||
""" | ||
|
||
|
||
#define the default empty list | ||
returnable = [] | ||
|
||
#internal function that appends keys | ||
def append_keys(key): | ||
if isinstance(key, bytes): | ||
returnable.append(key.decode("utf8")) | ||
elif isinstance(key, str): | ||
returnable.append(key) | ||
|
||
#carrier is a dict, so iterate over .items() | ||
for _key, _val in carrier.items(): | ||
|
||
#if we have another dict, lets make a recursive call | ||
if isinstance(_val, Dict): | ||
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. Consider move the code that append dict to new function 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. done |
||
#append our current key | ||
append_keys(_key) | ||
|
||
#append all keys within the dict | ||
for x in self.keys(_val): | ||
append_keys(x) | ||
|
||
# if we have a list, lets iter over that. List can contain tuples(headers) dicts and string so lets approach them all as well | ||
elif isinstance(_val, List): | ||
for list_key in _val: | ||
|
||
#Check for the Tuple | ||
if isinstance(list_key, Tuple): | ||
append_keys(list_key[0]) | ||
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. What does the tuple represent? Do we need the rest of the tuple? 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. you know i thought long and hard on this. So the only example of a Tuple ive seen come through is within the header, and the tuple is used to keep devs from modifying header values when they are received. So really every tuple was only 2 keys long and acted as a key value store. So i grab the first one as the key and let the 2nd value drop. for example. the headers come in looking like. Carrier{'headers':[('host','127.0.0.01'),('port','6020')]}. I decided not to spend time on checking if the tuple was longer than 2 because there wouldnt be a defined way for this method to determine what a key was and what a value was over. To be perfectly honest i feel like this is all a little heavy for what this method needs to do, but i dont see any prescribed structure. However I could be convinced that this needs to account and add all tuple values to list just to make sure we get all possible header values. 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. Ok sounds good to me, maybe just check if the tuple is not empty so the code will not throw an exception |
||
|
||
#check for the dict | ||
elif isinstance(list_key, Dict): | ||
append_keys(_key) | ||
|
||
#append all keys within the dict | ||
for x in self.keys(_val): | ||
append_keys(x) | ||
else: | ||
append_keys(list_key) | ||
|
||
#finally, if our key was just a string, append that | ||
else: | ||
append_keys(_key) | ||
|
||
return returnable | ||
|
||
|
||
asgi_getter = ASGIGetter() | ||
|
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.
I think if you will move the append_keys function outside the keys function will be more clear and readable
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.
I'm not sure I agree whole heartedly on that. There are many use cases for inner functions, in this case, as a helper function. going this route allowed it to have access to everything within the keys() scope. which was beneficial. And all this does is gives us reusablility within the keys() method. they do not serve a purpose outside of the scope of keys()