Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement predefined field constraints #178

Merged
merged 8 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

import com.google.protobuf.DescriptorProtos;
import com.google.protobuf.Descriptors;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.TypeRegistry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -70,4 +73,50 @@ static Map<String, Descriptors.FileDescriptor> parseFileDescriptors(
}
return fileDescriptorMap;
}

static TypeRegistry createTypeRegistry(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this function and the one below it want to be recursive, in case there are ever messages or extensions nested inside other messages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one below it probably does, but createTypeRegistry doesn't: TypeRegistry.add does its own recursion.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I have addressed this for the latter function (as mentioned above it is not needed for TypeRegistry.)

Iterable<? extends Descriptors.FileDescriptor> fileDescriptors) {
TypeRegistry.Builder registryBuilder = TypeRegistry.newBuilder();
for (Descriptors.FileDescriptor fileDescriptor : fileDescriptors) {
registryBuilder.add(fileDescriptor.getMessageTypes());
}
return registryBuilder.build();
}

static ExtensionRegistry createExtensionRegistry(
Iterable<? extends Descriptors.FileDescriptor> fileDescriptors) {
ExtensionRegistry registry = ExtensionRegistry.newInstance();
for (Descriptors.FileDescriptor fileDescriptor : fileDescriptors) {
registerFileExtensions(registry, fileDescriptor);
}
return registry;
}

private static void registerFileExtensions(
ExtensionRegistry registry, Descriptors.FileDescriptor fileDescriptor) {
registerExtensions(registry, fileDescriptor.getExtensions());
for (Descriptors.Descriptor descriptor : fileDescriptor.getMessageTypes()) {
registerMessageExtensions(registry, descriptor);
}
}

private static void registerMessageExtensions(
ExtensionRegistry registry, Descriptors.Descriptor descriptor) {
registerExtensions(registry, descriptor.getExtensions());
for (Descriptors.Descriptor nestedDescriptor : descriptor.getNestedTypes()) {
registerMessageExtensions(registry, nestedDescriptor);
}
}

private static void registerExtensions(
ExtensionRegistry registry, List<Descriptors.FieldDescriptor> extensions) {
for (Descriptors.FieldDescriptor fieldDescriptor : extensions) {
if (fieldDescriptor.getJavaType() == Descriptors.FieldDescriptor.JavaType.MESSAGE) {
registry.add(
fieldDescriptor, DynamicMessage.getDefaultInstance(fieldDescriptor.getMessageType()));
} else {
registry.add(fieldDescriptor);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.TypeRegistry;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -57,7 +58,17 @@ static TestConformanceResponse testConformance(TestConformanceRequest request) {
try {
Map<String, Descriptors.Descriptor> descriptorMap =
FileDescriptorUtil.parse(request.getFdset());
Validator validator = new Validator(Config.newBuilder().build());
Map<String, Descriptors.FileDescriptor> fileDescriptorMap =
FileDescriptorUtil.parseFileDescriptors(request.getFdset());
TypeRegistry typeRegistry = FileDescriptorUtil.createTypeRegistry(fileDescriptorMap.values());
ExtensionRegistry extensionRegistry =
FileDescriptorUtil.createExtensionRegistry(fileDescriptorMap.values());
Validator validator =
new Validator(
Config.newBuilder()
.setTypeRegistry(typeRegistry)
.setExtensionRegistry(extensionRegistry)
.build());
TestConformanceResponse.Builder responseBuilder = TestConformanceResponse.newBuilder();
Map<String, TestResult> resultsMap = new HashMap<>();
for (Map.Entry<String, Any> entry : request.getCasesMap().entrySet()) {
Expand Down
Loading