Ejemplo n.º 1
0
def type_(*, schema: types.Schema, schemas: types.Schemas) -> str:
    """
    Get the type of the schema.

    Raises TypeMissingError if the final schema does not have a type or the value is
    not a string or list of string or has multiple non-null types.

    Args:
        schema: The schema for which to get the type.
        schemas: The schemas for $ref lookup.

    Returns:
        The type of the schema.

    """
    value = peek_key(schema=schema,
                     schemas=schemas,
                     key=types.OpenApiProperties.TYPE)
    if value is None:
        raise exceptions.TypeMissingError("Every property requires a type.")

    if isinstance(value, str):
        return value

    if isinstance(value, list):
        # ignore null
        type_values = filter(lambda item: item != "null", value)

        try:
            item_value = next(type_values)
        except StopIteration as exc:
            raise exceptions.TypeMissingError(
                "An array type property must have at least 1 element that is not "
                "'null'.") from exc

        try:
            next(type_values)
            raise exceptions.TypeMissingError(
                "An array type property must have at most 1 element that is not "
                "'null'.")
        except StopIteration:
            pass

        if isinstance(item_value, str):
            return item_value

    raise exceptions.TypeMissingError(
        "A type property value must be of type string or list of string.")
Ejemplo n.º 2
0
def _determine_type(*,
                    spec: types.Schema) -> sqlalchemy.sql.type_api.TypeEngine:
    """
    Determine the type for a specification.

    If no type is found, raises TypeMissingError. If the type is found but is not
    handled, raises FeatureNotImplementedError.

    Args:
        spec: The specification to determine the type for.

    Returns:
        The type for the specification.

    """
    # Checking for type
    spec_type = spec.get("type")
    if spec_type is None:
        raise exceptions.TypeMissingError("Every property requires a type.")

    # Determining the type
    type_: typing.Optional[sqlalchemy.sql.type_api.TypeEngine] = None
    if spec_type == "integer":
        type_ = _handle_integer(spec=spec)
    elif spec_type == "number":
        type_ = _handle_number(spec=spec)
    elif spec_type == "string":
        type_ = _handle_string(spec=spec)
    elif spec_type == "boolean":
        type_ = sqlalchemy.Boolean

    if type_ is None:
        raise exceptions.FeatureNotImplementedError(
            f"{spec['type']} has not been implemented")
    return type_
Ejemplo n.º 3
0
def type_(*, schema: types.Schema, schemas: types.Schemas) -> str:
    """
    Get the type of the schema.

    Raises TypeMissingError if the final schema does not have a type or the value is
    not a string.

    Args:
        schema: The schema for which to get the type.
        schemas: The schemas for $ref lookup.

    Returns:
        The type of the schema.

    """
    value = peek_key(schema=schema, schemas=schemas, key="type")
    if value is None:
        raise exceptions.TypeMissingError("Every property requires a type.")
    if not isinstance(value, str):
        raise exceptions.TypeMissingError(
            "A type property value must be of type string.")
    return value
Ejemplo n.º 4
0
def peek_type(*, schema: types.Schema, schemas: types.Schemas) -> str:
    """
    Get the type of the schema.

    Raises TypeMissingError if the final schema does not have a type.

    Args:
        schema: The schema for which to get the type.
        schemas: The schemas for $ref lookup.

    Returns:
        The type of the schema.

    """
    type_ = _peek_type(schema=schema, schemas=schemas)
    if type_ is None:
        raise exceptions.TypeMissingError("Every property requires a type.")
    return type_