-
Notifications
You must be signed in to change notification settings - Fork 1
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
Implement explicit enums #116
Changes from 2 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 |
---|---|---|
|
@@ -87,7 +87,7 @@ def set_payload(self, value: Any): | |
raise ValueError("cannot set payload for enum (should be either a dictionary or a list)") | ||
|
||
def get_payload(self) -> Any: | ||
obj = SimpleNamespace() | ||
obj = _EnumPayload() | ||
|
||
for field in self.fields: | ||
setattr(obj, field.name, field.get_payload()) | ||
|
@@ -108,3 +108,8 @@ def __iter__(self): | |
|
||
for field in self.fields: | ||
yield (field.name, field.value) | ||
|
||
|
||
class _EnumPayload(SimpleNamespace): | ||
def __int__(self): | ||
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. This way, people can convert an |
||
return getattr(self, ENUM_DISCRIMINANT_FIELD_NAME) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from typing import Any | ||
|
||
from multiversx_sdk.abi.string_value import StringValue | ||
|
||
|
||
class ExplicitEnumValue(StringValue): | ||
def __init__(self, value: str = "") -> None: | ||
self.value = value | ||
|
||
def __eq__(self, other: Any) -> bool: | ||
return isinstance(other, ExplicitEnumValue) and self.value == other.value | ||
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. Currently, we don't differentiate between different types of enums (as seen in ABI) if they have the same value / name. We'll brainstorm this a bit (could be perfectly fine in practice). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,93 @@ | ||
{ | ||
"name": "Artificial", | ||
"constructor": { | ||
"inputs": [ | ||
{ | ||
"name": "a", | ||
"type": "utf-8 string" | ||
} | ||
], | ||
"outputs": [] | ||
}, | ||
"upgradeConstructor": { | ||
"inputs": [ | ||
{ | ||
"name": "a", | ||
"type": "u8" | ||
} | ||
], | ||
"outputs": [] | ||
}, | ||
"endpoints": [ | ||
{ | ||
"name": "blue", | ||
"mutability": "readonly", | ||
"inputs": [], | ||
"outputs": [ | ||
{ | ||
"type": "optional<multi<TokenIdentifier,u64,BigUint>>" | ||
} | ||
] | ||
"name": "Artificial", | ||
"constructor": { | ||
"inputs": [ | ||
{ | ||
"name": "a", | ||
"type": "utf-8 string" | ||
} | ||
], | ||
"outputs": [] | ||
}, | ||
"upgradeConstructor": { | ||
"inputs": [ | ||
{ | ||
"name": "a", | ||
"type": "u8" | ||
} | ||
], | ||
"outputs": [] | ||
}, | ||
{ | ||
"name": "yellow", | ||
"mutability": "mutable", | ||
"inputs": [ | ||
"endpoints": [ | ||
{ | ||
"name": "value", | ||
"type": "multi<u32, bytes, bool>" | ||
"name": "blue", | ||
"mutability": "readonly", | ||
"inputs": [], | ||
"outputs": [ | ||
{ | ||
"type": "optional<multi<TokenIdentifier,u64,BigUint>>" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "yellow", | ||
"mutability": "mutable", | ||
"inputs": [ | ||
{ | ||
"name": "value", | ||
"type": "multi<u32, bytes, bool>" | ||
} | ||
], | ||
"outputs": [] | ||
}, | ||
{ | ||
"name": "orange", | ||
"mutability": "mutable", | ||
"inputs": [ | ||
{ | ||
"name": "value", | ||
"type": "EgldOrEsdtTokenIdentifier" | ||
} | ||
], | ||
"outputs": [] | ||
}, | ||
{ | ||
"name": "green", | ||
"inputs": [], | ||
"outputs": [ | ||
{ | ||
"type": "OperationCompletionStatus" | ||
} | ||
] | ||
} | ||
], | ||
"types": { | ||
"OperationCompletionStatus": { | ||
"type": "explicit-enum", | ||
"variants": [ | ||
{ | ||
"docs": ["indicates that operation was completed"], | ||
"name": "completed" | ||
}, | ||
{ | ||
"docs": [ | ||
"indicates that operation was interrupted prematurely, due to low gas" | ||
], | ||
"name": "interrupted" | ||
} | ||
] | ||
} | ||
], | ||
"outputs": [] | ||
}, | ||
{ | ||
"name": "orange", | ||
"mutability": "mutable", | ||
"inputs": [ | ||
"events": [ | ||
{ | ||
"name": "value", | ||
"type": "EgldOrEsdtTokenIdentifier" | ||
"identifier": "firstEvent", | ||
"inputs": [ | ||
{ | ||
"name": "result", | ||
"type": "BigUint", | ||
"indexed": true | ||
} | ||
] | ||
} | ||
], | ||
"outputs": [] | ||
} | ||
], | ||
"types": {}, | ||
"events": [ | ||
{ | ||
"identifier": "firstEvent", | ||
"inputs": [ | ||
{ | ||
"name": "result", | ||
"type": "BigUint", | ||
"indexed": true | ||
} | ||
] | ||
} | ||
] | ||
] | ||
} |
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.
why was this deleted?
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.
By mistake 🙈
Fixed.