Skip to content

Commit

Permalink
[DUBBO-4010] TypeDefinitonBuilder support google protobuf entity buil…
Browse files Browse the repository at this point in the history
…d # (apache#4011)

* add protobuf typeDefinitionBuilder

* fix pull request comment
add test case to test NestPbType
  • Loading branch information
vio-lin authored and beiwei30 committed May 22, 2019
1 parent 52cbfc9 commit a7f56a3
Show file tree
Hide file tree
Showing 11 changed files with 426 additions and 24 deletions.
1 change: 0 additions & 1 deletion dubbo-metadata-report/dubbo-metadata-definition/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-metadata-definition</artifactId>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@
*/
package org.apache.dubbo.metadata.definition;

import org.apache.dubbo.metadata.definition.builder.ArrayTypeBuilder;
import org.apache.dubbo.metadata.definition.builder.CollectionTypeBuilder;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.metadata.definition.builder.DefaultTypeBuilder;
import org.apache.dubbo.metadata.definition.builder.EnumTypeBuilder;
import org.apache.dubbo.metadata.definition.builder.MapTypeBuilder;
import org.apache.dubbo.metadata.definition.builder.TypeBuilder;
import org.apache.dubbo.metadata.definition.model.TypeDefinition;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -35,13 +31,16 @@
* 2015/1/27.
*/
public class TypeDefinitionBuilder {
private static final List<TypeBuilder> BUILDERS;

private static final TypeBuilder ARRAY_BUILDER = new ArrayTypeBuilder();
private static final TypeBuilder COLLECTION_BUILDER = new CollectionTypeBuilder();
private static final TypeBuilder MAP_BUILDER = new MapTypeBuilder();
private static final TypeBuilder ENUM_BUILDER = new EnumTypeBuilder();

private static final List<TypeBuilder> BUILDERS = Arrays.asList(ARRAY_BUILDER, COLLECTION_BUILDER, MAP_BUILDER, ENUM_BUILDER);
static{
List<TypeBuilder> builders = new ArrayList<>();
ExtensionLoader<TypeBuilder> extensionLoader = ExtensionLoader.getExtensionLoader(TypeBuilder.class);
for(String extensionName : extensionLoader.getSupportedExtensions()){
builders.add(extensionLoader.getExtension(extensionName));
}
BUILDERS = builders;
}

public static TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinition> typeCache) {
TypeBuilder builder = getGenericTypeBuilder(type, clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinit
Type[] actualTypeArgs = parameterizedType.getActualTypeArguments();
if (actualTypeArgs == null || actualTypeArgs.length != 1) {
throw new IllegalArgumentException(MessageFormat.format(
"[ServiceDefinitionBuilder] Collection type [{0}] with unexpected amount of arguments [{1}]." + Arrays.toString(actualTypeArgs),
"[ServiceDefinitionBuilder] Collection type [{0}] with unexpected amount of arguments [{1}]."
+ Arrays.toString(actualTypeArgs),
type, actualTypeArgs));
}

Expand All @@ -60,11 +61,7 @@ public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinit
TypeDefinitionBuilder.build(actualType, rawType, typeCache);
} else if (actualType instanceof Class<?>) {
Class<?> actualClass = (Class<?>) actualType;
if (actualClass.isArray() || actualClass.isEnum()) {
TypeDefinitionBuilder.build(null, actualClass, typeCache);
} else {
DefaultTypeBuilder.build(actualClass, typeCache);
}
TypeDefinitionBuilder.build(null, actualClass, typeCache);
}

return new TypeDefinition(type.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinit
Type[] actualTypeArgs = parameterizedType.getActualTypeArguments();
if (actualTypeArgs == null || actualTypeArgs.length != 2) {
throw new IllegalArgumentException(MessageFormat.format(
"[ServiceDefinitionBuilder] Map type [{0}] with unexpected amount of arguments [{1}]." + Arrays.toString(actualTypeArgs), type, actualTypeArgs));
"[ServiceDefinitionBuilder] Map type [{0}] with unexpected amount of arguments [{1}]."
+ Arrays.toString(actualTypeArgs), type, actualTypeArgs));
}

for (Type actualType : actualTypeArgs) {
Expand All @@ -58,11 +59,7 @@ public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinit
TypeDefinitionBuilder.build(actualType, rawType, typeCache);
} else if (actualType instanceof Class<?>) {
Class<?> actualClass = (Class<?>) actualType;
if (actualClass.isArray() || actualClass.isEnum()) {
TypeDefinitionBuilder.build(null, actualClass, typeCache);
} else {
DefaultTypeBuilder.build(actualClass, typeCache);
}
TypeDefinitionBuilder.build(null, actualClass, typeCache);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.metadata.definition.builder;

import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.metadata.definition.model.TypeDefinition;

import java.lang.reflect.Type;
Expand All @@ -24,6 +25,7 @@
/**
* 2015/1/27.
*/
@SPI
public interface TypeBuilder {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
array=org.apache.dubbo.metadata.definition.builder.ArrayTypeBuilder
collection=org.apache.dubbo.metadata.definition.builder.CollectionTypeBuilder
map=org.apache.dubbo.metadata.definition.builder.MapTypeBuilder
enum=org.apache.dubbo.metadata.definition.builder.EnumTypeBuilder
5 changes: 5 additions & 0 deletions dubbo-serialization/dubbo-serialization-protobuf-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ limitations under the License.
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-definition</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>
Loading

0 comments on commit a7f56a3

Please sign in to comment.