Search in sources :

Example 11 with ProtoFile

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

the class ProtoFileView method protoFiles.

private Set<ProtoFile> protoFiles(Field field) {
    Set<ProtoFile> fields = new HashSet<ProtoFile>();
    if (field.getType().getKind() != Type.TYPE_MESSAGE) {
        return fields;
    }
    MessageType messageType = field.getType().getMessageType();
    fields.add(messageType.getFile());
    for (Field f : messageType.getNonCyclicFields()) {
        fields.addAll(protoFiles(f));
    }
    return fields;
}
Also used : Field(com.google.api.tools.framework.model.Field) ProtoFile(com.google.api.tools.framework.model.ProtoFile) MessageType(com.google.api.tools.framework.model.MessageType) HashSet(java.util.HashSet)

Example 12 with ProtoFile

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

the class GapicProductConfig method create.

/**
 * Creates an instance of GapicProductConfig based on ConfigProto, linking up API interface
 * configurations with specified interfaces in interfaceConfigMap. On errors, null will be
 * returned, and diagnostics are reported to the model.
 */
@Nullable
public static GapicProductConfig create(Model model, ConfigProto configProto) {
    // Get the proto file containing the first interface listed in the config proto, and use it as
    // the assigned file for generated resource names, and to get the default message namespace
    ProtoFile file = model.getSymbolTable().lookupInterface(configProto.getInterfaces(0).getName()).getFile();
    String defaultPackage = file.getProto().getPackage();
    ResourceNameMessageConfigs messageConfigs = ResourceNameMessageConfigs.createMessageResourceTypesConfig(model, configProto, defaultPackage);
    ImmutableMap<String, ResourceNameConfig> resourceNameConfigs = createResourceNameConfigs(model.getDiagCollector(), configProto, file);
    TransportProtocol transportProtocol = TransportProtocol.GRPC;
    LanguageSettingsProto settings = configProto.getLanguageSettings().get(configProto.getLanguage());
    if (settings == null) {
        settings = LanguageSettingsProto.getDefaultInstance();
    }
    ImmutableMap<String, InterfaceConfig> interfaceConfigMap = createInterfaceConfigMap(model.getDiagCollector(), configProto, settings, messageConfigs, resourceNameConfigs, model.getSymbolTable());
    ImmutableList<String> copyrightLines = null;
    ImmutableList<String> licenseLines = null;
    try {
        LicenseHeaderProto licenseHeader = configProto.getLicenseHeader().toBuilder().mergeFrom(settings.getLicenseHeaderOverride()).build();
        copyrightLines = loadCopyrightLines(model.getDiagCollector(), licenseHeader);
        licenseLines = loadLicenseLines(model.getDiagCollector(), licenseHeader);
    } catch (Exception e) {
        model.getDiagCollector().addDiag(Diag.error(SimpleLocation.TOPLEVEL, "Exception: %s", e.getMessage()));
        e.printStackTrace(System.err);
        throw new RuntimeException(e);
    }
    String configSchemaVersion = configProto.getConfigSchemaVersion();
    // TODO(eoogbe): Move the validation logic to GAPIC config advisor.
    if (Strings.isNullOrEmpty(configSchemaVersion)) {
        model.getDiagCollector().addDiag(Diag.error(SimpleLocation.TOPLEVEL, "config_schema_version field is required in GAPIC yaml."));
    }
    if (interfaceConfigMap == null || copyrightLines == null || licenseLines == null) {
        return null;
    }
    return new AutoValue_GapicProductConfig(interfaceConfigMap, settings.getPackageName(), settings.getDomainLayerLocation(), settings.getReleaseLevel(), messageConfigs, copyrightLines, licenseLines, resourceNameConfigs, transportProtocol, createResponseFieldConfigMap(messageConfigs, resourceNameConfigs), configSchemaVersion);
}
Also used : LicenseHeaderProto(com.google.api.codegen.LicenseHeaderProto) ProtoFile(com.google.api.tools.framework.model.ProtoFile) IOException(java.io.IOException) LanguageSettingsProto(com.google.api.codegen.LanguageSettingsProto) Nullable(javax.annotation.Nullable)

Example 13 with ProtoFile

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

the class ResourceNameMessageConfigsTest method testCreateFlattenings.

