Skip to content

Varied verbosity of data with a canonical form #9

@neithere

Description

@neithere

Automated rule-based transformation (e.g. unfolding).

Use Case

A model contains nested structures allowing variable verbosity:

need: foo

need: foo
plan: bar

need: foo
plan: [ bar ]

need: foo
plan:
- action: bar

The plan key has a strictly defined set of variations.

The existing function monk.manipulation.unfold_list_of_dicts,
applied to the value, predictably converts it to the verbose form.

Problem

In its current state Monk supports the use case via a couple of functions that
must be applied to the value before its validation:

schema = [str]
value = 'foo'
transformed_value = monk.manipulation.unfold_to_list(value)
validate(schema, transformed_value)

A schema therefore only describes the most verbose of the variations.

It is important that all variations of the value are covered by the schema
in a uniform manner.

NOTE: this is not about a kind of an or operator; this is about
variations of incoming data with a "canonical" verbose form to which the data
can/should be automatically converted. This point is not very clear and needs
to be thinked over.

Proposed Examples

>>> schema = Rule(list, inner_spec=str, merger=unfold_to_list)
>>> raw_data = 'foo'
>>> transformed_data = transform(schema, raw_data)
>>> print(transformed_data)
['foo']

>>> validate(schema, raw_data)
Traceback (most recent call last):
...
TypeError: expected list, got str 'foo'

>>> validate(schema, transformed_data)

# OK

A more complex example of the same behaviour:

>>> merger = lambda value: monk.manipulation.unfold_list_of_dicts(value, 'foo')
>>> schema = Rule(list, inner_spec=str, merger=merger)
>>> raw_data = 'bar'
>>> transformed_data = transform(schema, raw_data)
>>> print(transformed_data)
[{'foo': 'bar'}]

Implementation

The existing functions unfold_to_list and unfold_list_of_dicts
probably should be converted to a more powerful universal merger function
(or set of functions) that can predictably generate structures based on
given Rule instance and value.

The schema is defined in a uniform way for validation and manipulation
as it describes the possible variations of values.

Open question: whether validate() should call the merger or it should be done
manually before validation.

Note: This issue has been automatically migrated from Bitbucket
Created by @neithere on 2013-05-16 22:11:51+00:00

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions