下面列出了怎么用com.google.protobuf.DescriptorProtos的API类实例代码及写法,或者点击链接到github查看源代码。
static Descriptors.FileDescriptor try_get_inner_deile_desc(String name) {
if (inner_file_descs != null) {
return inner_file_descs.getOrDefault(name.replace('\\', '/').toLowerCase(), null);
}
Descriptors.FileDescriptor[] inner_descs = new Descriptors.FileDescriptor[] { Xresloader.getDescriptor(),
XresloaderUe.getDescriptor(), PbHeaderV3.getDescriptor(), DescriptorProtos.getDescriptor(),
DurationProto.getDescriptor(), TimestampProto.getDescriptor(), AnyProto.getDescriptor(),
ApiProto.getDescriptor(), EmptyProto.getDescriptor(), FieldMaskProto.getDescriptor(),
StructProto.getDescriptor(), TypeProto.getDescriptor(), WrappersProto.getDescriptor(),
SourceContextProto.getDescriptor(), };
inner_file_descs = new HashMap<String, Descriptors.FileDescriptor>();
for (Descriptors.FileDescriptor innerFileDesc : inner_descs) {
inner_file_descs.put(innerFileDesc.getName().replace('\\', '/').toLowerCase(), innerFileDesc);
}
return inner_file_descs.getOrDefault(name.toLowerCase(), null);
}
private static void renameRecordTypeUsages(@Nonnull String namespace,
@Nonnull DescriptorProtos.DescriptorProto.Builder messageTypeBuilder,
@Nonnull String fullOldRecordTypeName, @Nonnull String fullNewRecordTypeName) {
// Rename any fields within the record type to the new type name
for (DescriptorProtos.FieldDescriptorProto.Builder field : messageTypeBuilder.getFieldBuilderList()) {
final FieldTypeMatch fieldTypeMatch = fieldIsType(namespace, messageTypeBuilder, field, fullOldRecordTypeName);
if (fieldTypeMatch.isAmbiguousMatch()) {
throw new AmbiguousTypeNameException(namespace, messageTypeBuilder, field, fullOldRecordTypeName);
} else if (FieldTypeMatch.MATCHES.equals(fieldTypeMatch)) {
field.setTypeName(fullNewRecordTypeName);
} else if (FieldTypeMatch.MATCHES_AS_NESTED.equals(fieldTypeMatch)) {
final String messageNamespace = (namespace.isEmpty()) ? messageTypeBuilder.getName() : (namespace + "." + messageTypeBuilder.getName());
final String fieldTypeName = fullyQualifiedTypeName(messageNamespace, field.getTypeName());
final String newFieldTypeName = fullNewRecordTypeName + fieldTypeName.substring(fullOldRecordTypeName.length());
field.setTypeName(newFieldTypeName);
}
}
// Rename the record type if used within any nested message types
if (messageTypeBuilder.getNestedTypeCount() > 0) {
final String nestedNamespace = namespace.isEmpty() ? messageTypeBuilder.getName() : (namespace + "." + messageTypeBuilder.getName());
for (DescriptorProtos.DescriptorProto.Builder nestedTypeBuilder : messageTypeBuilder.getNestedTypeBuilderList()) {
renameRecordTypeUsages(nestedNamespace, nestedTypeBuilder, fullOldRecordTypeName, fullNewRecordTypeName);
}
}
}
public ProtoLocation(
DescriptorProtos.SourceCodeInfo.Location location, final ProtoElement element) {
// Spit out "?:?" for line:column if there's no "span" set in the location. This can happen
// when (for example) a proto transform tool synthesizes a field that doesn't appear in the
// source *.proto files.
if (location.getSpanCount() > 0) {
this.displayString =
String.format(
"%s:%d:%d",
element.getFile().getLocation().getDisplayString(),
location.getSpan(0) + 1,
location.getSpan(1) + 1);
} else {
this.displayString =
String.format("%s:?:?", element.getFile().getLocation().getDisplayString());
}
this.element = element;
}
private List<ServiceContext> findServices(List<DescriptorProtos.FileDescriptorProto> protos, ProtoTypeMap typeMap) {
List<ServiceContext> contexts = new ArrayList<>();
protos.forEach(fileProto -> {
for (int serviceNumber = 0; serviceNumber < fileProto.getServiceCount(); serviceNumber++) {
ServiceContext serviceContext = buildServiceContext(
fileProto.getService(serviceNumber),
typeMap,
fileProto.getSourceCodeInfo().getLocationList(),
serviceNumber);
serviceContext.protoName = fileProto.getName();
serviceContext.packageName = extractPackageName(fileProto);
contexts.add(serviceContext);
}
});
return contexts;
}
@Test
public void fieldIsType() {
final DescriptorProtos.FileDescriptorProto file = TestRecords1Proto.getDescriptor().toProto();
assertEquals(FieldTypeMatch.MATCHES,
fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MySimpleRecord"));
assertEquals(FieldTypeMatch.MATCHES,
fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.MySimpleRecord"));
assertEquals(FieldTypeMatch.MATCHES_AS_NESTED,
fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record"));
assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MySimpleRecord.MyNestedRecord"));
assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test1.MySimpleRecord.MyNestedRecord"));
assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", ".com.apple.foundationdb.record.test2.MySimpleRecord"));
assertEquals(FieldTypeMatch.DOES_NOT_MATCH,
fieldIsType(file, RecordMetaDataBuilder.DEFAULT_UNION_NAME, "_MySimpleRecord", "MyOtherRecord"));
}
public Builder replacePackageBinary(String packageName, Collection<ByteString> updateBytes)
throws InvalidProtocolBufferException {
Map<String, DescriptorProtos.FileDescriptorProto> updated = toMap(updateBytes);
List<String> removing = new ArrayList<>();
// clear the package
fileDescriptorMap.forEach(
(k, v) -> {
if (v.getPackage().equals(packageName)) {
removing.add(k);
}
});
removing.forEach(f -> fileDescriptorMap.remove(f));
// only add with package prefix
updated.forEach(
(fileName, fdp) -> {
if (fdp.getPackage().equals(packageName)) {
fileDescriptorMap.put(fileName, fdp);
}
});
return this;
}
@Test
public void fieldLabelChanged() {
FileDescriptor oldFile = TestRecords1Proto.getDescriptor();
List<DescriptorProtos.FieldDescriptorProto.Label> labels = Arrays.asList(
DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL,
DescriptorProtos.FieldDescriptorProto.Label.LABEL_REPEATED,
DescriptorProtos.FieldDescriptorProto.Label.LABEL_REQUIRED
);
for (int i = 0; i < labels.size(); i++) {
final int itr = i;
final DescriptorProtos.FieldDescriptorProto.Label label = labels.get(itr);
final String labelText = label.name().substring(label.name().indexOf('_') + 1).toLowerCase();
final String errMsg = String.format("%s field is no longer %s", labelText, labelText);
FileDescriptor updatedFile = mutateField("MySimpleRecord", "str_value_indexed", oldFile,
field -> field.setLabel(labels.get((itr + 1) % labels.size())));
assertInvalid(errMsg, oldFile, updatedFile);
oldFile = updatedFile;
}
}
public Builder replacePackagePrefixBinary(
String packagePrefix, Collection<ByteString> updateBytes)
throws InvalidProtocolBufferException {
Map<String, DescriptorProtos.FileDescriptorProto> updated = toMap(updateBytes);
List<String> removing = new ArrayList<>();
// clear the package prefix
fileDescriptorMap.forEach(
(fileName, fdp) -> {
if (isInPackagePrefix(fdp, packagePrefix)) {
removing.add(fileName);
}
});
removing.forEach(f -> fileDescriptorMap.remove(f));
// only add in package
updated.forEach(
(fileName, fdp) -> {
if (isInPackagePrefix(fdp, packagePrefix)) {
fileDescriptorMap.put(fileName, fdp);
}
});
return this;
}
/**
* Creates a default union descriptor for the given file descriptor if missing.
*
* <p>
* If the given file descriptor is missing a union message, this method will add one before updating the meta-data.
* The generated union descriptor is constructed by adding any non-{@code NESTED} types in the file descriptor to the
* union descriptor from the currently stored meta-data. A new field is not added if a field of the given type already
* exists, and the order of any existing fields is preserved. Note that types are identified by name, so renaming
* top-level message types may result in validation errors when trying to update the record descriptor.
* </p>
*
* @param fileDescriptor the file descriptor to create a union for
* @param baseUnionDescriptor the base union descriptor
* @return the builder for the union
*/
@Nonnull
public static Descriptors.FileDescriptor addDefaultUnionIfMissing(@Nonnull Descriptors.FileDescriptor fileDescriptor, @Nonnull Descriptors.Descriptor baseUnionDescriptor) {
if (MetaDataProtoEditor.hasUnion(fileDescriptor)) {
return fileDescriptor;
}
DescriptorProtos.FileDescriptorProto fileDescriptorProto = fileDescriptor.toProto();
DescriptorProtos.FileDescriptorProto.Builder fileBuilder = fileDescriptorProto.toBuilder();
DescriptorProtos.DescriptorProto.Builder unionDescriptorBuilder = createSyntheticUnion(fileDescriptor, baseUnionDescriptor);
for (DescriptorProtos.DescriptorProto.Builder messageType : fileBuilder.getMessageTypeBuilderList()) {
RecordMetaDataOptionsProto.RecordTypeOptions.Usage messageTypeUsage = getMessageTypeUsage(messageType);
if (messageTypeUsage != RecordMetaDataOptionsProto.RecordTypeOptions.Usage.NESTED
&& !hasField(fileBuilder, unionDescriptorBuilder, messageType)) {
addFieldToUnion(unionDescriptorBuilder, fileBuilder, messageType);
}
}
fileBuilder.addMessageType(unionDescriptorBuilder);
try {
return Descriptors.FileDescriptor.buildFrom(fileBuilder.build(), fileDescriptor.getDependencies().toArray(new Descriptors.FileDescriptor[0]));
} catch (Descriptors.DescriptorValidationException e) {
throw new MetaDataException("Failed to add a default union", e);
}
}
@Test
public void noPackageSetTest() throws Exception {
DescriptorProtos.FileDescriptorProto.Builder fileDescriptorProtoBuilder =
DescriptorProtos.FileDescriptorProto.newBuilder().setName("test").setSyntax("proto3");
DescriptorProtos.DescriptorProto.Builder descriptor =
DescriptorProtos.DescriptorProto.newBuilder();
descriptor.setName("TestMessage");
fileDescriptorProtoBuilder.addMessageType(descriptor);
testOutput(
fileDescriptorProtoBuilder.build(),
null,
"syntax = \"proto3\";\n"
+ "\n"
+ "import \"test/v1/option.proto\";\n"
+ "\n"
+ "\n"
+ "\n"
+ "message TestMessage {\n"
+ "\n"
+ "}\n");
}
@Test
public void removeMethod() throws Exception {
ProtoDomain dRef = ProtoDomain.builder().add(FILE_V1).build();
DescriptorProtos.FileDescriptorProto fd =
FILE_V1
.toBuilder()
.setService(0, FILE_V1.getService(0).toBuilder().removeMethod(0))
.build();
ProtoDomain dNew = ProtoDomain.builder().add(fd).build();
Report report = diff(dRef, dNew);
ServiceResult result = report.getServiceResultsMap().get("package.v1.Service1");
Assert.assertEquals(ChangeType.UNCHANGED, result.getChange().getChangeType());
Assert.assertEquals(ChangeType.REMOVAL, result.getMethodResults(0).getChange().getChangeType());
Assert.assertEquals("Method1", result.getMethodResults(0).getName());
Assert.assertEquals("Method1", result.getMethodResults(0).getChange().getFromName());
Assert.assertEquals("", result.getMethodResults(0).getChange().getToName());
}
public DataVerifyPbEnum(DescriptorProtos.EnumDescriptorProto desc) {
super(desc.getName());
for (DescriptorProtos.EnumValueDescriptorProto val_desc : desc.getValueList()) {
all_names.put(val_desc.getName(), (long) val_desc.getNumber());
all_numbers.add((long) val_desc.getNumber());
// alias extension
if (val_desc.getOptions().hasExtension(Xresloader.enumAlias)) {
String alias_name = val_desc.getOptions().getExtension(Xresloader.enumAlias);
if (!alias_name.isEmpty()) {
all_names.put(alias_name, (long) val_desc.getNumber());
}
}
}
}
@Test
public void writeMessage() throws Exception {
DescriptorProtos.FileDescriptorProto.Builder fileDescriptorProtoBuilder =
DescriptorProtos.FileDescriptorProto.newBuilder().setName("test").setSyntax("proto3");
DescriptorProtos.DescriptorProto.Builder descriptor =
DescriptorProtos.DescriptorProto.newBuilder()
.setName("Proto3Message")
.setOptions(TestProto.MESSAGE_OPTIONS)
.addField(
DescriptorProtos.FieldDescriptorProto.newBuilder()
.setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_BOOL)
.setNumber(1)
.setName("field_1")
.setOptions(TestProto.FIELD_OPTIONS)
.build());
fileDescriptorProtoBuilder.addMessageType(descriptor);
assertMessage(fileDescriptorProtoBuilder.build(), null);
}
private void diffMessageType(
Descriptors.Descriptor descriptorRef, Descriptors.Descriptor descriptorNew) {
DescriptorProtos.MessageOptions optionsRef = descriptorRef.getOptions();
DescriptorProtos.MessageOptions optionsNew = descriptorNew.getOptions();
diffExtensionOptions(
OptionChangeInfo.OptionType.MESSAGE_OPTION,
descriptorRef,
optionsRef.getAllFields(),
descriptorNew,
optionsNew.getAllFields());
diffUnknownOptions(
OptionChangeInfo.OptionType.MESSAGE_OPTION,
descriptorRef,
optionsRef.getUnknownFields(),
descriptorNew,
optionsNew.getUnknownFields());
diffFields(descriptorRef, descriptorNew);
}
@Test
public void shouldTestShouldCreateNestedMapping() throws IOException {
ProtoField protoField = new ProtoField(new ArrayList<ProtoField>() {{
add(new ProtoField("order_number", 1));
add(new ProtoField("order_url", "some.type.name", DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE, 2, new ArrayList<ProtoField>() {{
add(new ProtoField("host", 1));
add(new ProtoField("url", 2));
}}));
add(new ProtoField("order_details", 3));
}});
ObjectNode objNode = JsonNodeFactory.instance.objectNode();
ObjectNode innerObjNode = JsonNodeFactory.instance.objectNode();
innerObjNode.put("1", "host");
innerObjNode.put("2", "url");
innerObjNode.put("record_name", "order_url");
objNode.put("1", "order_number");
objNode.put("2", innerObjNode);
objNode.put("3", "order_details");
String columnMapping = converter.generateColumnMappings(protoField.getFields());
String expectedProtoMapping = objectMapper.writeValueAsString(objNode);
assertEquals(expectedProtoMapping, columnMapping);
}
private void diffOptionsFromFile(
Descriptors.FileDescriptor descriptorRef, Descriptors.FileDescriptor descriptorNew) {
DescriptorProtos.FileOptions optionsRef = descriptorRef.getOptions();
DescriptorProtos.FileOptions optionsNew = descriptorNew.getOptions();
diffExtensionOptions(
OptionChangeInfo.OptionType.FILE_OPTION,
descriptorRef,
optionsRef.getAllFields(),
descriptorNew,
optionsNew.getAllFields());
diffUnknownOptions(
OptionChangeInfo.OptionType.FILE_OPTION,
descriptorRef,
optionsRef.getUnknownFields(),
descriptorNew,
optionsNew.getUnknownFields());
}
private void diffOptionsFromField(
Descriptors.FieldDescriptor descriptorRef, Descriptors.FieldDescriptor descriptorNew) {
DescriptorProtos.FieldOptions optionsRef = descriptorRef.getOptions();
DescriptorProtos.FieldOptions optionsNew = descriptorNew.getOptions();
diffExtensionOptions(
OptionChangeInfo.OptionType.FIELD_OPTION,
descriptorRef,
optionsRef.getAllFields(),
descriptorNew,
optionsNew.getAllFields());
diffUnknownOptions(
OptionChangeInfo.OptionType.FIELD_OPTION,
descriptorRef,
optionsRef.getUnknownFields(),
descriptorNew,
optionsNew.getUnknownFields());
}
private void diffOptionsFromMethod(
Descriptors.MethodDescriptor descriptorRef, Descriptors.MethodDescriptor descriptorNew) {
DescriptorProtos.MethodOptions optionsRef = descriptorRef.getOptions();
DescriptorProtos.MethodOptions optionsNew = descriptorNew.getOptions();
diffExtensionOptions(
OptionChangeInfo.OptionType.METHOD_OPTION,
descriptorRef,
optionsRef.getAllFields(),
descriptorNew,
optionsNew.getAllFields());
diffUnknownOptions(
OptionChangeInfo.OptionType.METHOD_OPTION,
descriptorRef,
optionsRef.getUnknownFields(),
descriptorNew,
optionsNew.getUnknownFields());
}
private void applyFieldResults(
MessageResult messageResult,
Descriptors.Descriptor messageDescriptor,
DescriptorProtos.DescriptorProto.Builder newDescriptorProtoBuilder) {
HashMap<Integer, Integer> fieldNumberToIndexMap = getFieldNumberToIndexMap(messageDescriptor);
for (FieldResult fieldResult : messageResult.getFieldResultsList()) {
if (fieldResult.getOptionChangeCount() > 0) {
Descriptors.FieldDescriptor fieldDescriptor =
messageDescriptor.findFieldByNumber(fieldResult.getNumber());
DescriptorProtos.FieldDescriptorProto newFieldDescriptorProto =
applyFieldOptionChanges(fieldDescriptor, fieldResult.getOptionChangeList());
newDescriptorProtoBuilder.setField(
fieldNumberToIndexMap.get(fieldResult.getNumber()), newFieldDescriptorProto);
}
}
}
@Test
public void addEnum() throws Exception {
ProtoDomain dRef = ProtoDomain.builder().add(FILE_V1).build();
DescriptorProtos.FileDescriptorProto fd =
FILE_V1
.toBuilder()
.addEnumType(
DescriptorProtos.EnumDescriptorProto.newBuilder()
.setName("Enum2")
.addValue(
DescriptorProtos.EnumValueDescriptorProto.newBuilder()
.setName("ENUM_VALUE2_UNSET")
.setNumber(0)
.build())
.build())
.build();
ProtoDomain dNew = ProtoDomain.builder().add(fd).build();
Report report = diff(dRef, dNew);
EnumResult result = report.getEnumResultsMap().get("package.v1.Enum2");
Assert.assertEquals("package.v1.Enum2", result.getChange().getToName());
Assert.assertEquals(ChangeType.ADDITION, result.getChange().getChangeType());
}
@Test
public void renameEnumValue() throws Exception {
ProtoDomain dRef = ProtoDomain.builder().add(FILE_V1).build();
DescriptorProtos.FileDescriptorProto fd =
FILE_V1
.toBuilder()
.setEnumType(
0,
FILE_V1
.getEnumType(0)
.toBuilder()
.setValue(1, FILE_V1.getEnumType(0).getValue(1).toBuilder().setName("FOO")))
.build();
ProtoDomain dNew = ProtoDomain.builder().add(fd).build();
Report report = diff(dRef, dNew);
EnumResult result = report.getEnumResultsMap().get("package.v1.Enum1");
Assert.assertEquals(ChangeType.UNCHANGED, result.getChange().getChangeType());
Assert.assertEquals(ChangeType.CHANGED, result.getValueResults(0).getChange().getChangeType());
Assert.assertEquals(1, result.getValueResults(0).getNumber());
Assert.assertEquals("FOO", result.getValueResults(0).getName());
Assert.assertEquals("ENUM_VALUE1_VALUE1", result.getValueResults(0).getChange().getFromName());
Assert.assertEquals("FOO", result.getValueResults(0).getChange().getToName());
}
/**
* Recursively constructs file descriptors for all dependencies of the supplied proto and
* returns a FileDescriptor for the supplied proto itself.
* For maximal efficientcy, reuse the descriptorCache argument across calls.
*/
private static Descriptors.FileDescriptor descriptorFromProto(
DescriptorProtos.FileDescriptorProto descriptorProto,
ImmutableMap<String, DescriptorProtos.FileDescriptorProto> descriptorProtoIndex,
Map<String, Descriptors.FileDescriptor> descriptorCache) throws Descriptors.DescriptorValidationException {
// First, check the cache.
String descriptorName = descriptorProto.getName();
if (descriptorCache.containsKey(descriptorName)) {
return descriptorCache.get(descriptorName);
}
// Then, fetch all the required dependencies recursively.
ImmutableList.Builder<Descriptors.FileDescriptor> dependencies = ImmutableList.builder();
ProtocolStringList protocolStringList = descriptorProto.getDependencyList();
protocolStringList.forEach(dependencyName -> {
if (!descriptorProtoIndex.containsKey(dependencyName)) {
throw new IllegalArgumentException("Can't find dependency: " + dependencyName);
}
DescriptorProtos.FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName);
try {
dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache));
} catch (Descriptors.DescriptorValidationException e) {
logger.warning(e.getMessage());
}
});
// Finally, construct the actual descriptor.
Descriptors.FileDescriptor[] empty = new Descriptors.FileDescriptor[0];
return Descriptors.FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty));
}
@VisibleForTesting
public ProtoField(String name, DescriptorProtos.FieldDescriptorProto.Type type, DescriptorProtos.FieldDescriptorProto.Label label, List<ProtoField> fields) {
this.name = name;
this.type = type;
this.label = label;
this.fields = fields;
}
@Test
public void testSingleLong() throws IOException, StatusRuntimeException {
String node = getJsonNode("testSingleLong");
final DescriptorProtos.FileDescriptorProto fileDescriptorProto =
TestSingleLong.getDescriptor().getFile().toProto();
ProtoDomain protoDomain = ProtoDomain.builder().add(fileDescriptorProto).build();
String avroSchema =
ProtoToAvroSchema.convert(
protoDomain, String.format("%s.TestSingleLong", fileDescriptorProto.getPackage()));
Assert.assertEquals(node, avroSchema);
}
private void crosswire() {
HashMap<String, DescriptorProtos.FileDescriptorProto> map = new HashMap<>();
fileDescriptorSet.getFileList().stream()
.filter(fdp -> !fdp.getName().startsWith("google/protobuf"))
.forEach(fdp -> map.put(fdp.getName(), fdp));
Map<String, Descriptors.FileDescriptor> outMap = new HashMap<>();
ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
map.forEach(
(fileName, proto) -> convertToFileDescriptorMap(fileName, map, outMap, extensionRegistry));
fileDescriptorMap = outMap;
indexOptionsByNumber();
indexDescriptorByName();
}
private static boolean hasField(@Nonnull DescriptorProtos.FileDescriptorProtoOrBuilder file,
@Nonnull DescriptorProtos.DescriptorProtoOrBuilder message,
@Nonnull DescriptorProtos.DescriptorProtoOrBuilder messageType) {
for (DescriptorProtos.FieldDescriptorProto field : message.getFieldList()) {
final String fullTypeName = fullyQualifiedTypeName(file, messageType.getName());
FieldTypeMatch fieldTypeMatch = fieldIsType(file, message, field, fullTypeName);
if (fieldTypeMatch.isAmbiguousMatch()) {
throw new AmbiguousTypeNameException(file.getPackage(), message, field, fullTypeName);
} else if (FieldTypeMatch.MATCHES.equals(fieldTypeMatch)) {
return true;
}
// Nested matches do not count.
}
return false;
}
@Test
public void changeComplexFieldType() throws Exception {
ProtoDomain dRef = ProtoDomain.builder().add(FILE_V1).build();
DescriptorProtos.FileDescriptorProto fd =
FILE_V1
.toBuilder()
.setMessageType(
0,
FILE_V1
.getMessageType(0)
.toBuilder()
.setField(
1,
DescriptorProtos.FieldDescriptorProto.newBuilder()
.setNumber(2)
.setName("seconde_nullable_field")
.setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_STRING)
.build())
.build())
.build();
ProtoDomain dNew = ProtoDomain.builder().add(fd).build();
Report report = diff(dRef, dNew);
MessageResult result = report.getMessageResultsMap().get("package.v1.Message1");
Assert.assertEquals(ChangeType.UNCHANGED, result.getChange().getChangeType());
Assert.assertEquals(ChangeType.CHANGED, result.getFieldResults(0).getChange().getChangeType());
Assert.assertEquals(
"google.protobuf.StringValue", result.getFieldResults(0).getChange().getFromTypeName());
Assert.assertEquals(
FieldChangeInfo.FieldType.FIELD_TYPE_STRING,
result.getFieldResults(0).getChange().getToType());
}
public static List<DescriptorProtos.FileDescriptorSet> registerByIpAndPort(String hostAndPort) {
String[] strings = hostAndPort.split(":");
if (strings.length != 2) {
return emptyList();
}
return registerByIpAndPort(strings[0], Integer.parseInt(strings[1]));
}
@Test
public void recordTypesWithOneOfUnion() {
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
RecordMetaDataBuilder metaData = RecordMetaData.newBuilder().setRecords(TestRecordsOneOfProto.getDescriptor());
final KeyExpression pkey = Key.Expressions.field("rec_no");
metaData.getRecordType("MySimpleRecord").setPrimaryKey(pkey);
metaData.getRecordType("MyOtherRecord").setPrimaryKey(pkey);
metaDataStore.saveRecordMetaData(metaData);
context.commit();
}
// Add a record type to oneOf. It should fail.
try (FDBRecordContext context = fdb.openContext()) {
openMetaDataStore(context);
assertNotNull(metaDataStore.getRecordMetaData().getRecordType("MySimpleRecord"));
DescriptorProtos.DescriptorProto newRecordType = DescriptorProtos.DescriptorProto.newBuilder()
.setName("MyNewRecord")
.addField(DescriptorProtos.FieldDescriptorProto.newBuilder()
.setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL)
.setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_INT32)
.setName("rec_no")
.setNumber(1))
.build();
MetaDataException e = assertThrows(MetaDataException.class, () -> addRecordType(newRecordType, Key.Expressions.field("rec_no")));
assertEquals(e.getMessage(), "Adding record type to oneof is not allowed");
context.commit();
}
}
@Test
public void removeService() throws Exception {
ProtoDomain dRef = ProtoDomain.builder().add(FILE_V1).build();
DescriptorProtos.FileDescriptorProto fd = FILE_V1.toBuilder().clearService().build();
ProtoDomain dNew = ProtoDomain.builder().add(fd).build();
Report report = diff(dRef, dNew);
ServiceResult serviceResult = report.getServiceResultsMap().get("package.v1.Service1");
Assert.assertEquals("package.v1.Service1", serviceResult.getChange().getFromName());
Assert.assertEquals(ChangeType.REMOVAL, serviceResult.getChange().getChangeType());
}