use of com.google.protobuf.DescriptorProtos.MessageOptions in project gapic-generator-java by googleapis.
the class Parser method parseField.
private static Field parseField(FieldDescriptor fieldDescriptor, Descriptor messageDescriptor, boolean hasFieldNameConflict, Set<ResourceReference> outputResourceReferencesSeen) {
FieldOptions fieldOptions = fieldDescriptor.getOptions();
MessageOptions messageOptions = messageDescriptor.getOptions();
ResourceReference resourceReference = null;
if (fieldOptions.hasExtension(ResourceProto.resourceReference)) {
com.google.api.ResourceReference protoResourceReference = fieldOptions.getExtension(ResourceProto.resourceReference);
// Assumes only one of type or child_type is set.
String typeString = protoResourceReference.getType();
String childTypeString = protoResourceReference.getChildType();
Preconditions.checkState(!Strings.isNullOrEmpty(typeString) ^ !Strings.isNullOrEmpty(childTypeString), String.format("Exactly one of type or child_type must be set for resource_reference in field %s", fieldDescriptor.getName()));
boolean isChildType = !Strings.isNullOrEmpty(childTypeString);
resourceReference = isChildType ? ResourceReference.withChildType(childTypeString) : ResourceReference.withType(typeString);
outputResourceReferencesSeen.add(resourceReference);
} else if (messageOptions.hasExtension(ResourceProto.resource)) {
ResourceDescriptor protoResource = messageOptions.getExtension(ResourceProto.resource);
// aip.dev/4231.
String resourceFieldNameValue = ResourceNameConstants.NAME_FIELD_NAME;
if (!Strings.isNullOrEmpty(protoResource.getNameField())) {
resourceFieldNameValue = protoResource.getNameField();
}
if (fieldDescriptor.getName().equals(resourceFieldNameValue)) {
resourceReference = ResourceReference.withType(protoResource.getType());
}
}
Field.Builder fieldBuilder = Field.builder();
if (fieldDescriptor.getFile().toProto().hasSourceCodeInfo()) {
SourceCodeInfoLocation protoFieldLocation = SOURCE_CODE_INFO_PARSER.getLocation(fieldDescriptor);
if (!Objects.isNull(protoFieldLocation) && !Strings.isNullOrEmpty(protoFieldLocation.getLeadingComments())) {
fieldBuilder.setDescription(protoFieldLocation.getLeadingComments());
}
}
// Mirror protoc's name conflict resolution behavior for fields.
// For more context, trace hasFieldNameConflict back to where it gets passed in above.
String actualFieldName = hasFieldNameConflict ? fieldDescriptor.getName() + fieldDescriptor.getNumber() : fieldDescriptor.getName();
return fieldBuilder.setName(actualFieldName).setOriginalName(fieldDescriptor.getName()).setType(TypeParser.parseType(fieldDescriptor)).setIsMessage(fieldDescriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE).setIsEnum(fieldDescriptor.getJavaType() == FieldDescriptor.JavaType.ENUM).setIsContainedInOneof(fieldDescriptor.getContainingOneof() != null && !fieldDescriptor.getContainingOneof().isSynthetic()).setIsProto3Optional(fieldDescriptor.getContainingOneof() != null && fieldDescriptor.getContainingOneof().isSynthetic()).setIsRepeated(fieldDescriptor.isRepeated()).setIsMap(fieldDescriptor.isMapField()).setResourceReference(resourceReference).build();
}
use of com.google.protobuf.DescriptorProtos.MessageOptions in project gapic-generator-java by googleapis.
the class ResourceNameParser method parseResourceNameFromMessageType.
@VisibleForTesting
static Optional<ResourceName> parseResourceNameFromMessageType(Descriptor messageTypeDescriptor, String pakkage) {
MessageOptions messageOptions = messageTypeDescriptor.getOptions();
if (!messageOptions.hasExtension(ResourceProto.resource)) {
return Optional.empty();
}
ResourceDescriptor protoResource = messageOptions.getExtension(ResourceProto.resource);
// Validation - check that a resource name field is present.
if (Strings.isNullOrEmpty(protoResource.getNameField())) {
// aip.dev/4231
boolean resourceNameFieldFound = messageTypeDescriptor.findFieldByName(ResourceNameConstants.NAME_FIELD_NAME) != null;
// Example: AccountBudgetProposal.
for (FieldDescriptor fieldDescriptor : messageTypeDescriptor.getFields()) {
FieldOptions fieldOptions = fieldDescriptor.getOptions();
if (fieldOptions.hasExtension(ResourceProto.resourceReference)) {
resourceNameFieldFound = true;
break;
}
}
Preconditions.checkState(resourceNameFieldFound, String.format("Message %s has a resource annotation but no field titled \"name\" or containing a" + " resource reference", messageTypeDescriptor.getName()));
}
TypeNode javaMessageType = TypeParser.parseType(messageTypeDescriptor);
return createResourceName(protoResource, pakkage, javaMessageType.reference().fullName());
}
Aggregations