diff --git a/src/azure-cli-core/azure/cli/core/aaz/_field_type.py b/src/azure-cli-core/azure/cli/core/aaz/_field_type.py index 7ad7b5a468e..5de1f09b97f 100644 --- a/src/azure-cli-core/azure/cli/core/aaz/_field_type.py +++ b/src/azure-cli-core/azure/cli/core/aaz/_field_type.py @@ -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 @@ -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 @@ -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 diff --git a/src/azure-cli-core/azure/cli/core/tests/test_aaz_field.py b/src/azure-cli-core/azure/cli/core/tests/test_aaz_field.py index d623d1c4181..20254f90a13 100644 --- a/src/azure-cli-core/azure/cli/core/tests/test_aaz_field.py +++ b/src/azure-cli-core/azure/cli/core/tests/test_aaz_field.py @@ -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() @@ -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 @@ -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, @@ -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