use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project incubator-dubbo-ops by apache.
the class ServiceTestV3Util method generateEnclosedType.
private static void generateEnclosedType(Map<String, Object> holder, String key, ServiceDefinition sd, String type) {
if (isPrimitiveType(type)) {
holder.put(key, generateType(sd, type));
} else {
TypeDefinition td = findTypeDefinition(sd, type);
if (td.getProperties() == null || td.getProperties().size() == 0) {
holder.put(key, generateType(sd, td));
} else {
Map<String, Object> enclosedMap = new HashMap<>();
holder.put(key, enclosedMap);
generateComplexType(sd, td, enclosedMap);
}
}
}
use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.
the class ProtobufTypeBuilderTest method testProtobufBuilder.
@Test
public void testProtobufBuilder() {
// TEST Pb Service metaData builder
FullServiceDefinition serviceDefinition = ServiceDefinitionBuilder.buildFullDefinition(ServiceInterface.class);
MethodDefinition methodDefinition = serviceDefinition.getMethods().get(0);
String parameterName = methodDefinition.getParameterTypes()[0];
TypeDefinition typeDefinition = null;
for (TypeDefinition type : serviceDefinition.getTypes()) {
if (parameterName.equals(type.getType())) {
typeDefinition = type;
break;
}
}
Map<String, TypeDefinition> propertiesMap = typeDefinition.getProperties();
assertThat(propertiesMap.size(), is(11));
assertThat(propertiesMap.containsKey("money"), is(true));
assertThat(propertiesMap.get("money").getType(), equalTo("double"));
assertThat(propertiesMap.containsKey("cash"), is(true));
assertThat(propertiesMap.get("cash").getType(), equalTo("float"));
assertThat(propertiesMap.containsKey("age"), is(true));
assertThat(propertiesMap.get("age").getType(), equalTo("int"));
assertThat(propertiesMap.containsKey("num"), is(true));
assertThat(propertiesMap.get("num").getType(), equalTo("long"));
assertThat(propertiesMap.containsKey("sex"), is(true));
assertThat(propertiesMap.get("sex").getType(), equalTo("boolean"));
assertThat(propertiesMap.containsKey("name"), is(true));
assertThat(propertiesMap.get("name").getType(), equalTo("java.lang.String"));
assertThat(propertiesMap.containsKey("msg"), is(true));
assertThat(propertiesMap.get("msg").getType(), equalTo("com.google.protobuf.ByteString"));
assertThat(propertiesMap.containsKey("phone"), is(true));
assertThat(propertiesMap.get("phone").getType(), equalTo("java.util.List<org.apache.dubbo.metadata.definition.protobuf.model.GooglePB$PhoneNumber>"));
assertThat(propertiesMap.containsKey("doubleMap"), is(true));
assertThat(propertiesMap.get("doubleMap").getType(), equalTo("java.util.Map<java.lang.String,org.apache.dubbo.metadata.definition.protobuf.model.GooglePB$PhoneNumber>"));
assertThat(propertiesMap.get("bytesList").getType(), equalTo("java.util.List<com.google.protobuf.ByteString>"));
assertThat(propertiesMap.get("bytesMap").getType(), equalTo("java.util.Map<java.lang.String,com.google.protobuf.ByteString>"));
}
use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.
the class GeneralTypeDefinitionBuilder method buildProperties.
protected void buildProperties(ProcessingEnvironment processingEnv, TypeElement type, TypeDefinition definition) {
getNonStaticFields(type).forEach(field -> {
String fieldName = field.getSimpleName().toString();
TypeDefinition propertyType = TypeDefinitionBuilder.build(processingEnv, field);
if (propertyType != null) {
definition.getProperties().put(fieldName, propertyType);
}
});
}
use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.
the class TypeDefinitionBuilder method build.
/**
* Build the instance of {@link TypeDefinition} from the specified {@link TypeMirror type}
*
* @param processingEnv {@link ProcessingEnvironment}
* @param type {@link TypeMirror type}
* @return non-null
*/
static TypeDefinition build(ProcessingEnvironment processingEnv, TypeMirror type) {
String typeName = type.toString();
TypeDefinition typeDefinition = new TypeDefinition(typeName);
// Build by all instances of TypeDefinitionBuilder that were loaded By Java SPI
getExtensionLoader(TypeDefinitionBuilder.class).getSupportedExtensionInstances().stream().filter(builder -> builder.accept(processingEnv, type)).findFirst().ifPresent(builder -> {
builder.build(processingEnv, type, typeDefinition);
// typeDefinition.setTypeBuilderName(builder.getClass().getName());
});
return typeDefinition;
}
use of org.apache.dubbo.metadata.definition.model.TypeDefinition in project dubbo by alibaba.
the class MetadataTest method testRawMap.
/**
*/
@Test
public void testRawMap() {
TypeDefinitionBuilder builder = new TypeDefinitionBuilder();
TypeDefinition td = builder.build(ResultWithRawCollections.class, ResultWithRawCollections.class);
System.out.println(">> testRawMap: " + new Gson().toJson(td));
Assertions.assertEquals("org.apache.dubbo.metadata.definition.common.ResultWithRawCollections", td.getType());
Assertions.assertEquals(2, td.getProperties().size());
Assertions.assertEquals("java.util.Map", td.getProperties().get("map").getType());
Assertions.assertEquals(MapTypeBuilder.class.getName(), td.getProperties().get("map").getTypeBuilderName());
Assertions.assertEquals("java.util.List", td.getProperties().get("list").getType());
Assertions.assertEquals(CollectionTypeBuilder.class.getName(), td.getProperties().get("list").getTypeBuilderName());
ServiceDefinition sd = MetadataUtils.generateMetadata(TestService.class);
System.out.println(">> testRawMap: " + new Gson().toJson(sd));
Assertions.assertEquals(TestService.class.getName(), sd.getCanonicalName());
Assertions.assertEquals(TestService.class.getMethods().length, sd.getMethods().size());
boolean containsType = false;
for (TypeDefinition type : sd.getTypes()) {
if (type.getType().equals("org.apache.dubbo.metadata.definition.common.ResultWithRawCollections")) {
containsType = true;
break;
}
}
Assertions.assertTrue(containsType);
}
Aggregations