Example usage for org.apache.thrift.meta_data StructMetaData StructMetaData

List of usage examples for org.apache.thrift.meta_data StructMetaData StructMetaData

Introduction

In this page you can find the example usage for org.apache.thrift.meta_data StructMetaData StructMetaData.

Prototype

public StructMetaData(byte type, Class sClass) 

Source Link

Usage

From source file:com.linecorp.armeria.server.docs.ServiceInfoTest.java

License:Apache License

@Test
public void fooServiceTest() throws Exception {
    final ServiceInfo service = ServiceInfo.of(FooService.class,
            Arrays.asList(//from w w w .j  a  v  a  2 s. c  o  m
                    EndpointInfo.of("*", "/foo", "a", SerializationFormat.THRIFT_BINARY,
                            EnumSet.of(SerializationFormat.THRIFT_BINARY)),
                    EndpointInfo.of("*", "/debug/foo", "b", SerializationFormat.THRIFT_TEXT,
                            EnumSet.of(SerializationFormat.THRIFT_TEXT))),
            ImmutableMap.of(bar3_args.class, new bar3_args().setIntVal(10)),
            ImmutableMap.of("foobar", "barbaz"));

    assertThat(service.endpoints()).hasSize(2);
    // Should be sorted alphabetically
    assertThat(service.endpoints()).containsExactlyInAnyOrder(
            EndpointInfo.of("*", "/debug/foo", "b", SerializationFormat.THRIFT_TEXT,
                    EnumSet.of(SerializationFormat.THRIFT_TEXT)),
            EndpointInfo.of("*", "/foo", "a", SerializationFormat.THRIFT_BINARY,
                    EnumSet.of(SerializationFormat.THRIFT_BINARY)));

    final Map<String, FunctionInfo> functions = service.functions();
    assertThat(functions).hasSize(5);

    final FunctionInfo bar1 = functions.get("bar1");
    assertThat(bar1.parameters()).isEmpty();
    assertThat(bar1.returnType()).isEqualTo(TypeInfo.VOID);
    assertThat(bar1.exceptions()).hasSize(1);
    assertThat(bar1.sampleJsonRequest()).isEmpty();

    final TypeInfo string = TypeInfo.of(ValueType.STRING, false);
    final FunctionInfo bar2 = functions.get("bar2");
    assertThat(bar2.parameters()).isEmpty();
    assertThat(bar2.returnType()).isEqualTo(string);
    assertThat(bar2.exceptions()).hasSize(1);
    assertThat(bar2.sampleJsonRequest()).isEmpty();

    final StructInfo foo = StructInfo.of(new StructMetaData(TType.STRUCT, FooStruct.class));
    final FunctionInfo bar3 = functions.get("bar3");
    assertThat(bar3.parameters()).containsExactly(
            FieldInfo.of("intVal", RequirementType.DEFAULT, TypeInfo.of(ValueType.I32, false)),
            FieldInfo.of("foo", RequirementType.DEFAULT, foo));
    assertThat(bar3.returnType()).isEqualTo(foo);
    assertThat(bar3.exceptions()).hasSize(1);
    assertThatJson(bar3.sampleJsonRequest()).isEqualTo("{\"intVal\": 10}");

    final FunctionInfo bar4 = functions.get("bar4");
    assertThat(bar4.parameters())
            .containsExactly(FieldInfo.of("foos", RequirementType.DEFAULT, ListInfo.of(foo)));
    assertThat(bar4.returnType()).isEqualTo(ListInfo.of(foo));
    assertThat(bar4.exceptions()).hasSize(1);
    assertThat(bar4.sampleJsonRequest()).isEmpty();

    final FunctionInfo bar5 = functions.get("bar5");
    assertThat(bar5.parameters())
            .containsExactly(FieldInfo.of("foos", RequirementType.DEFAULT, MapInfo.of(string, foo)));
    assertThat(bar5.returnType()).isEqualTo(MapInfo.of(string, foo));
    assertThat(bar5.exceptions()).hasSize(1);
    assertThat(bar5.sampleJsonRequest()).isEmpty();

    final String sampleHttpHeaders = service.sampleHttpHeaders();
    assertThat(sampleHttpHeaders).isNotNull();
    assertThatJson(sampleHttpHeaders).isEqualTo("{ \"foobar\": \"barbaz\" }");
}

From source file:com.linecorp.armeria.server.docs.StructInfoTest.java

License:Apache License

