Skip to content

Commit 5103af8

Browse files
authored
[Core] aaz: Support simple type parsed from string value (#30623)
* [Core] �az: support simple type parsed from string. * Update src/azure-cli-core/azure/cli/core/aaz/_field_type.py * Update src/azure-cli-core/azure/cli/core/aaz/_field_type.py
1 parent 26cf88c commit 5103af8

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

src/azure-cli-core/azure/cli/core/aaz/_field_type.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import abc
66

77
from collections import OrderedDict
8+
9+
from azure.cli.core.util import shell_safe_json_parse
810
from ._base import AAZBaseType, AAZValuePatch, AAZUndefined
911
from ._field_value import AAZObject, AAZDict, AAZFreeFormDict, AAZList, AAZSimpleValue, \
1012
AAZIdentityObject
@@ -43,7 +45,13 @@ def process_data(self, data, **kwargs):
4345

4446
assert self.DataType is not None
4547
if not isinstance(data, self.DataType):
46-
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
48+
try:
49+
json_data = shell_safe_json_parse(data)
50+
except: # pylint:disable=bare-except
51+
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
52+
if not isinstance(json_data, self.DataType):
53+
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
54+
data = json_data
4755

4856
return data
4957

@@ -87,7 +95,18 @@ def process_data(self, data, **kwargs):
8795
data = float(data)
8896

8997
if not isinstance(data, self.DataType):
90-
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
98+
try:
99+
json_data = shell_safe_json_parse(data)
100+
except: # pylint:disable=bare-except
101+
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
102+
if isinstance(json_data, int):
103+
# transform int to float
104+
if float(json_data) != json_data:
105+
raise AAZValuePrecisionLossError(json_data, float(json_data))
106+
json_data = float(json_data)
107+
if not isinstance(json_data, self.DataType):
108+
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
109+
data = json_data
91110

92111
return data
93112

src/azure-cli-core/azure/cli/core/tests/test_aaz_field.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ def test_aaz_model_and_simple_types(self):
3737

3838
with self.assertRaises(AAZInvalidValueError):
3939
v.properties.count = "a"
40+
41+
with self.assertRaises(AAZInvalidValueError):
42+
v.properties.count = "1a"
43+
44+
with self.assertRaises(AAZInvalidValueError):
45+
v.properties.count = "1.1"
46+
47+
# support string input with json parsing
48+
v.properties.count = "12"
49+
assert v.properties.count == 12
50+
assert v.properties.count._data == 12
4051

4152
# test string type
4253
model_schema.properties.name = AAZStrType()
@@ -70,6 +81,17 @@ def test_aaz_model_and_simple_types(self):
7081
assert not v.properties.enable
7182
assert v.properties.enable is not False
7283

84+
with self.assertRaises(AAZInvalidValueError):
85+
v.properties.enable = 1
86+
87+
# support string input with json parsing
88+
v.properties.enable = "false"
89+
v.properties.enable = "true"
90+
assert v.properties.enable == True
91+
assert not (v.properties.enable != True)
92+
assert v.properties.enable
93+
assert v.properties.enable is not True # cannot us is
94+
7395
# test float type
7496
model_schema.properties.height = AAZFloatType()
7597
v.properties.height = 10.0
@@ -86,6 +108,17 @@ def test_aaz_model_and_simple_types(self):
86108
v.properties.height = 10 # test assign int
87109
assert str(v.properties.height) == "10.0"
88110

111+
with self.assertRaises(AAZInvalidValueError):
112+
v.properties.height = "1a"
113+
114+
v.properties.height = "12.1"
115+
assert v.properties.height == 12.1
116+
assert v.properties.height._data == 12.1
117+
118+
v.properties.height = "12"
119+
assert v.properties.height == 12
120+
assert v.properties.height._data == 12
121+
89122
# test assign properties by dict
90123
v.properties = {
91124
"count": 100,
@@ -100,6 +133,21 @@ def test_aaz_model_and_simple_types(self):
100133
assert v.properties.enable._data == True
101134
assert v.properties.height._data == 111
102135

136+
# test assign properties by dict with all string value
137+
v.properties = {
138+
"count": "101",
139+
"name": "abcd",
140+
"enable": "False",
141+
"height": "121"
142+
}
143+
144+
assert not v.properties._is_patch
145+
assert v.properties.name._data == "abcd"
146+
assert v.properties.count._data == 101
147+
assert v.properties.enable._data == False
148+
assert v.properties.height._data == 121
149+
150+
103151
def test_aaz_dict_type(self):
104152
from azure.cli.core.aaz._field_type import AAZObjectType, AAZDictType, AAZStrType
105153
from azure.cli.core.aaz._field_value import AAZObject

0 commit comments

Comments
 (0)