Search in sources :

Example 1 with MessageType

use of com.google.api.tools.framework.model.MessageType in project toolkit by googleapis.

the class GrpcElementDocTransformer method generateMessageDocs.

private List<GrpcElementDocView> generateMessageDocs(ModelTypeTable typeTable, SurfaceNamer namer, ProtoContainerElement containerElement) {
    ImmutableList.Builder<GrpcElementDocView> messageDocs = ImmutableList.builder();
    for (MessageType message : containerElement.getMessages()) {
        // Doesn't have to document map entries because a dictionary is used.
        if (message.isMapEntry()) {
            continue;
        }
        GrpcMessageDocView.Builder doc = GrpcMessageDocView.newBuilder();
        doc.name(namer.getMessageTypeName(typeTable, message));
        doc.fullName(typeTable.getFullNameFor(TypeRef.of(message)));
        doc.fileUrl(GapicParser.getFileUrl(message.getFile()));
        doc.lines(namer.getDocLines(GapicParser.getDocString(message)));
        doc.properties(generateMessagePropertyDocs(typeTable, namer, FieldConfig.toFieldTypeIterableFromField(message.getFields())));
        doc.elementDocs(generateElementDocs(typeTable, namer, message));
        doc.packageName(message.getFile().getFullName());
        messageDocs.add(doc.build());
    }
    return messageDocs.build();
}
Also used : GrpcMessageDocView(com.google.api.codegen.viewmodel.GrpcMessageDocView) ImmutableList(com.google.common.collect.ImmutableList) GrpcElementDocView(com.google.api.codegen.viewmodel.GrpcElementDocView) MessageType(com.google.api.tools.framework.model.MessageType)

Example 2 with MessageType

use of com.google.api.tools.framework.model.MessageType in project toolkit by googleapis.

the class ProtoParser method getResourceDescriptorConfigMap.

/**
 * Return a Map of Unified Resource Types to a ResourceDescriptorConfig object.
 */
public Map<String, ResourceDescriptorConfig> getResourceDescriptorConfigMap(List<ProtoFile> protoFiles, DiagCollector diagCollector) {
    ImmutableMap.Builder<String, ResourceDescriptorConfig> mapBuilder = ImmutableMap.builder();
    // Maps base names to ResourceDescriptors. Used to check redeclarations.
    Map<String, ResourceDescriptor> localDefs = new LinkedHashMap<>();
    // Skip unnecessary file parsing.
    if (!enableProtoAnnotations)
        return mapBuilder.build();
    for (ProtoFile protoFile : protoFiles) {
        List<ResourceDescriptor> resourceDescriptors = new ArrayList<>();
        // Get Resource[Set] definitions from file-level annotations.
        for (ResourceDescriptor definition : getFileLevelResourceDescriptors(protoFile)) {
            collectResourceDescriptor(diagCollector, localDefs, mapBuilder, definition, protoFile, false);
        }
        // Get Resource[Set] definitions from fields in message types.
        for (MessageType message : protoFile.getMessages()) {
            ResourceDescriptor definition = getResourceDescriptor(message);
            if (definition != null) {
                collectResourceDescriptor(diagCollector, localDefs, mapBuilder, definition, protoFile, true);
            }
        }
    }
    return mapBuilder.build();
}
Also used : ProtoFile(com.google.api.tools.framework.model.ProtoFile) ArrayList(java.util.ArrayList) ResourceDescriptorConfig(com.google.api.codegen.config.ResourceDescriptorConfig) ImmutableMap(com.google.common.collect.ImmutableMap) MessageType(com.google.api.tools.framework.model.MessageType) ResourceDescriptor(com.google.api.ResourceDescriptor) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with MessageType

use of com.google.api.tools.framework.model.MessageType in project toolkit by googleapis.

the class ModelTypeNameConverterTestUtil method getTestType.

public static TypeRef getTestType(String fileName, TemporaryFolder tempDir, String... path) {
    String yamlFileName = fileName + ".yaml";
    fileName = fileName + ".proto";
    TestDataLocator locator = TestDataLocator.create(CodegenTestUtil.class);
    locator.addTestDataSource(CodegenTestUtil.class, "testsrc/common");
    Model model = CodegenTestUtil.readModel(locator, tempDir, new String[] { fileName }, new String[] { yamlFileName });
    ProtoContainerElement container = getElementWithName(model.getFiles(), fileName);
    if (container == null) {
        throw new IllegalStateException("file not found: " + fileName);
    }
    for (int i = 0; i < path.length; i++) {
        String pathElement = path[i];
        MessageType messageType = getElementWithName(container.getMessages(), pathElement);
        EnumType enumType = getElementWithName(container.getEnums(), pathElement);
        if (messageType != null) {
            container = messageType;
        } else if (enumType != null && i == path.length - 1) {
            return TypeRef.of(enumType);
        } else if (enumType != null) {
            throw new IllegalStateException("enum type cannot contain further elements: " + enumType);
        } else {
            throw new IllegalStateException("element not found: " + pathElement);
        }
    }
    if (container instanceof MessageType) {
        return TypeRef.of((MessageType) container);
    }
    throw new IllegalStateException("not a type: " + container);
}
Also used : EnumType(com.google.api.tools.framework.model.EnumType) Model(com.google.api.tools.framework.model.Model) ProtoContainerElement(com.google.api.tools.framework.model.ProtoContainerElement) TestDataLocator(com.google.api.tools.framework.model.testing.TestDataLocator) MessageType(com.google.api.tools.framework.model.MessageType)