@Test
public void testOf() throws Exception {
    final TypeInfo string = TypeInfo.of(ValueType.STRING, false);
    final EnumInfo fooEnum = EnumInfo.of(new EnumMetaData(TType.ENUM, FooEnum.class));
    final StructInfo union = StructInfo.of(new StructMetaData(TType.STRUCT, FooUnion.class));

    final List<FieldInfo> fields = new ArrayList<>();
    fields.add(FieldInfo.of("boolVal", RequirementType.DEFAULT, TypeInfo.of(ValueType.BOOL, false)));
    fields.add(FieldInfo.of("byteVal", RequirementType.DEFAULT, TypeInfo.of(ValueType.BYTE, false)));
    fields.add(FieldInfo.of("i16Val", RequirementType.DEFAULT, TypeInfo.of(ValueType.I16, false)));
    fields.add(FieldInfo.of("i32Val", RequirementType.DEFAULT, TypeInfo.of(ValueType.I32, false)));
    fields.add(FieldInfo.of("i64Val", RequirementType.DEFAULT, TypeInfo.of(ValueType.I64, false)));
    fields.add(FieldInfo.of("doubleVal", RequirementType.DEFAULT, TypeInfo.of(ValueType.DOUBLE, false)));
    fields.add(FieldInfo.of("stringVal", RequirementType.DEFAULT, string));
    fields.add(FieldInfo.of("binaryVal", RequirementType.DEFAULT, TypeInfo.of(ValueType.STRING, true)));
    fields.add(FieldInfo.of("enumVal", RequirementType.DEFAULT, fooEnum));
    fields.add(FieldInfo.of("unionVal", RequirementType.DEFAULT, union));
    fields.add(FieldInfo.of("mapVal", RequirementType.DEFAULT, MapInfo.of(string, fooEnum)));
    fields.add(FieldInfo.of("setVal", RequirementType.DEFAULT, SetInfo.of(union)));
    fields.add(FieldInfo.of("listVal", RequirementType.DEFAULT, ListInfo.of(string)));

    final StructInfo fooStruct = StructInfo.of(new StructMetaData(TType.STRUCT, FooStruct.class));
    assertThat(fooStruct, is(StructInfo.of(FooStruct.class.getName(), fields)));
}

From source file:com.linecorp.armeria.server.thrift.ThriftServiceSpecificationGeneratorTest.java

License:Apache License

@Test
public void testNewServiceInfo() throws Exception {
    final ServiceInfo service = newServiceInfo(FooService.class,
            Arrays.asList(/*from  w w  w.  j av a 2  s . c o m*/
                    new EndpointInfo("*", "/foo", "a", SerializationFormat.THRIFT_BINARY,
                            EnumSet.of(SerializationFormat.THRIFT_BINARY)),
                    new EndpointInfo("*", "/debug/foo", "b", SerializationFormat.THRIFT_TEXT,
                            EnumSet.of(SerializationFormat.THRIFT_TEXT))),
            ImmutableMap.of(bar3_args.class, new bar3_args().setIntVal(10)),
            ImmutableMap.of("foobar", "barbaz"));

    assertThat(service.endpoints()).hasSize(2);
    // Should be sorted alphabetically
    assertThat(service.endpoints()).containsExactlyInAnyOrder(
            new EndpointInfo("*", "/debug/foo", "b", SerializationFormat.THRIFT_TEXT,
                    EnumSet.of(SerializationFormat.THRIFT_TEXT)),
            new EndpointInfo("*", "/foo", "a", SerializationFormat.THRIFT_BINARY,
                    EnumSet.of(SerializationFormat.THRIFT_BINARY)));

    final Map<String, FunctionInfo> functions = service.functions();
    assertThat(functions).hasSize(5);

    final FunctionInfo bar1 = functions.get("bar1");
    assertThat(bar1.parameters()).isEmpty();
    assertThat(bar1.returnTypeInfo()).isEqualTo(TypeInfo.VOID);
    assertThat(bar1.exceptions()).hasSize(1);
    assertThat(bar1.sampleJsonRequest()).isEmpty();

    final TypeInfo string = TypeInfo.STRING;
    final FunctionInfo bar2 = functions.get("bar2");
    assertThat(bar2.parameters()).isEmpty();
    assertThat(bar2.returnTypeInfo()).isEqualTo(string);
    assertThat(bar2.exceptions()).hasSize(1);
    assertThat(bar2.sampleJsonRequest()).isEmpty();

    final StructInfo foo = newStructInfo(new StructMetaData(TType.STRUCT, FooStruct.class), emptyMap());
    final FunctionInfo bar3 = functions.get("bar3");
    assertThat(bar3.parameters()).containsExactly(
            new FieldInfo("intVal", FieldRequirement.DEFAULT, TypeInfo.I32),
            new FieldInfo("foo", FieldRequirement.DEFAULT, foo));
    assertThat(bar3.returnTypeInfo()).isEqualTo(foo);
    assertThat(bar3.exceptions()).hasSize(1);
    assertThatJson(bar3.sampleJsonRequest()).isEqualTo("{\"intVal\": 10}");

    final FunctionInfo bar4 = functions.get("bar4");
    assertThat(bar4.parameters())
            .containsExactly(new FieldInfo("foos", FieldRequirement.DEFAULT, new ListInfo(foo)));
    assertThat(bar4.returnTypeInfo()).isEqualTo(new ListInfo(foo));
    assertThat(bar4.exceptions()).hasSize(1);
    assertThat(bar4.sampleJsonRequest()).isEmpty();

    final FunctionInfo bar5 = functions.get("bar5");
    assertThat(bar5.parameters())
            .containsExactly(new FieldInfo("foos", FieldRequirement.DEFAULT, new MapInfo(string, foo)));
    assertThat(bar5.returnTypeInfo()).isEqualTo(new MapInfo(string, foo));
    assertThat(bar5.exceptions()).hasSize(1);
    assertThat(bar5.sampleJsonRequest()).isEmpty();

    final String sampleHttpHeaders = service.sampleHttpHeaders();
    assertThat(sampleHttpHeaders).isNotNull();
    assertThatJson(sampleHttpHeaders).isEqualTo("{ \"foobar\": \"barbaz\" }");
}

