-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
231 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import Any, Callable, List, Optional, Union | ||
|
||
from multiversx_sdk.abi.interface import IPayloadHolder, ISingleValue | ||
from multiversx_sdk.abi.multi_value import MultiValue | ||
from multiversx_sdk.abi.shared import convert_native_value_to_list | ||
|
||
|
||
class CountedVariadicValues(IPayloadHolder): | ||
def __init__(self, | ||
items: Optional[List[Union[ISingleValue, MultiValue]]] = None, | ||
item_creator: Optional[Callable[[], Union[ISingleValue, MultiValue]]] = None) -> None: | ||
self.items = items or [] | ||
self.length = len(self.items) | ||
|
||
self.item_creator = item_creator | ||
|
||
def set_payload(self, value: Any): | ||
if not self.item_creator: | ||
raise ValueError("populating variadic values from a native object requires the item creator to be set") | ||
|
||
native_items, _ = convert_native_value_to_list(value) | ||
self.length = len(native_items) | ||
|
||
self.items.clear() | ||
|
||
for native_item in native_items: | ||
item = self.item_creator() | ||
item.set_payload(native_item) | ||
self.items.append(item) | ||
|
||
def get_payload(self) -> Any: | ||
return [item.get_payload() for item in self.items] | ||
|
||
def __eq__(self, other: Any) -> bool: | ||
return ( | ||
isinstance(other, CountedVariadicValues) | ||
and self.items == other.items | ||
and self.item_creator == other.item_creator | ||
) | ||
|
||
def __iter__(self) -> Any: | ||
return iter(self.items) |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import re | ||
|
||
import pytest | ||
|
||
from multiversx_sdk.abi.counted_variadic_values import CountedVariadicValues | ||
from multiversx_sdk.abi.multi_value import MultiValue | ||
from multiversx_sdk.abi.small_int_values import U32Value | ||
from multiversx_sdk.abi.string_value import StringValue | ||
|
||
|
||
def test_set_payload_and_get_payload(): | ||
# Simple | ||
values = CountedVariadicValues(item_creator=lambda: U32Value()) | ||
values.set_payload([1, 2, 3]) | ||
|
||
assert values.length == 3 | ||
assert values.items == [U32Value(1), U32Value(2), U32Value(3)] | ||
assert values.get_payload() == [1, 2, 3] | ||
|
||
# Nested | ||
values = CountedVariadicValues(item_creator=lambda: MultiValue([U32Value(), StringValue()])) | ||
values.set_payload([[42, "hello"], [43, "world"]]) | ||
|
||
assert values.length == 2 | ||
assert values.items == [ | ||
MultiValue([U32Value(42), StringValue("hello")]), | ||
MultiValue([U32Value(43), StringValue("world")]) | ||
] | ||
|
||
assert values.get_payload() == [[42, "hello"], [43, "world"]] | ||
|
||
# With errors | ||
with pytest.raises(ValueError, match="populating variadic values from a native object requires the item creator to be set"): | ||
CountedVariadicValues().set_payload([1, 2, 3]) | ||
|
||
# With errors | ||
with pytest.raises(ValueError, match=re.escape("invalid literal for int() with base 10: 'foo'")): | ||
values = CountedVariadicValues(item_creator=lambda: U32Value()) | ||
values.set_payload(["foo"]) |
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
43 changes: 43 additions & 0 deletions
43
multiversx_sdk/testutils/testdata/counted-variadic.abi.json
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"endpoints": [ | ||
{ | ||
"name": "foo", | ||
"inputs": [ | ||
{ | ||
"type": "counted-variadic<Dummy>" | ||
} | ||
], | ||
"outputs": [] | ||
}, | ||
{ | ||
"name": "bar", | ||
"inputs": [ | ||
{ | ||
"type": "counted-variadic<u32>" | ||
}, | ||
{ | ||
"type": "counted-variadic<bytes>" | ||
} | ||
], | ||
"outputs": [ | ||
{ | ||
"type": "counted-variadic<u32>" | ||
}, | ||
{ | ||
"type": "counted-variadic<bytes>" | ||
} | ||
] | ||
} | ||
], | ||
"types": { | ||
"Dummy": { | ||
"type": "struct", | ||
"fields": [ | ||
{ | ||
"name": "a", | ||
"type": "u32" | ||
} | ||
] | ||
} | ||
} | ||
} |