@Test
public void testCreateFlattenings() {
    ProtoMethodModel methodModel = new ProtoMethodModel(createShelvesMethod);
    Field bookField = Mockito.mock(Field.class);
    Mockito.when(bookField.getType()).thenReturn(TypeRef.of(bookType));
    Mockito.when(bookField.getParent()).thenReturn(createShelvesRequest);
    Mockito.when(bookField.getSimpleName()).thenReturn("book");
    Mockito.when(bookType.getFields()).thenReturn(ImmutableList.of());
    Field nameField = Mockito.mock(Field.class);
    Mockito.when(nameField.getParent()).thenReturn(createShelvesRequest);
    Mockito.when(createShelvesRequest.getFullName()).thenReturn("library.CreateShelvesRequest");
    Mockito.when(nameField.getType()).thenReturn(TypeRef.fromPrimitiveName("string"));
    Mockito.when(nameField.getSimpleName()).thenReturn("name");
    Mockito.doReturn(ResourceReference.newBuilder().setType("library.googleapis.com/Shelf").build()).when(protoParser).getResourceReference(nameField);
    Mockito.doReturn(true).when(protoParser).hasResourceReference(nameField);
    Mockito.when(createShelvesRequest.lookupField("book")).thenReturn(bookField);
    Mockito.when(createShelvesRequest.lookupField("name")).thenReturn(nameField);
    Mockito.when(createShelvesRequest.getFields()).thenReturn(ImmutableList.of(bookField, nameField));
    Mockito.doReturn(ResourceReference.newBuilder().setType("library.googleapis.com/Shelf").build()).when(protoParser).getResourceReference(shelfName);
    Mockito.doReturn(true).when(protoParser).hasResourceReference(shelfName);
    Mockito.doReturn(ResourceReference.newBuilder().setType("library.googleapis.com/Book").build()).when(protoParser).getResourceReference(bookField);
    Mockito.doReturn(true).when(protoParser).hasResourceReference(bookField);
    Mockito.doReturn(ResourceReference.newBuilder().setType("library.googleapis.com/Shelf").build()).when(protoParser).getResourceReference(nameField);
    Mockito.doReturn(true).when(protoParser).hasResourceReference(nameField);
    // ProtoFile contributes flattenings {["name", "book"], ["name"]}.
    Mockito.doReturn(Arrays.asList(Arrays.asList("name", "book"), Arrays.asList("name"))).when(protoParser).getMethodSignatures(createShelvesMethod);
    Mockito.when(protoFile.getMessages()).thenReturn(ImmutableList.of(bookMessage, shelfMessage, createShelvesRequest));
    // Gapic config contributes flattenings {["book"]}.
    MethodConfigProto methodConfigProto = MethodConfigProto.newBuilder().setName(CREATE_SHELF_METHOD_NAME).setFlattening(FlatteningConfigProto.newBuilder().addGroups(FlatteningGroupProto.newBuilder().addAllParameters(Arrays.asList("book")))).setResourceNameTreatment(ResourceNameTreatment.STATIC_TYPES).build();
    DiagCollector diagCollector = new BoundedDiagCollector();
    assertThat(diagCollector.getErrorCount()).isEqualTo(0);
    ImmutableMap<String, ResourceNameConfig> resourceNameConfigs = GapicProductConfig.createResourceNameConfigsFromAnnotationsAndGapicConfig(null, diagCollector, ConfigProto.getDefaultInstance(), protoFile, TargetLanguage.CSHARP, resourceDescriptorConfigMap, resourceDescriptorConfigMap.keySet(), Collections.emptySet(), Collections.emptyMap(), patternResourceDescriptorMap, Collections.emptyMap(), "library");
    assertThat(diagCollector.getErrorCount()).isEqualTo(0);
    ResourceNameMessageConfigs messageConfigs = ResourceNameMessageConfigs.createFromAnnotations(diagCollector, sourceProtoFiles, resourceNameConfigs, protoParser, resourceDescriptorConfigMap, Collections.emptyMap());
    List<FlatteningConfig> flatteningConfigs = new ArrayList<>(FlatteningConfig.createFlatteningConfigs(diagCollector, messageConfigs, resourceNameConfigs, methodConfigProto, methodModel, protoParser));
    assertThat(diagCollector.getErrorCount()).isEqualTo(0);
    List<Diag> warningDiags = diagCollector.getDiags().stream().filter(d -> d.getKind().equals(Kind.WARNING)).collect(Collectors.toList());
    assertThat(flatteningConfigs).isNotNull();
    assertThat(flatteningConfigs.size()).isEqualTo(6);
    // Check the flattening from the Gapic config.
    List<FlatteningConfig> flatteningConfigFromGapicConfigs = flatteningConfigs.stream().filter(f -> f.getFlattenedFieldConfigs().size() == 1 && f.getFlattenedFieldConfigs().containsKey("book")).collect(ImmutableList.toImmutableList());
    assertThat(flatteningConfigFromGapicConfigs.size()).isEqualTo(2);
    for (FlatteningConfig configFromGapicConfig : flatteningConfigFromGapicConfigs) {
        Map<String, FieldConfig> paramsFromGapicConfigFlattening = configFromGapicConfig.getFlattenedFieldConfigs();
        assertThat(paramsFromGapicConfigFlattening.get("book").getField().getSimpleName()).isEqualTo("book");
        assertThat(((ProtoField) paramsFromGapicConfigFlattening.get("book").getField()).getType().getProtoType().getMessageType()).isEqualTo(bookType);
    }
    flatteningConfigs.removeAll(flatteningConfigFromGapicConfigs);
    // Check the flattenings from the protofile annotations.
    flatteningConfigs.sort(Comparator.comparingInt(c -> Iterables.size(c.getFlattenedFields())));
    FlatteningConfig shelfFlattening = flatteningConfigs.get(0);
    assertThat(Iterables.size(shelfFlattening.getFlattenedFields())).isEqualTo(1);
    FieldConfig nameConfig = shelfFlattening.getFlattenedFieldConfigs().get("name");
    assertThat(nameConfig.getResourceNameTreatment()).isEqualTo(ResourceNameTreatment.STATIC_TYPES);
    assertThat(((SingleResourceNameConfig) nameConfig.getResourceNameConfig()).getNamePattern()).isEqualTo(PROTO_SHELF_PATH);
    FlatteningConfig shelfAndBookFlattening = flatteningConfigs.get(2);
    assertThat(Iterables.size(shelfAndBookFlattening.getFlattenedFields())).isEqualTo(2);
    FieldConfig nameConfig2 = shelfAndBookFlattening.getFlattenedFieldConfigs().get("name");
    assertThat(nameConfig2.getResourceNameTreatment()).isEqualTo(ResourceNameTreatment.STATIC_TYPES);
    // Use PROTO_SHELF_PATH over GAPIC_SHELF_PATH.
    assertThat(((SingleResourceNameConfig) nameConfig2.getResourceNameConfig()).getNamePattern()).isEqualTo(PROTO_SHELF_PATH);
    FieldConfig bookConfig = shelfAndBookFlattening.getFlattenedFieldConfigs().get("book");
    assertThat(bookConfig.getResourceNameTreatment()).isEqualTo(ResourceNameTreatment.STATIC_TYPES);
    // Use the resource name path from proto file.
    assertThat(((SingleResourceNameConfig) bookConfig.getResourceNameConfig()).getNamePattern()).isEqualTo(PROTO_BOOK_PATH);
    assertThat(((ProtoTypeRef) bookConfig.getField().getType()).getProtoType().getMessageType()).isEqualTo(bookType);
    // Restore protoFile.getMessages()
    Mockito.when(protoFile.getMessages()).thenReturn(ImmutableList.of(bookMessage, shelfMessage));
}
Also used : TypeRef(com.google.api.tools.framework.model.TypeRef) CollectionOneofProto(com.google.api.codegen.CollectionOneofProto) Diag(com.google.api.tools.framework.model.Diag) Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) MethodConfigProto(com.google.api.codegen.MethodConfigProto) MessageType(com.google.api.tools.framework.model.MessageType) CollectionConfigProto(com.google.api.codegen.CollectionConfigProto) ProtoFile(com.google.api.tools.framework.model.ProtoFile) Field(com.google.api.tools.framework.model.Field) Method(com.google.api.tools.framework.model.Method) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Spy(org.mockito.Spy) ResourceNameMessageConfigProto(com.google.api.codegen.ResourceNameMessageConfigProto) TargetLanguage(com.google.api.codegen.common.TargetLanguage) ResourceNameTreatment(com.google.api.codegen.ResourceNameTreatment) ConfigProto(com.google.api.codegen.ConfigProto) Kind(com.google.api.tools.framework.model.Diag.Kind) Before(org.junit.Before) ResourceDescriptor(com.google.api.ResourceDescriptor) ImmutableSet(com.google.common.collect.ImmutableSet) ResourceReference(com.google.api.ResourceReference) ImmutableMap(com.google.common.collect.ImmutableMap) InterfaceConfigProto(com.google.api.codegen.InterfaceConfigProto) FlatteningGroupProto(com.google.api.codegen.FlatteningGroupProto) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) Name(com.google.api.codegen.util.Name) Mockito(org.mockito.Mockito) List(java.util.List) DiagCollector(com.google.api.tools.framework.model.DiagCollector) Interface(com.google.api.tools.framework.model.Interface) FileDescriptorProto(com.google.protobuf.DescriptorProtos.FileDescriptorProto) ProtoParser(com.google.api.codegen.util.ProtoParser) Comparator(java.util.Comparator) FlatteningConfigProto(com.google.api.codegen.FlatteningConfigProto) BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) Collections(java.util.Collections) BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) ArrayList(java.util.ArrayList) Diag(com.google.api.tools.framework.model.Diag) Field(com.google.api.tools.framework.model.Field) DiagCollector(com.google.api.tools.framework.model.DiagCollector) BoundedDiagCollector(com.google.api.tools.framework.model.BoundedDiagCollector) MethodConfigProto(com.google.api.codegen.MethodConfigProto) Test(org.junit.Test)

