Exemplo n.º 1
0
def _check_casing_endpoints(endpoints: List[swagger_to.intermediate.Endpoint]) -> List[Complaint]:
    """
    Check whether the endpoints conform to the casing conventions.

    :param endpoints: swagger endpoint definitions
    :return: the list of failed checks
    """
    complaints = []  # type: List[Complaint]

    for endpoint in endpoints:
        if endpoint.path != swagger_to.snake_case(endpoint.path):
            complaints.append(
                Complaint(
                    message="Path doesn't conform to snake case (e.g. snake_case)",
                    what=endpoint.path,
                    where="In endpoint {}".format(endpoint.operation_id),
                    line=endpoint.line))
        if endpoint.operation_id != swagger_to.snake_case(endpoint.operation_id):
            complaints.append(
                Complaint(
                    message="Endpoint operation ID is not a snake case identifier (e.g. snake_case)",
                    what=endpoint.operation_id,
                    where="In endpoint {}".format(endpoint.operation_id),
                    line=endpoint.line))
        for param in endpoint.parameters:
            if param.name != swagger_to.snake_case(param.name):
                complaints.append(
                    Complaint(
                        message="Parameter has not a snake case identifier (e.g. snake_case)",
                        what=param.name,
                        where="In endpoint {}, parameter {}".format(endpoint.operation_id, param.name),
                        line=param.line))

    return complaints
Exemplo n.º 2
0
def _check_header(swagger: swagger_to.swagger.Swagger) -> List[Complaint]:
    """
    Check whether the swagger header conforms to our style guide.

    :param swagger: parsed Swagger spec
    :return: the list of failed checks
    """
    complaints = []  # type: List[Complaint]

    if swagger.name != swagger_to.snake_case(swagger.name):
        complaints.append(
            Complaint(
                message=
                "Name of the Swagger specification is not snake case (e.g. snake_case)",
                what=swagger.name,
                where="In the Swagger header",
                line=1))

    if not swagger.base_path.startswith("/"):
        complaints.append(
            Complaint(message="Swagger base path doesn't start with a slash",
                      what=swagger.base_path,
                      where="In the Swagger header",
                      line=1))

    if swagger.description.capitalize() != swagger.description:
        complaints.append(
            Complaint(message="Swagger description should be capitalized",
                      what=swagger.description,
                      where="In the Swagger header",
                      line=1))

    return complaints
Exemplo n.º 3
0
def _check_recursively_cases(typedef: swagger_to.intermediate.Typedef,
                             visited: MutableSet[swagger_to.intermediate.Typedef]) -> List[Complaint]:
    """
    Check the typedef's adherence to the casing conventions.

    :param typedef: to be translated
    :param visited: already seen typedefs
    :return: the list of failed checks
    """
    complaints = []  # type: List[Complaint]
    if typedef in visited:
        return complaints

    visited.add(typedef)

    if isinstance(typedef, swagger_to.intermediate.Primitivedef):
        pass

    elif isinstance(typedef, swagger_to.intermediate.Arraydef):
        if typedef.identifier != "" and typedef.identifier != swagger_to.capital_camel_case(typedef.identifier):
            complaints.append(
                Complaint(
                    message="Not a capital camel case identifier (e.g. CamelCase)",
                    what=typedef.identifier,
                    where="In array {}".format(typedef.identifier),
                    line=typedef.line))
        complaints.extend(_check_recursively_cases(typedef=typedef.items, visited=visited))

    elif isinstance(typedef, swagger_to.intermediate.Mapdef):
        if typedef.identifier != "" and typedef.identifier != swagger_to.capital_camel_case(typedef.identifier):
            complaints.append(
                Complaint(
                    message="Not a capital camel case identifier (e.g. CamelCase)",
                    what=typedef.identifier,
                    where="In map {}".format(typedef.identifier),
                    line=typedef.line))
        complaints.extend(_check_recursively_cases(typedef=typedef.values, visited=visited))

    elif isinstance(typedef, swagger_to.intermediate.Objectdef):

        if typedef.identifier != "" and typedef.identifier != swagger_to.capital_camel_case(typedef.identifier):
            complaints.append(
                Complaint(
                    message="Not a capital camel case identifier (e.g. CamelCase)",
                    what=typedef.identifier,
                    where="In object {}".format(typedef.identifier),
                    line=typedef.line))

        for prop in typedef.properties.values():
            if prop.name != swagger_to.snake_case(prop.name):
                complaints.append(
                    Complaint(
                        message="Not a snake case identifier (e.g. snake_case)",
                        what=prop.name,
                        where="In object {}, property {}".format(typedef.identifier, prop.name),
                        line=typedef.line))
            complaints.extend(_check_recursively_cases(typedef=prop.typedef, visited=visited))

    return complaints
