Skip to content

Commit b3ac724

Browse files
author
iscai-msft
committed
illustrate backcompat methods
1 parent 6b05a20 commit b3ac724

File tree

1 file changed

+10
-7
lines changed
  • src/azure-cli-core/azure/cli/core

1 file changed

+10
-7
lines changed

src/azure-cli-core/azure/cli/core/util.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from knack.log import get_logger
2121
from knack.util import CLIError, to_snake_case, to_camel_case
22+
from azure.core.serialization import is_generated_model, attribute_list
2223

2324
logger = get_logger(__name__)
2425

@@ -647,16 +648,18 @@ def todict(obj, post_processor=None):
647648
# This is the only difference with knack.util.todict because for typespec generated SDKs
648649
# The base model stores data in obj.__dict__['_data'] instead of in obj.__dict__
649650
# We need to call obj.as_dict() to extract data for this kind of model
650-
if hasattr(obj, 'as_dict') and not hasattr(obj, '_attribute_map'):
651-
result = {to_camel_case(k): todict(v, post_processor) for k, v in obj.as_dict().items()}
651+
if is_generated_model(obj):
652+
# returns True for both msrest model and new hybrid model
653+
# the backcompat methods handle both models the same way, so you don't need to have separate code
654+
# for old vs new models
655+
result = {
656+
to_camel_case(attr): todict(getattr(obj, attr), post_processor)
657+
for attr in attribute_list(obj)
658+
}
659+
# don't need to verify whether something is callable or private, as attribute_list() already does that
652660
return post_processor(obj, result) if post_processor else result
653661
if hasattr(obj, '_asdict'):
654662
return todict(obj._asdict(), post_processor)
655-
if hasattr(obj, '__dict__'):
656-
result = {to_camel_case(k): todict(v, post_processor)
657-
for k, v in obj.__dict__.items()
658-
if not callable(v) and not k.startswith('_')}
659-
return post_processor(obj, result) if post_processor else result
660663
return obj
661664

662665

0 commit comments

Comments
 (0)