Example 14 with ProtoFile

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

the class CSharpModelTypeNameConverter method getTypeName.

@Override
public TypeName getTypeName(ProtoElement elem) {
    // Handle special wrapper types first
    String wrapper = WRAPPER_TYPE_MAP.getOrDefault(elem.getFullName(), null);
    if (wrapper != null) {
        return typeNameConverter.getTypeName(wrapper);
    }
    // Handle nested types, construct the required type prefix
    ProtoElement parentEl = elem.getParent();
    String shortNamePrefix = "";
    while (parentEl != null && parentEl instanceof MessageType) {
        shortNamePrefix = parentEl.getSimpleName() + "+Types+" + shortNamePrefix;
        parentEl = parentEl.getParent();
    }
    String prefix = "";
    if (parentEl instanceof ProtoFile) {
        ProtoFile protoFile = (ProtoFile) parentEl;
        String namespace = protoFile.getProto().getOptions().getCsharpNamespace();
        if (Strings.isNullOrEmpty(namespace)) {
            for (String name : Splitter.on('.').split(parentEl.getFullName())) {
                prefix += Name.from(name).toUpperCamelAndDigits() + ".";
            }
        } else {
            prefix = namespace + ".";
        }
    }
    String shortName = shortNamePrefix + elem.getSimpleName();
    return typeNameConverter.getTypeName(prefix + shortName);
}
Also used : ProtoElement(com.google.api.tools.framework.model.ProtoElement) ProtoFile(com.google.api.tools.framework.model.ProtoFile) MessageType(com.google.api.tools.framework.model.MessageType)