Exemplo n.º 4
0
def _write_encoder(typedef: Typedef, fid: TextIO) -> None:
    """
    Write the Encoder in the Elm code.

    :param typedef: to be encoded
    :param fid: target
    :return:
    """
    if typedef.identifier == '':
        raise ValueError(
            "Expected a typedef with an identifier, but got a typedef with an empty identifier."
        )

    if isinstance(typedef, Recorddef):
        obj_name = 'a' + typedef.identifier
        fid.write("encode{0} : {0} -> Json.Encode.Value\n".format(
            typedef.identifier))
        fid.write("encode{} {} =\n".format(typedef.identifier, obj_name))
        fid.write(INDENT + 'Json.Encode.object\n')
        prefix = 2 * INDENT + '[ '
        for prop in typedef.properties.values():
            fid.write(
                prefix +
                '( "{}", '.format(swagger_to.snake_case(identifier=prop.name)))

            if prop.typedef is None:
                raise ValueError(
                    "Unexpected None typedef of prop {!r} in type {!r}".format(
                        prop.name, typedef.identifier))

            encoder = _type_encoder(typedef=prop.typedef,
                                    path='{}.{}'.format(
                                        typedef.identifier, prop.name))
            if not prop.required:
                fid.write('Json.Encode.Extra.maybe ({}) '.format(encoder))
            else:
                fid.write('{} '.format(encoder))
            fid.write('<| {}.{} )\n'.format(
                obj_name, swagger_to.camel_case(identifier=prop.name)))
            prefix = 2 * INDENT + ', '

        fid.write(2 * INDENT + "]")

    elif isinstance(typedef,
                    (Booldef, Intdef, Floatdef, Stringdef, Listdef, Dictdef)):
        bracketed_type_expression = _argument_expression(
            typedef=typedef, path=typedef.identifier)
        var_name = 'a' + typedef.identifier

        fid.write("encode{} : {} -> Json.Encode.Value \n".format(
            typedef.identifier, bracketed_type_expression))
        fid.write("encode{} {} =\n".format(typedef.identifier, var_name))
        fid.write(INDENT + "{}".format(
            _type_encoder(typedef=typedef, path=typedef.identifier)))
        fid.write(" <| {}".format(var_name))

    else:
        raise AssertionError("Unexpected type {}.".format(typedef.__class__))
Exemplo n.º 5
0
def _write_decoder(typedef: Typedef, fid: TextIO) -> None:
    """
    Write the Decoder in the Elm code.

    :param typedef: to be decoded
    :param fid: target
    :return:
    """
    if typedef.identifier == '':
        raise ValueError(
            "Expected a typedef with an identifier, but got a typedef with an empty identifier."
        )

    if isinstance(typedef, Recorddef):
        record_id = typedef.identifier
        fid.write("decode{0} : Json.Decode.Decoder {0}\n".format(record_id))
        fid.write("decode{} =\n".format(record_id))
        fid.write(INDENT +
                  'Json.Decode.Pipeline.decode {}\n'.format(record_id))
        prefix = 2 * INDENT + '|> '

        for prop in typedef.properties.values():
            snake_case_name = swagger_to.snake_case(identifier=prop.name)

            if prop.typedef is None:
                raise ValueError(
                    "Unexpected None typedef of prop {!r} in type {!r}".format(
                        prop.name, typedef.identifier))

            decoder = _argument_decoder_expression(typedef=prop.typedef,
                                                   path='{}.{}'.format(
                                                       record_id, prop.name))
            if prop.required:
                fid.write(prefix +
                          'Json.Decode.Pipeline.required "{}" {}\n'.format(
                              snake_case_name, decoder))
            else:
                fid.write(
                    prefix +
                    'Json.Decode.Pipeline.optional "{}" (Json.Decode.nullable {}) Nothing\n'
                    .format(snake_case_name, decoder))

    elif isinstance(typedef,
                    (Booldef, Intdef, Floatdef, Stringdef, Listdef, Dictdef)):
        bracketed_type_expression = _argument_expression(
            typedef=typedef, path=typedef.identifier)
        fid.write("decode{} : Json.Decode.Decoder {}\n".format(
            typedef.identifier, bracketed_type_expression))
        fid.write("decode{} =\n".format(typedef.identifier))
        fid.write(INDENT + "{}".format(
            _type_decoder(typedef=typedef, path=typedef.identifier)))

    else:
        raise AssertionError("Unexpected type {}.".format(typedef.__class__))
Exemplo n.º 6
0
    def test_snake_case(self):
        table = [
            ('CvSizeInt', 'cv_size_int'),
            ('URLsToFind', 'urls_to_find'),
            ('IDsToFind', 'ids_to_find'),
            ('some_ids', 'some_ids'),
            ('someIDs', 'some_ids'),
        ]

        for an_input, expected in table:
            got = swagger_to.snake_case(identifier=an_input)
            self.assertEqual(expected, got)