From 1d5c6256cf2f2b87717592844656e9eb372b9f02 Mon Sep 17 00:00:00 2001 From: emontnemery Date: Mon, 30 Sep 2019 22:50:20 +0200 Subject: [PATCH 1/6] Add support for custom serializer --- voluptuous_serialize/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/voluptuous_serialize/__init__.py b/voluptuous_serialize/__init__.py index 110eafe..0fc782f 100644 --- a/voluptuous_serialize/__init__.py +++ b/voluptuous_serialize/__init__.py @@ -12,12 +12,17 @@ } -def convert(schema): +def convert(schema, custom_serializer=None): """Convert a voluptuous schema to a dictionary.""" # pylint: disable=too-many-return-statements,too-many-branches if isinstance(schema, vol.Schema): schema = schema.schema + if custom_serializer: + val = custom_serializer(schema) + if val: + return val + if isinstance(schema, Mapping): val = [] @@ -29,7 +34,7 @@ def convert(schema): else: pkey = key - pval = convert(value) + pval = convert(value, custom_serializer) pval['name'] = pkey if description is not None: pval['description'] = description @@ -47,7 +52,7 @@ def convert(schema): if isinstance(schema, vol.All): val = {} for validator in schema.validators: - val.update(convert(validator)) + val.update(convert(validator, custom_serializer)) return val if isinstance(schema, (vol.Clamp, vol.Range)): From b7ebaee1415d0ae42d1dc8fc0567b0b51988d5fe Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 1 Oct 2019 06:37:07 +0200 Subject: [PATCH 2/6] Update voluptuous_serialize/__init__.py Co-Authored-By: Paulus Schoutsen --- voluptuous_serialize/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/voluptuous_serialize/__init__.py b/voluptuous_serialize/__init__.py index 0fc782f..023084e 100644 --- a/voluptuous_serialize/__init__.py +++ b/voluptuous_serialize/__init__.py @@ -12,7 +12,7 @@ } -def convert(schema, custom_serializer=None): +def convert(schema, *, custom_serializer=None): """Convert a voluptuous schema to a dictionary.""" # pylint: disable=too-many-return-statements,too-many-branches if isinstance(schema, vol.Schema): From 513f35561f4a91d778744827820cdac88ff3af7e Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 1 Oct 2019 06:37:26 +0200 Subject: [PATCH 3/6] Update voluptuous_serialize/__init__.py Co-Authored-By: Paulus Schoutsen --- voluptuous_serialize/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/voluptuous_serialize/__init__.py b/voluptuous_serialize/__init__.py index 023084e..659a148 100644 --- a/voluptuous_serialize/__init__.py +++ b/voluptuous_serialize/__init__.py @@ -20,7 +20,7 @@ def convert(schema, *, custom_serializer=None): if custom_serializer: val = custom_serializer(schema) - if val: + if val not is UNSUPPORTED: return val if isinstance(schema, Mapping): From 9ba9a481310ccc3553773178ebc1b96cf4bfe975 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 1 Oct 2019 06:37:34 +0200 Subject: [PATCH 4/6] Update voluptuous_serialize/__init__.py Co-Authored-By: Paulus Schoutsen --- voluptuous_serialize/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/voluptuous_serialize/__init__.py b/voluptuous_serialize/__init__.py index 659a148..36dd735 100644 --- a/voluptuous_serialize/__init__.py +++ b/voluptuous_serialize/__init__.py @@ -34,7 +34,7 @@ def convert(schema, *, custom_serializer=None): else: pkey = key - pval = convert(value, custom_serializer) + pval = convert(value, custom_serializer=custom_serializer) pval['name'] = pkey if description is not None: pval['description'] = description From 61e868f1823aca008bf096fc0d7df6e80a49b7d3 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 1 Oct 2019 06:37:43 +0200 Subject: [PATCH 5/6] Update voluptuous_serialize/__init__.py Co-Authored-By: Paulus Schoutsen --- voluptuous_serialize/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/voluptuous_serialize/__init__.py b/voluptuous_serialize/__init__.py index 36dd735..548e5e1 100644 --- a/voluptuous_serialize/__init__.py +++ b/voluptuous_serialize/__init__.py @@ -52,7 +52,7 @@ def convert(schema, *, custom_serializer=None): if isinstance(schema, vol.All): val = {} for validator in schema.validators: - val.update(convert(validator, custom_serializer)) + val.update(convert(validator, custom_serializer=custom_serializer)) return val if isinstance(schema, (vol.Clamp, vol.Range)): From a66c4219bf7b096ab56c43b611ff764bdd73908a Mon Sep 17 00:00:00 2001 From: emontnemery Date: Tue, 1 Oct 2019 06:50:01 +0200 Subject: [PATCH 6/6] Add sentinel object --- voluptuous_serialize/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/voluptuous_serialize/__init__.py b/voluptuous_serialize/__init__.py index 548e5e1..b12d597 100644 --- a/voluptuous_serialize/__init__.py +++ b/voluptuous_serialize/__init__.py @@ -11,6 +11,7 @@ bool: 'boolean', } +UNSUPPORTED = object() def convert(schema, *, custom_serializer=None): """Convert a voluptuous schema to a dictionary.""" @@ -20,7 +21,7 @@ def convert(schema, *, custom_serializer=None): if custom_serializer: val = custom_serializer(schema) - if val not is UNSUPPORTED: + if val is not UNSUPPORTED: return val if isinstance(schema, Mapping):