Example 4 with MessageType

use of com.google.api.tools.framework.model.MessageType in project toolkit by googleapis.

the class ProtoParserTest method testGetResourceType.

@Test
public void testGetResourceType() {
    MessageType getShelfRequest = libraryProtoFile.getMessages().stream().filter(m -> m.getSimpleName().equals("GetShelfRequest")).findFirst().get();
    Field shelves = getShelfRequest.getFields().stream().filter(f -> f.getSimpleName().equals("name")).findFirst().get();
    String shelfType = protoParser.getResourceReference(shelves).getType();
    assertThat(shelfType).isEqualTo("library.googleapis.com/Shelf");
}
Also used : Field(com.google.api.tools.framework.model.Field) MessageType(com.google.api.tools.framework.model.MessageType) GapicCodeGeneratorAnnotationsTest(com.google.api.codegen.protoannotations.GapicCodeGeneratorAnnotationsTest) Test(org.junit.Test)

Example 5 with MessageType

use of com.google.api.tools.framework.model.MessageType in project toolkit by googleapis.

the class ResourceNameMessageConfigs method createFromAnnotations.

@VisibleForTesting
static ResourceNameMessageConfigs createFromAnnotations(DiagCollector diagCollector, List<ProtoFile> protoFiles, Map<String, ResourceNameConfig> resourceNameConfigs, ProtoParser parser, Map<String, ResourceDescriptorConfig> descriptorConfigMap, Map<String, List<ResourceDescriptorConfig>> childParentResourceMap) {
    ImmutableMap.Builder<String, ResourceNameMessageConfig> builder = ImmutableMap.builder();
    for (ProtoFile protoFile : protoFiles) {
        for (MessageType message : protoFile.getMessages()) {
            ImmutableListMultimap.Builder<String, String> fieldEntityMapBuilder = ImmutableListMultimap.builder();
            // Handle resource definitions.
            ResourceDescriptor resourceDescriptor = parser.getResourceDescriptor(message);
            if (resourceDescriptor != null) {
                loadFieldEntityPairFromResourceAnnotation(fieldEntityMapBuilder, resourceDescriptor, message);
            }
            // Handle resource references.
            loadFieldEntityPairFromResourceReferenceAnnotation(fieldEntityMapBuilder, parser, message, resourceNameConfigs, descriptorConfigMap, childParentResourceMap);
            ImmutableListMultimap<String, String> fieldEntityMap = fieldEntityMapBuilder.build();
            if (fieldEntityMap.size() > 0) {
                ResourceNameMessageConfig messageConfig = new AutoValue_ResourceNameMessageConfig(message.getFullName(), fieldEntityMap);
                builder.put(messageConfig.messageName(), messageConfig);
            }
        }
    }
    ImmutableMap<String, ResourceNameMessageConfig> map = builder.build();
    return new AutoValue_ResourceNameMessageConfigs(map, createFieldsByMessage(protoFiles, map));
}
Also used : ProtoFile(com.google.api.tools.framework.model.ProtoFile) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) MessageType(com.google.api.tools.framework.model.MessageType) ResourceDescriptor(com.google.api.ResourceDescriptor) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

MessageType (com.google.api.tools.framework.model.MessageType)16 ProtoFile (com.google.api.tools.framework.model.ProtoFile)7 Field (com.google.api.tools.framework.model.Field)6 ImmutableList (com.google.common.collect.ImmutableList)5 GrpcElementDocView (com.google.api.codegen.viewmodel.GrpcElementDocView)3 GrpcMessageDocView (com.google.api.codegen.viewmodel.GrpcMessageDocView)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ResourceDescriptor (com.google.api.ResourceDescriptor)2 GapicCodeGeneratorAnnotationsTest (com.google.api.codegen.protoannotations.GapicCodeGeneratorAnnotationsTest)2 HeaderRequestParamView (com.google.api.codegen.viewmodel.HeaderRequestParamView)2 ProtoElement (com.google.api.tools.framework.model.ProtoElement)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 ResourceNameMessageConfigProto (com.google.api.codegen.ResourceNameMessageConfigProto)1 GapicMethodConfig (com.google.api.codegen.config.GapicMethodConfig)1 ProtoField (com.google.api.codegen.config.ProtoField)1 ResourceDescriptorConfig (com.google.api.codegen.config.ResourceDescriptorConfig)1 TypeName (com.google.api.codegen.util.TypeName)1 EnumType (com.google.api.tools.framework.model.EnumType)1 Method (com.google.api.tools.framework.model.Method)1