1010import re
1111import sys
1212from collections import defaultdict
13+ from collections .abc import Callable
14+ from dataclasses import dataclass
1315from getpass import getpass
1416from os import environ , path
15- from typing import Any , Dict , List , Optional , Tuple , Union
17+ from typing import Any , Dict , List , Optional , Tuple
1618from urllib .parse import urlparse
1719
1820import openapi3 .paths
@@ -49,46 +51,12 @@ def parse_boolean(value: str) -> bool:
4951 raise argparse .ArgumentTypeError ("Expected a boolean value" )
5052
5153
52- def parse_dict (
53- value : str ,
54- ) -> Union [Dict [str , Any ], "ExplicitEmptyDictValue" , "ExplicitEmptyListValue" ]:
55- """
56- A helper function to decode incoming JSON data as python dicts. This is
57- intended to be passed to the `type=` kwarg for ArgumentParaser.add_argument.
58-
59- :param value: The json string to be parsed into dict.
60- :type value: str
61-
62- :returns: The dict value of the input.
63- :rtype: dict, ExplicitEmptyDictValue, or ExplicitEmptyListValue
64- """
65- if not isinstance (value , str ):
66- raise argparse .ArgumentTypeError ("Expected a JSON string" )
67-
68- try :
69- result = json .loads (value )
70- except Exception as e :
71- raise argparse .ArgumentTypeError ("Expected a JSON string" ) from e
72-
73- # This is necessary because empty dicts and lists are excluded from requests
74- # by default, but we still want to support user-defined empty dict
75- # strings. This is particularly helpful when updating LKE node pool
76- # labels and taints.
77- if isinstance (result , dict ) and result == {}:
78- return ExplicitEmptyDictValue ()
79-
80- if isinstance (result , list ) and result == []:
81- return ExplicitEmptyListValue ()
82-
83- return result
84-
85-
8654TYPES = {
8755 "string" : str ,
8856 "integer" : int ,
8957 "boolean" : parse_boolean ,
9058 "array" : list ,
91- "object" : parse_dict ,
59+ "object" : lambda value : ExplicitJsonValue ( json_value = json . loads ( value )) ,
9260 "number" : float ,
9361}
9462
@@ -112,7 +80,16 @@ class ExplicitEmptyDictValue:
11280 """
11381
11482
115- def wrap_parse_nullable_value (arg_type : str ) -> TYPES :
83+ @dataclass
84+ class ExplicitJsonValue :
85+ """
86+ A special type used to explicitly pass raw JSON from user input as is.
87+ """
88+
89+ json_value : Any
90+
91+
92+ def wrap_parse_nullable_value (arg_type : str ) -> Callable [[Any ], Any ]:
11693 """
11794 A helper function to parse `null` as None for nullable CLI args.
11895 This is intended to be called and passed to the `type=` kwarg for ArgumentParser.add_argument.
0 commit comments