Exemplo n.º 1
0
def test_struct_type():
    struct_thrift = load_thrift_from_testdir("structThrift.thrift")
    my_int_struct = ThriftSpec(name="myIntStruct",
                               type_info=TI64(),
                               required=True)
    my_other_struct = ThriftSpec(
        name="myOtherStruct",
        type_info=TStruct(
            name="MyOtherStruct",
            fields=[
                ThriftSpec(name="id", type_info=TString(), required=True),
                ThriftSpec(name="ints",
                           type_info=TList(value_type=TI64()),
                           required=True),
            ],
        ),
        required=False,
    )
    expected = ServiceEndpoint(
        name="getMyStruct",
        args=[],
        results=[
            ThriftSpec(
                name="success",
                type_info=TStruct(name="MyStruct",
                                  fields=[my_int_struct, my_other_struct]),
                required=False,
            )
        ],
    )
    result = thrift_parser._parse_thrift_endpoint(
        struct_thrift.__thrift_meta__["services"][0], "getMyStruct")
    assert expected == result
Exemplo n.º 2
0
def _parse_type(type_info):
    try:
        ttype_code, nested_type_info = type_info
    except TypeError:
        ttype_code = type_info
        nested_type_info = None

    ttype = TType._VALUES_TO_NAMES[ttype_code].lower()
    if nested_type_info is None:
        return _BASIC_TYPE_MAP[ttype]()
    elif ttype == "list":
        return TList(value_type=_parse_type(nested_type_info))
    elif ttype == "set":
        return TSet(value_type=_parse_type(nested_type_info))
    elif ttype == "map":
        key, value = nested_type_info
        return TMap(key_type=_parse_type(key), value_type=_parse_type(value))
    elif ttype == "struct":
        return TStruct(
            name=nested_type_info.__name__,
            fields=[
                _parse_arg(result)
                for result in nested_type_info.thrift_spec.values()
            ],
        )
    # Its a basic type but has defined nested type info. its probably an enum
    return TEnum(
        name=nested_type_info.__name__,
        names_to_values=nested_type_info._NAMES_TO_VALUES,
        values_to_names=nested_type_info._VALUES_TO_NAMES,
    )
def test_exception():
    exceptional_thrift = load_thrift_from_testdir("exceptional.thrift")
    expected = ServiceEndpoint(
        name="ping",
        args=[],
        results=[
            ThriftSpec(
                field_id=1,
                name="omg",
                type_info=TStruct(
                    name="OMGException",
                    fields=[
                        ThriftSpec(
                            field_id=1,
                            name="description",
                            type_info=TString(),
                            required=True,
                        )
                    ],
                ),
                required=False,
            )
        ],
    )
    assert expected == thrift_parser._parse_thrift_endpoint(
        exceptional_thrift.__thrift_meta__["services"][0], "ping")
Exemplo n.º 4
0
def superhero_struct():
    return TStruct(
        name="super_hero",
        fields=[
            ThriftSpec(field_id=1,
                       name="name",
                       type_info=TString(),
                       required=True),
            ThriftSpec(
                field_id=2,
                name="villains",
                type_info=TList(value_type=TString()),
                required=False,
            ),
        ],
    )
def test_super_nesting():
    turducken_thrift = load_thrift_from_testdir("turducken.thrift")
    dog_names_to_values = {"GOLDEN": 0, "CORGI": 1, "BASSET": 2}
    dog_enum = TEnum(
        name="DOG",
        names_to_values=dog_names_to_values,
        values_to_names={
            value: key
            for key, value in dog_names_to_values.items()
        },
    )

    outer_value = TI64()
    inner_value = TList(value_type=TString())
    inner_key = TSet(value_type=TList(value_type=dog_enum))
    outer_key = TMap(key_type=inner_key, value_type=inner_value)

    map_field = TMap(key_type=outer_key, value_type=outer_value)

    expected = ServiceEndpoint(
        name="getTheStruct",
        args=[],
        results=[
            ThriftSpec(
                field_id=0,
                name="success",
                type_info=TStruct(
                    name="TheStruct",
                    fields=[
                        ThriftSpec(
                            field_id=1,
                            name="myInsaneStruct",
                            type_info=map_field,
                            required=True,
                        )
                    ],
                ),
                required=False,
            )
        ],
    )

    assert expected == thrift_parser._parse_thrift_endpoint(
        turducken_thrift.__thrift_meta__["services"][0], "getTheStruct")