-
Notifications
You must be signed in to change notification settings - Fork 217
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
Add prefixed enums in addition to pretty ones #589
Open
snikch
wants to merge
10
commits into
danielgtaylor:master
Choose a base branch
from
snikch:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−6
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dbd0903
Add prefixed enums in addition to pretty ones
snikch 8e9b9b2
Use a lookup map for full name
snikch 30a5271
Revert
snikch beaa873
Revert
snikch c07d5b0
Use a lookup map for full name
snikch 5a2f64c
Move to property
snikch 6b80b2a
Add json output
snikch 675e71b
Add backwards compat test
snikch 9f928de
Handle from_json
snikch 6ab06ca
Merge branch 'danielgtaylor:master' into master
snikch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1515,24 +1515,30 @@ def to_dict( | |||||||||
else: | ||||||||||
output[cased_name] = b64encode(value).decode("utf8") | ||||||||||
elif meta.proto_type == TYPE_ENUM: | ||||||||||
def name(enum_class, value): | ||||||||||
obj = enum_class(value) | ||||||||||
if hasattr(obj.__class__, 'full_name') and isinstance(obj.__class__.full_name, property): | ||||||||||
return obj.full_name | ||||||||||
return obj.name | ||||||||||
if field_is_repeated: | ||||||||||
enum_class = field_types[field_name].__args__[0] | ||||||||||
if isinstance(value, typing.Iterable) and not isinstance( | ||||||||||
value, str | ||||||||||
): | ||||||||||
output[cased_name] = [enum_class(el).name for el in value] | ||||||||||
output[cased_name] = [ | ||||||||||
name(enum_class, el) for el in value] | ||||||||||
else: | ||||||||||
# transparently upgrade single value to repeated | ||||||||||
output[cased_name] = [enum_class(value).name] | ||||||||||
output[cased_name] = [name(enum_class, value)] | ||||||||||
elif value is None: | ||||||||||
if include_default_values: | ||||||||||
output[cased_name] = value | ||||||||||
elif meta.optional: | ||||||||||
enum_class = field_types[field_name].__args__[0] | ||||||||||
output[cased_name] = enum_class(value).name | ||||||||||
output[cased_name] = name(enum_class, value) | ||||||||||
else: | ||||||||||
enum_class = field_types[field_name] # noqa | ||||||||||
output[cased_name] = enum_class(value).name | ||||||||||
output[cased_name] = name(enum_class, value) | ||||||||||
elif meta.proto_type in (TYPE_FLOAT, TYPE_DOUBLE): | ||||||||||
if field_is_repeated: | ||||||||||
output[cased_name] = [_dump_float(n) for n in value] | ||||||||||
|
@@ -1592,10 +1598,13 @@ def _from_dict_init(cls, mapping: Mapping[str, Any]) -> Mapping[str, Any]: | |||||||||
) | ||||||||||
elif meta.proto_type == TYPE_ENUM: | ||||||||||
enum_cls = cls._betterproto.cls_by_field[field_name] | ||||||||||
|
||||||||||
def obj(enum_class, value): | ||||||||||
return enum_class.from_full_name(value) if hasattr(enum_class, 'from_full_name') else enum_class.from_string(value) | ||||||||||
Comment on lines
+1602
to
+1603
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.
Suggested change
Same as previous here |
||||||||||
if isinstance(value, list): | ||||||||||
value = [enum_cls.from_string(e) for e in value] | ||||||||||
value = [obj(enum_cls, e) for e in value] | ||||||||||
elif isinstance(value, str): | ||||||||||
value = enum_cls.from_string(value) | ||||||||||
value = obj(enum_cls, value) | ||||||||||
elif meta.proto_type in (TYPE_FLOAT, TYPE_DOUBLE): | ||||||||||
value = ( | ||||||||||
[_parse_float(n) for n in value] | ||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This should probably go in utils or enum.py