From source file:com.linecorp.armeria.server.thrift.ThriftServiceSpecificationGeneratorTest.java

License:Apache License

@Test
public void testNewStructInfoTest() throws Exception {
    final EnumInfo fooEnum = newEnumInfo(new EnumMetaData(TType.ENUM, FooEnum.class), emptyMap());
    final StructInfo union = newStructInfo(new StructMetaData(TType.STRUCT, FooUnion.class), emptyMap());

    final List<FieldInfo> fields = new ArrayList<>();
    fields.add(new FieldInfo("boolVal", FieldRequirement.DEFAULT, TypeInfo.BOOL));
    fields.add(new FieldInfo("byteVal", FieldRequirement.DEFAULT, TypeInfo.I8));
    fields.add(new FieldInfo("i16Val", FieldRequirement.DEFAULT, TypeInfo.I16));
    fields.add(new FieldInfo("i32Val", FieldRequirement.DEFAULT, TypeInfo.I32));
    fields.add(new FieldInfo("i64Val", FieldRequirement.DEFAULT, TypeInfo.I64));
    fields.add(new FieldInfo("doubleVal", FieldRequirement.DEFAULT, TypeInfo.DOUBLE));
    fields.add(new FieldInfo("stringVal", FieldRequirement.DEFAULT, TypeInfo.STRING));
    fields.add(new FieldInfo("binaryVal", FieldRequirement.DEFAULT, TypeInfo.BINARY));
    fields.add(new FieldInfo("enumVal", FieldRequirement.DEFAULT, fooEnum));
    fields.add(new FieldInfo("unionVal", FieldRequirement.DEFAULT, union));
    fields.add(new FieldInfo("mapVal", FieldRequirement.DEFAULT, new MapInfo(TypeInfo.STRING, fooEnum)));
    fields.add(new FieldInfo("setVal", FieldRequirement.DEFAULT, new SetInfo(union)));
    fields.add(new FieldInfo("listVal", FieldRequirement.DEFAULT, new ListInfo(TypeInfo.STRING)));

    final StructInfo fooStruct = newStructInfo(new StructMetaData(TType.STRUCT, FooStruct.class), emptyMap());
    assertThat(fooStruct).isEqualTo(new StructInfo(FooStruct.class.getName(), fields));
}

From source file:io.nettythrift.protocol.TSimpleJSONProtocol.java

License:Apache License

/**
 * Reading methods./*from   w  w  w .  j a  v  a  2 s. c  om*/
 */
public TMessage readMessageBegin() throws TException {
    byte[] buf = new byte[256];
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    while (true) {
        int readLen = trans_.read(buf, 0, buf.length);
        if (readLen == 0) {
            break;
        }
        out.write(buf, 0, readLen);
        if (readLen < buf.length) {
            break;
        }
    }
    String sb = null;
    try {
        buf = out.toByteArray();
        sb = new String(buf, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }
    // System.out.println("? sb=" + sb);
    // TODO JSON ?
    if (sb.charAt(0) != '[' || sb.charAt(sb.length() - 1) != ']') {
        throw new TProtocolException(TProtocolException.INVALID_DATA, "bad format!");
    }
    JSONArray jsonArray = new JSONArray(sb);
    TMessage msg = new TMessage(jsonArray.getString(0), (byte) jsonArray.getInt(1), jsonArray.getInt(2));
    // System.out.println(msg + ", jsonArray.len = " + jsonArray.length());

    if (jsonArray.length() > 3) {
        if (argsTBaseClass == null) {
            try {
                argsTBaseClass = guessTBaseClassByMethodName(msg.name);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (argsTBaseClass == null) {
            // throw new
            // TProtocolException(TApplicationException.UNKNOWN_METHOD,
            // "Invalid method name: '" + msg.name + "'");
            return new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid);
        }
        @SuppressWarnings("unchecked")
        StructMetaData meta = new StructMetaData(TType.STRUCT, argsTBaseClass);
        msgStruct = new BaseArray(meta, (ArrayJson) jsonArray.get(3));
    }
    return msg;
}