-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: Improve handling for list structures (#456)
## 📝 Description This change improves the handling for nested list structures (e.g. Instance Interfaces) by implicitly detecting when the next list entry should be populated. This means that users do not have to explicitly specify empty values for null fields. For example, see this command: ```bash linode-cli linodes config-update 1234 5678 \ --interfaces.purpose public \ --interfaces.purpose vlan --interfaces.label my-vlan ``` A user would intuitively expect this command to set the first interface to public and the second interface to a VLAN with the label `my-vlan`, however executing this command would generate this request body: ```json { "interfaces":[ { "label":"cool", "purpose":"public" } ] } ``` After this change, the request body is now generated as expected: ```json { "interfaces":[ { "label":null, "ipam_address":null, "purpose":"public" }, { "label":"cool", "purpose":"vlan" } ] } ``` ## ✔️ How to Test ``` make test ```
- Loading branch information
1 parent
7bdf730
commit 80e4c6d
Showing
3 changed files
with
143 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,80 @@ | ||
import argparse | ||
|
||
from linodecli import operation | ||
|
||
|
||
class TestOperation: | ||
def test_list_arg_action_basic(self): | ||
""" | ||
Tests a basic list argument condition. | ||
""" | ||
|
||
parser = argparse.ArgumentParser( | ||
prog=f"foo", | ||
) | ||
|
||
for arg_name in ["foo", "bar", "aaa"]: | ||
parser.add_argument( | ||
f"--foo.{arg_name}", | ||
metavar=arg_name, | ||
action=operation.ListArgumentAction, | ||
type=str, | ||
) | ||
|
||
result = parser.parse_args( | ||
[ | ||
"--foo.foo", | ||
"cool", | ||
"--foo.bar", | ||
"wow", | ||
"--foo.aaa", | ||
"computer", | ||
"--foo.foo", | ||
"test", | ||
"--foo.bar", | ||
"wow", | ||
"--foo.aaa", | ||
"akamai", | ||
] | ||
) | ||
assert getattr(result, "foo.foo") == ["cool", "test"] | ||
assert getattr(result, "foo.bar") == ["wow", "wow"] | ||
assert getattr(result, "foo.aaa") == ["computer", "akamai"] | ||
|
||
def test_list_arg_action_missing_attr(self): | ||
""" | ||
Tests that a missing attribute for the first element will be | ||
implicitly populated. | ||
""" | ||
|
||
parser = argparse.ArgumentParser( | ||
prog=f"foo", | ||
) | ||
|
||
for arg_name in ["foo", "bar", "aaa"]: | ||
parser.add_argument( | ||
f"--foo.{arg_name}", | ||
metavar=arg_name, | ||
action=operation.ListArgumentAction, | ||
type=str, | ||
) | ||
|
||
result = parser.parse_args( | ||
[ | ||
"--foo.foo", | ||
"cool", | ||
"--foo.aaa", | ||
"computer", | ||
"--foo.foo", | ||
"test", | ||
"--foo.bar", | ||
"wow", | ||
"--foo.foo", | ||
"linode", | ||
"--foo.aaa", | ||
"akamai", | ||
] | ||
) | ||
assert getattr(result, "foo.foo") == ["cool", "test", "linode"] | ||
assert getattr(result, "foo.bar") == [None, "wow"] | ||
assert getattr(result, "foo.aaa") == ["computer", None, "akamai"] |