Example 15 with ProtoFile

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

the class ProtoFiles method getProtoFiles.

/**
 * Gets the ProtoFile objects in which the fields of the reachable methods in the model are
 * defined.
 */
public static Iterable<ProtoFile> getProtoFiles(GapicProductConfig productConfig) {
    Set<ProtoFile> files = newFileSet();
    for (InterfaceConfig interfaceConfig : productConfig.getInterfaceConfigMap().values()) {
        for (MethodConfig methodConfig : interfaceConfig.getMethodConfigs()) {
            MethodModel method = methodConfig.getMethodModel();
            files.addAll(getFilesForMessage(((ProtoTypeRef) method.getInputType()).getProtoType().getMessageType(), false));
            files.addAll(getFilesForMessage(((ProtoTypeRef) method.getOutputType()).getProtoType().getMessageType(), false));
        }
    }
    return files;
}
Also used : MethodConfig(com.google.api.codegen.config.MethodConfig) InterfaceConfig(com.google.api.codegen.config.InterfaceConfig) MethodModel(com.google.api.codegen.config.MethodModel) ProtoFile(com.google.api.tools.framework.model.ProtoFile)

Aggregations

ProtoFile (com.google.api.tools.framework.model.ProtoFile)19 MessageType (com.google.api.tools.framework.model.MessageType)9 Field (com.google.api.tools.framework.model.Field)5 TypeRef (com.google.api.tools.framework.model.TypeRef)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 ArrayList (java.util.ArrayList)4 ResourceDescriptor (com.google.api.ResourceDescriptor)3 ProtoElement (com.google.api.tools.framework.model.ProtoElement)3 HashSet (java.util.HashSet)3 ProtoFileView (com.google.api.codegen.ProtoFileView)2 ResourceNameMessageConfigProto (com.google.api.codegen.ResourceNameMessageConfigProto)2 ProtoTypeRef (com.google.api.codegen.config.ProtoTypeRef)2 ViewModel (com.google.api.codegen.viewmodel.ViewModel)2 Interface (com.google.api.tools.framework.model.Interface)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Map (java.util.Map)2