mirror of https://github.com/zulip/zulip.git
data_type: Add StringDict data type.
StringDict is a data type for representing dictionaries where all keys and values are strings. Add this data type to data_types.py and edit other files so that this data type is put to use and tested. (slightly tweaked by @showell to remove a comment and shorten a var name now that we have a proper data type)
This commit is contained in:
parent
78a2059b8d
commit
91ca1afe98
|
@ -47,6 +47,7 @@ from zerver.lib.data_types import (
|
|||
EnumType,
|
||||
ListType,
|
||||
NumberType,
|
||||
StringDictType,
|
||||
UnionType,
|
||||
make_checker,
|
||||
schema,
|
||||
|
@ -62,9 +63,6 @@ SKIP_LIST = [
|
|||
|
||||
|
||||
EXEMPT_OPENAPI_NAMES = [
|
||||
# bots
|
||||
"realm_bot_add_event",
|
||||
"realm_bot_update_event",
|
||||
# users field missing
|
||||
"update_display_settings_event",
|
||||
"update_global_notifications_event",
|
||||
|
@ -132,6 +130,8 @@ def from_openapi(node: Dict[str, Any]) -> Any:
|
|||
# this might be a glitch in our current spec? or
|
||||
# maybe I just understand it yet
|
||||
if isinstance(node["additionalProperties"], dict):
|
||||
if node["additionalProperties"].get("type", "") == "string":
|
||||
return StringDictType()
|
||||
return from_openapi(node["additionalProperties"])
|
||||
|
||||
if "properties" not in node:
|
||||
|
|
|
@ -121,6 +121,21 @@ class ListType:
|
|||
return f"{var_name} (list):\n{indent(sub_schema)}"
|
||||
|
||||
|
||||
@dataclass
|
||||
class StringDictType:
|
||||
def check_data(self, var_name: str, val: Dict[Any, Any]) -> None:
|
||||
if not isinstance(val, dict):
|
||||
raise AssertionError(f"{var_name} is not a dictionary")
|
||||
|
||||
for key, value in val.items():
|
||||
if isinstance(key, str) and isinstance(value, str):
|
||||
continue
|
||||
raise AssertionError(f"{var_name} is not a string:string dictionary")
|
||||
|
||||
def schema(self, var_name: str) -> str:
|
||||
return f"{var_name}: string_dict"
|
||||
|
||||
|
||||
@dataclass
|
||||
class OptionalType:
|
||||
sub_type: Any
|
||||
|
|
|
@ -14,6 +14,7 @@ from zerver.lib.data_types import (
|
|||
ListType,
|
||||
NumberType,
|
||||
OptionalType,
|
||||
StringDictType,
|
||||
UnionType,
|
||||
UrlType,
|
||||
check_data,
|
||||
|
@ -313,16 +314,13 @@ bot_services_outgoing_type = DictType(
|
|||
]
|
||||
)
|
||||
|
||||
# We use a strict check here, because our tests
|
||||
# don't specifically focus on seeing how
|
||||
# flexible we can make the types be for config_data.
|
||||
_ad_hoc_config_data_schema = Equals(dict(foo="bar"))
|
||||
config_data_schema = StringDictType()
|
||||
|
||||
bot_services_embedded_type = DictType(
|
||||
required_keys=[
|
||||
# force vertical
|
||||
("service_name", str),
|
||||
("config_data", _ad_hoc_config_data_schema),
|
||||
("config_data", config_data_schema),
|
||||
]
|
||||
)
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ from zerver.lib.data_types import (
|
|||
ListType,
|
||||
NumberType,
|
||||
OptionalType,
|
||||
StringDictType,
|
||||
UnionType,
|
||||
UrlType,
|
||||
schema,
|
||||
|
@ -27,12 +28,14 @@ class MiscTest(ZulipTestCase):
|
|||
("flag", bool),
|
||||
("level", EnumType([1, 2, 3])),
|
||||
("lst", ListType(int)),
|
||||
("config", StringDictType()),
|
||||
("value", UnionType([int, str])),
|
||||
("url", UrlType()),
|
||||
]
|
||||
)
|
||||
expected = """
|
||||
test (dict):
|
||||
config: string_dict
|
||||
flag: bool
|
||||
level in [1, 2, 3]
|
||||
lst (list):
|
||||
|
|
Loading…
Reference in New Issue