Esempio n. 1
0
def to_wire(swagger_spec, primitive_spec, value):
    """Converts a python primitive or object to a reasonable wire
    representation if it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :param value: primitive to convert to wire representation
    :type value: int, long, float, boolean, string, unicode, object, etc
    :rtype: int, long, float, boolean, string, unicode, etc
    :raises: SwaggerMappingError when format.to_wire raises an exception
    """
    if value is None or not schema.has_format(swagger_spec, primitive_spec):
        return value
    format_name = schema.get_format(swagger_spec, primitive_spec)
    formatter = swagger_spec.get_format(format_name)

    try:
        return formatter.to_wire(value) if formatter else value
    except Exception as e:
        raise SwaggerMappingError(
            'Error while marshalling value={} to type={}{}.'.format(
                value, primitive_spec['type'],
                '/{}'.format(primitive_spec['format']) if 'format' in primitive_spec else '',
            ),
            e,
        )
Esempio n. 2
0
def to_wire(swagger_spec, primitive_spec, value):
    """Converts a python primitive or object to a reasonable wire
    representation if it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :param value: primitive to convert to wire representation
    :type value: int, long, float, boolean, string, unicode, object, etc
    :rtype: int, long, float, boolean, string, unicode, etc
    :raises: SwaggerMappingError when format.to_wire raises an exception
    """
    if value is None or not schema.has_format(swagger_spec, primitive_spec):
        return value
    format_name = schema.get_format(swagger_spec, primitive_spec)
    formatter = swagger_spec.get_format(format_name)

    try:
        return formatter.to_wire(value) if formatter else value
    except Exception as e:
        raise SwaggerMappingError(
            'Error while marshalling value={} to type={}{}.'.format(
                value, primitive_spec['type'],
                '/{}'.format(primitive_spec['format']) if 'format' in primitive_spec else '',
            ),
            e,
        )
Esempio n. 3
0
def to_python(spec, value):
    """Converts a value in wire format to its python representation given
    the 'format' in the given spec.

    :param spec: spec for a primitive type as a dict
    :type value: int, long, float, boolean, string, unicode, etc
    :rtype: int, long, float, boolean, string, object, etc
    """
    if value is None or not schema.has_format(spec):
        return value
    formatter = get_format(schema.get_format(spec))
    return formatter.to_python(value) if formatter else value
Esempio n. 4
0
def to_python(swagger_spec, primitive_spec, value):
    """Converts a value in wire format to its python representation if
     it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :type value: int, long, float, boolean, string, unicode, etc
    :rtype: int, long, float, boolean, string, object, etc
    """
    if value is None or not schema.has_format(primitive_spec):
        return value
    formatter = swagger_spec.get_format(schema.get_format(primitive_spec))
    return formatter.to_python(value) if formatter else value
Esempio n. 5
0
def to_python(swagger_spec, primitive_spec, value):
    """Converts a value in wire format to its python representation if
     it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :type value: int, long, float, boolean, string, unicode, etc
    :rtype: int, long, float, boolean, string, object, etc
    """
    if value is None or not schema.has_format(primitive_spec):
        return value
    formatter = swagger_spec.get_format(schema.get_format(primitive_spec))
    return formatter.to_python(value) if formatter else value
Esempio n. 6
0
def to_wire(spec, value):
    """Converts a python primitive or object to a reasonable wire
    representation given the 'format' in the given spec.

    :param spec: spec for a primitive type as a dict
    :type value: int, long, float, boolean, string, unicode, etc
    :rtype: int, long, float, boolean, string, unicode, etc
    """
    if value is None or not schema.has_format(spec):
        return value

    to_wire, _, _ = _formatters[schema.get_format(spec)]
    return to_wire(value)
Esempio n. 7
0
def to_wire(swagger_spec, primitive_spec, value):
    """Converts a python primitive or object to a reasonable wire
    representation if it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :param value: primitive to convert to wire representation
    :type value: int, long, float, boolean, string, unicode, object, etc
    :rtype: int, long, float, boolean, string, unicode, etc
    """
    if value is None or not schema.has_format(swagger_spec, primitive_spec):
        return value
    format_name = schema.get_format(swagger_spec, primitive_spec)
    formatter = swagger_spec.get_format(format_name)
    return formatter.to_wire(value) if formatter else value
Esempio n. 8
0
def to_wire(swagger_spec, primitive_spec, value):
    """Converts a python primitive or object to a reasonable wire
    representation if it has an associated Swagger `format`.

    :type swagger_spec: :class:`bravado_core.spec.Spec`
    :param primitive_spec: spec for a primitive type as a dict
    :param value: primitive to convert to wire representation
    :type value: int, long, float, boolean, string, unicode, object, etc
    :rtype: int, long, float, boolean, string, unicode, etc
    """
    if value is None or not schema.has_format(swagger_spec, primitive_spec):
        return value
    format_name = schema.get_format(swagger_spec, primitive_spec)
    formatter = swagger_spec.get_format(format_name)
    return formatter.to_wire(value) if formatter else value
Esempio n. 9
0
def test_true(minimal_swagger_spec):
    int_spec = {'type': 'integer', 'format': 'int32'}
    assert has_format(minimal_swagger_spec, int_spec)
Esempio n. 10
0
def test_ref_false(minimal_swagger_dict):
    int_spec = {'type': 'integer'}
    minimal_swagger_dict['definitions']['Int'] = int_spec
    ref_spec = {'$ref': '#/definitions/Int'}
    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    assert not has_format(swagger_spec, ref_spec)
Esempio n. 11
0
def test_ref_true(minimal_swagger_dict):
    int32_spec = {'type': 'integer', 'format': 'int32'}
    minimal_swagger_dict['definitions']['Int32'] = int32_spec
    ref_spec = {'$ref': '#/definitions/Int32'}
    swagger_spec = Spec.from_dict(minimal_swagger_dict)
    assert has_format(swagger_spec, ref_spec)
Esempio n. 12
0
def test_false(minimal_swagger_spec):
    int_spec = {'type': 'integer'}
    assert not has_format(minimal_swagger_spec, int_spec)