Skip to content

Commit

Permalink
[Core] aaz: Support simple type parsed from string value (#30623)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
kairu-ms authored Jan 9, 2025
1 parent 26cf88c commit 5103af8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/azure-cli-core/azure/cli/core/aaz/_field_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import abc

from collections import OrderedDict

from azure.cli.core.util import shell_safe_json_parse
from ._base import AAZBaseType, AAZValuePatch, AAZUndefined
from ._field_value import AAZObject, AAZDict, AAZFreeFormDict, AAZList, AAZSimpleValue, \
AAZIdentityObject
Expand Down Expand Up @@ -43,7 +45,13 @@ def process_data(self, data, **kwargs):

assert self.DataType is not None
if not isinstance(data, self.DataType):
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
try:
json_data = shell_safe_json_parse(data)
except: # pylint:disable=bare-except
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
if not isinstance(json_data, self.DataType):
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
data = json_data

return data

Expand Down Expand Up @@ -87,7 +95,18 @@ def process_data(self, data, **kwargs):
data = float(data)

if not isinstance(data, self.DataType):
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
try:
json_data = shell_safe_json_parse(data)
except: # pylint:disable=bare-except
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
if isinstance(json_data, int):
# transform int to float
if float(json_data) != json_data:
raise AAZValuePrecisionLossError(json_data, float(json_data))
json_data = float(json_data)
if not isinstance(json_data, self.DataType):
raise AAZInvalidValueError('Expect {}, got {} ({})'.format(self.DataType, data, type(data)))
data = json_data

return data

Expand Down
48 changes: 48 additions & 0 deletions src/azure-cli-core/azure/cli/core/tests/test_aaz_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ def test_aaz_model_and_simple_types(self):

with self.assertRaises(AAZInvalidValueError):
v.properties.count = "a"

with self.assertRaises(AAZInvalidValueError):
v.properties.count = "1a"

with self.assertRaises(AAZInvalidValueError):
v.properties.count = "1.1"

# support string input with json parsing
v.properties.count = "12"
assert v.properties.count == 12
assert v.properties.count._data == 12

# test string type
model_schema.properties.name = AAZStrType()
Expand Down Expand Up @@ -70,6 +81,17 @@ def test_aaz_model_and_simple_types(self):
assert not v.properties.enable
assert v.properties.enable is not False

with self.assertRaises(AAZInvalidValueError):
v.properties.enable = 1

# support string input with json parsing
v.properties.enable = "false"
v.properties.enable = "true"
assert v.properties.enable == True
assert not (v.properties.enable != True)
assert v.properties.enable
assert v.properties.enable is not True # cannot us is

# test float type
model_schema.properties.height = AAZFloatType()
v.properties.height = 10.0
Expand All @@ -86,6 +108,17 @@ def test_aaz_model_and_simple_types(self):
v.properties.height = 10 # test assign int
assert str(v.properties.height) == "10.0"

with self.assertRaises(AAZInvalidValueError):
v.properties.height = "1a"

v.properties.height = "12.1"
assert v.properties.height == 12.1
assert v.properties.height._data == 12.1

v.properties.height = "12"
assert v.properties.height == 12
assert v.properties.height._data == 12

# test assign properties by dict
v.properties = {
"count": 100,
Expand All @@ -100,6 +133,21 @@ def test_aaz_model_and_simple_types(self):
assert v.properties.enable._data == True
assert v.properties.height._data == 111

# test assign properties by dict with all string value
v.properties = {
"count": "101",
"name": "abcd",
"enable": "False",
"height": "121"
}

assert not v.properties._is_patch
assert v.properties.name._data == "abcd"
assert v.properties.count._data == 101
assert v.properties.enable._data == False
assert v.properties.height._data == 121


def test_aaz_dict_type(self):
from azure.cli.core.aaz._field_type import AAZObjectType, AAZDictType, AAZStrType
from azure.cli.core.aaz._field_value import AAZObject
Expand Down

0 comments on commit 5103af8

Please sign in to comment.