@@ -297,6 +297,15 @@ def _add_common_options(self):
297
297
'key-value pair; dot notation for nested JSON is '
298
298
'supported.' ))
299
299
300
+ # Flag to opt-in to functionality introduced in PR #3670. More robust parsing
301
+ # of complex datatypes is planned for 2.6, so this flag will be deprecated soon
302
+ detail_arg_grp .add_argument ('--auto-dict' , action = 'store_true' , dest = 'auto_dict' ,
303
+ default = False , help = 'Automatically convert list items to '
304
+ 'dictionaries when colons are detected. '
305
+ '(NOTE - this parameter and its functionality will be '
306
+ 'deprecated in the next release in favor of a more '
307
+ 'robust conversion method)' )
308
+
300
309
return root_arg_grp
301
310
302
311
def _print_execution_details (self , execution , args , ** kwargs ):
@@ -523,7 +532,7 @@ def transform_object(value):
523
532
result [key ] = value
524
533
return result
525
534
526
- def transform_array (value , action_params = None ):
535
+ def transform_array (value , action_params = None , auto_dict = False ):
527
536
action_params = action_params or {}
528
537
529
538
# Sometimes an array parameter only has a single element:
@@ -555,13 +564,14 @@ def transform_array(value, action_params=None):
555
564
556
565
# When each values in this array represent dict type, this converts
557
566
# the 'result' to the dict type value.
558
- if all ([isinstance (x , str ) and ':' in x for x in result ]):
567
+ if all ([isinstance (x , str ) and ':' in x for x in result ]) and auto_dict :
559
568
result_dict = {}
560
569
for (k , v ) in [x .split (':' ) for x in result ]:
561
570
# To parse values using the 'transformer' according to the type which is
562
571
# specified in the action metadata, calling 'normalize' method recursively.
563
572
if 'properties' in action_params and k in action_params ['properties' ]:
564
- result_dict [k ] = normalize (k , v , action_params ['properties' ])
573
+ result_dict [k ] = normalize (k , v , action_params ['properties' ],
574
+ auto_dict = auto_dict )
565
575
else :
566
576
result_dict [k ] = v
567
577
return [result_dict ]
@@ -591,7 +601,7 @@ def get_param_type(key, action_params=None):
591
601
592
602
return None
593
603
594
- def normalize (name , value , action_params = None ):
604
+ def normalize (name , value , action_params = None , auto_dict = False ):
595
605
""" The desired type is contained in the action meta-data, so we can look that up
596
606
and call the desired "caster" function listed in the "transformer" dict
597
607
"""
@@ -609,7 +619,7 @@ def normalize(name, value, action_params=None):
609
619
# also leverage that to cast each array item to the correct type.
610
620
param_type = get_param_type (name , action_params )
611
621
if param_type == 'array' and name in action_params :
612
- return transformer [param_type ](value , action_params [name ])
622
+ return transformer [param_type ](value , action_params [name ], auto_dict = auto_dict )
613
623
elif param_type :
614
624
return transformer [param_type ](value )
615
625
@@ -649,9 +659,9 @@ def normalize(name, value, action_params=None):
649
659
else :
650
660
# This permits multiple declarations of argument only in the array type.
651
661
if get_param_type (k ) == 'array' and k in result :
652
- result [k ] += normalize (k , v )
662
+ result [k ] += normalize (k , v , auto_dict = args . auto_dict )
653
663
else :
654
- result [k ] = normalize (k , v )
664
+ result [k ] = normalize (k , v , auto_dict = args . auto_dict )
655
665
656
666
except Exception as e :
657
667
# TODO: Move transformers in a separate module and handle
0 commit comments