Ejemplo n.º 1
0
    def test_optional(self) -> None:
        cls = mapry.Class(name='some_class', plural='', description='', ref='')

        cls.properties = {
            "bff":
            mapry.Property(ref='',
                           name='friends',
                           a_type=cls,
                           description='',
                           json='',
                           optional=True,
                           composite=cls)
        }

        self.assertListEqual([cls], mapry.references(a_type=cls))
Ejemplo n.º 2
0
def dummy_property(name: str, a_type: mapry.Type,
                   composite: mapry.Composite) -> mapry.Property:
    """
    Generate a dummy property of a composite.

    :param name: name of the property
    :param a_type: type of the property
    :param composite: composite structure that this property belongs to
    :return: generated dummy property
    """
    return mapry.Property(ref='',
                          name=name,
                          a_type=a_type,
                          description='',
                          json=name,
                          optional=False,
                          composite=composite)
Ejemplo n.º 3
0
def _property_from_mapping(name: str, mapping: Mapping[str, Any],
                           classes: Mapping[str, mapry.Class],
                           embeds: Mapping[str, mapry.Embed], ref: str,
                           composite: mapry.Composite) -> mapry.Property:
    """
    Parse a property from the mapping.

    :param name: of the property
    :param mapping: to be parsed
    :param classes: pointer table to the classes
    :param embeds: pointer table to the embeddable structures
    :param ref: reference to the property in the original JSONable mapry schema
    :param composite: back-reference to the composite
    :return: parsed property
    """
    # pylint: disable=too-many-arguments
    # pylint: disable=too-many-branches
    prop = mapry.Property(ref=ref,
                          name=name,
                          description=mapping['description'],
                          json=mapping.get('json', name),
                          a_type=_recurse_type_from_mapping(mapping=mapping,
                                                            classes=classes,
                                                            embeds=embeds),
                          optional=mapping.get('optional', False),
                          composite=composite)

    prop.ref = ref
    prop.name = name
    prop.description = mapping['description']
    prop.json = mapping.get('json', name)

    prop.type = _recurse_type_from_mapping(mapping=mapping,
                                           classes=classes,
                                           embeds=embeds)

    prop.optional = mapping.get('optional', False)

    return prop
Ejemplo n.º 4
0
def _properties_from_mapping(
        mapping: Mapping[str, Any], classes: Mapping[str, mapry.Class],
        embeds: Mapping[str, mapry.Embed], ref: str,
        composite: mapry.Composite) -> MutableMapping[str, mapry.Property]:
    """
    Parse properties from the given mapping.

    :param mapping: to be parsed
    :param classes: pointer table to the classes
    :param embeds: pointer table to the embeddable structures
    :param ref: reference to the composite in the mapry schema
    :param composite: back-reference to the composite structure
    :return: parsed properties
    """
    properties = collections.OrderedDict(
    )  # type: MutableMapping[str, mapry.Property]

    if isinstance(mapping, collections.OrderedDict):
        mapping_items = list(mapping.items())
    else:
        # The sorting is necessary to make the unit tests a bit less verbose,
        # since defining OrderedDicts is quite tedious.
        mapping_items = sorted(mapping.items())

    for name, property_mapping in mapping_items:
        properties[name] = mapry.Property(
            ref='{}/{}'.format(ref, name),
            name=name,
            description=property_mapping['description'],
            json=property_mapping.get('json', name),
            a_type=_recurse_type_from_mapping(mapping=property_mapping,
                                              classes=classes,
                                              embeds=embeds),
            optional=property_mapping.get('optional', False),
            composite=composite)

    return properties