From 9780f36e37205fdc6107a0d806034ba9cc7d863c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Thu, 3 Aug 2017 14:42:25 +0200 Subject: [PATCH] config: Reduce duplication in schema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds a helper function for easy creation of a structure where either X or list of Xs is accepted. Signed-off-by: Lubomír Sedlář --- pungi/checks.py | 58 ++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 39 deletions(-) diff --git a/pungi/checks.py b/pungi/checks.py index 9fb820d5..71c45247 100644 --- a/pungi/checks.py +++ b/pungi/checks.py @@ -454,29 +454,14 @@ def make_schema(): ] }, - "list_of_repos": { - "type": "array", - "items": {"$ref": "#/definitions/repo"}, - }, - - "repos": { - "anyOf": [ - {"$ref": "#/definitions/repo"}, - {"$ref": "#/definitions/list_of_repos"}, - ] - }, + "repos": _one_or_list({"$ref": "#/definitions/repo"}), "list_of_strings": { "type": "array", "items": {"type": "string"}, }, - "strings": { - "anyOf": [ - {"type": "string"}, - {"$ref": "#/definitions/list_of_strings"}, - ] - }, + "strings": _one_or_list({"type": "string"}), "optional_string": { "anyOf": [ @@ -921,17 +906,9 @@ def make_schema(): "additionalProperties": False, }), - "live_images": _variant_arch_mapping({ - "anyOf": [ - {"$ref": "#/definitions/live_image_config"}, - { - "type": "array", - "items": { - "$ref": "#/definitions/live_image_config" - } - } - ] - }), + "live_images": _variant_arch_mapping( + _one_or_list({"$ref": "#/definitions/live_image_config"}) + ), "image_build": { "type": "object", @@ -1014,17 +991,7 @@ def make_schema(): # Warning: this pattern is a variant uid regex, but the # format does not let us validate it as there is no regular # expression to describe all regular expressions. - ".+": { - "anyOf": [ - {"$ref": "#/definitions/osbs_config"}, - { - "type": "array", - "items": { - "$ref": "#/definitions/osbs_config", - }, - }, - ], - }, + ".+": _one_or_list({"$ref": "#/definitions/osbs_config"}), }, "additionalProperties": False, }, @@ -1102,6 +1069,19 @@ def _variant_arch_mapping(value): } +def _one_or_list(value): + """Require either `value` or a list of `value`s.""" + return { + "anyOf": [ + value, + { + "type": "array", + "items": value, + }, + ], + } + + # This is a mapping of configuration option dependencies and conflicts. # # The key in this mapping is the trigger for the check. When the option is