Search in sources :

Example 6 with GapicProductConfig

use of com.google.api.codegen.config.GapicProductConfig in project toolkit by googleapis.

the class DiscoGapicGeneratorApi method getProviders.

/**
 * From config file paths, constructs the DiscoGapicProviders to run.
 */
@VisibleForTesting
static List<GapicProvider> getProviders(String discoveryDocPath, List<String> configFileNames, String packageConfigFile, List<String> enabledArtifacts) throws IOException {
    if (!new File(discoveryDocPath).exists()) {
        throw new IOException("File not found: " + discoveryDocPath);
    }
    Reader reader = new InputStreamReader(new FileInputStream(new File(discoveryDocPath)));
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(reader);
    // Read the YAML config and convert it to proto.
    if (configFileNames.size() == 0) {
        throw new IOException(String.format("--%s must be provided", GENERATOR_CONFIG_FILES.name()));
    }
    ConfigProto configProto = loadConfigFromFiles(configFileNames);
    if (configProto == null) {
        throw new IOException("Failed to load config proto.");
    }
    PackageMetadataConfig packageConfig = null;
    if (!Strings.isNullOrEmpty(packageConfigFile)) {
        String contents = new String(Files.readAllBytes(Paths.get(packageConfigFile)), StandardCharsets.UTF_8);
        packageConfig = PackageMetadataConfig.createFromString(contents);
    }
    GeneratorProto generator = configProto.getGenerator();
    String language = configProto.getLanguage();
    String defaultPackageName = configProto.getLanguageSettingsMap().get(language).getPackageName();
    SurfaceNamer surfaceNamer = null;
    if (language.equals(JAVA)) {
        surfaceNamer = new JavaSurfaceNamer(defaultPackageName, defaultPackageName, new JavaNameFormatter());
    }
    if (surfaceNamer == null) {
        throw new UnsupportedOperationException("DiscoGapicGeneratorApi: language \"" + language + "\" not yet supported");
    }
    DiscoApiModel model = new DiscoApiModel(Document.from(new DiscoveryNode(root)), defaultPackageName);
    GapicProductConfig productConfig = GapicProductConfig.create(model, configProto, surfaceNamer);
    String factory = generator.getFactory();
    String id = generator.getId();
    DiscoGapicProviderFactory providerFactory = createProviderFactory(factory);
    GapicGeneratorConfig generatorConfig = GapicGeneratorConfig.newBuilder().id(id).enabledArtifacts(enabledArtifacts).build();
    return providerFactory.create(model, productConfig, generatorConfig, packageConfig);
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) DiscoveryNode(com.google.api.codegen.discovery.DiscoveryNode) InputStreamReader(java.io.InputStreamReader) ConfigYamlReader(com.google.api.codegen.configgen.ConfigYamlReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) FileInputStream(java.io.FileInputStream) JavaNameFormatter(com.google.api.codegen.util.java.JavaNameFormatter) DiscoGapicProviderFactory(com.google.api.codegen.discogapic.DiscoGapicProviderFactory) DiscoApiModel(com.google.api.codegen.config.DiscoApiModel) GapicGeneratorConfig(com.google.api.codegen.gapic.GapicGeneratorConfig) PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JavaSurfaceNamer(com.google.api.codegen.transformer.java.JavaSurfaceNamer) SurfaceNamer(com.google.api.codegen.transformer.SurfaceNamer) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with GapicProductConfig

use of com.google.api.codegen.config.GapicProductConfig in project toolkit by googleapis.

the class GapicTestBase2 method createTestConfig.

/**
 * Creates the constructor arguments to be passed onto this class (GapicTestBase2) to create test
 * methods. The idForFactory String is passed to GapicProviderFactory to get the GapicProviders
 * provided by that id, and then the snippet file names are scraped from those providers, and a
 * set of arguments is created for each combination of GapicProvider x snippet that
 * GapicProviderFactory returns.
 */
public static Object[] createTestConfig(String idForFactory, String[] gapicConfigFileNames, String packageConfigFileName, String apiName) {
    Model model = Model.create(Service.getDefaultInstance());
    GapicProductConfig productConfig = GapicProductConfig.createDummyInstance();
    PackageMetadataConfig packageConfig = PackageMetadataConfig.createDummyPackageMetadataConfig();
    GapicGeneratorConfig generatorConfig = GapicGeneratorConfig.newBuilder().id(idForFactory).enabledArtifacts(Arrays.asList("surface", "test")).build();
    List<GapicProvider> providers = MainGapicProviderFactory.defaultCreate(model, productConfig, generatorConfig, packageConfig);
    List<String> snippetNames = new ArrayList<>();
    for (GapicProvider provider : providers) {
        snippetNames.addAll(provider.getSnippetFileNames());
    }
    String baseline = idForFactory + "_" + apiName + ".baseline";
    return new Object[] { idForFactory, gapicConfigFileNames, packageConfigFileName, snippetNames, apiName, baseline };
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) GapicGeneratorConfig(com.google.api.codegen.gapic.GapicGeneratorConfig) Model(com.google.api.tools.framework.model.Model) PackageMetadataConfig(com.google.api.codegen.config.PackageMetadataConfig) GapicProvider(com.google.api.codegen.gapic.GapicProvider) ArrayList(java.util.ArrayList)

Example 8 with GapicProductConfig

use of com.google.api.codegen.config.GapicProductConfig in project toolkit by googleapis.

the class GapicTestBase2 method run.

@Override
public Map<String, Doc> run() {
    model.establishStage(Merged.KEY);
    if (model.getDiagCollector().getErrorCount() > 0) {
        for (Diag diag : model.getDiagCollector().getDiags()) {
            System.err.println(diag.toString());
        }
        return null;
    }
    GapicProductConfig productConfig = GapicProductConfig.create(model, gapicConfig);
    if (productConfig == null) {
        for (Diag diag : model.getDiagCollector().getDiags()) {
            System.err.println(diag.toString());
        }
        return null;
    }
    List<String> enabledArtifacts = new ArrayList<>();
    if (hasSmokeTestConfig(productConfig)) {
        enabledArtifacts.addAll(Arrays.asList("surface", "test"));
    }
    GapicGeneratorConfig generatorConfig = GapicGeneratorConfig.newBuilder().id(idForFactory).enabledArtifacts(enabledArtifacts).build();
    List<GapicProvider> providers = MainGapicProviderFactory.defaultCreate(model, productConfig, generatorConfig, packageConfig);
    List<String> disabledGen = new ArrayList<>(snippetNames);
    for (GapicProvider provider : providers) {
        disabledGen.removeAll(provider.getSnippetFileNames());
    }
    for (String gen : disabledGen) {
        testOutput().printf("%s generation is not enabled for this test case.\n", gen);
    }
    // Don't run any providers we're not testing.
    ArrayList<GapicProvider> testedProviders = new ArrayList<>();
    for (GapicProvider provider : providers) {
        if (!Collections.disjoint(provider.getSnippetFileNames(), snippetNames)) {
            testedProviders.add(provider);
        }
    }
    boolean reportDiag = false;
    Map<String, Doc> output = new TreeMap<>();
    for (GapicProvider provider : testedProviders) {
        Map<String, Doc> out = provider.generate();
        if (output == null) {
            reportDiag = true;
        } else {
            if (!Collections.disjoint(out.keySet(), output.keySet())) {
                throw new IllegalStateException("file conflict");
            }
            output.putAll(out);
        }
    }
    if (reportDiag) {
        for (Diag diag : model.getDiagCollector().getDiags()) {
            testOutput().println(diag.toString());
        }
    }
    return output;
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) ArrayList(java.util.ArrayList) GapicProvider(com.google.api.codegen.gapic.GapicProvider) Diag(com.google.api.tools.framework.model.Diag) TreeMap(java.util.TreeMap) GapicGeneratorConfig(com.google.api.codegen.gapic.GapicGeneratorConfig) Doc(com.google.api.tools.framework.snippet.Doc)

Example 9 with GapicProductConfig

use of com.google.api.codegen.config.GapicProductConfig in project toolkit by googleapis.

the class PhpGapicCodePathMapperTest method getOutputPathTest.

@Test
public void getOutputPathTest() {
    PhpGapicCodePathMapper pathMapper = PhpGapicCodePathMapper.newBuilder().setPrefix("prefix").setSuffix("suffix").build();
    GapicProductConfig configWithGoogleCloud = GapicProductConfig.createDummyInstance(ImmutableMap.<String, InterfaceConfig>of(), "Google\\Cloud\\Sample\\Package\\V1", "", null);
    Truth.assertThat(pathMapper.getOutputPath(null, configWithGoogleCloud)).isEqualTo("prefix/Sample/Package/V1/suffix");
    GapicProductConfig configWithGoogleNonCloud = GapicProductConfig.createDummyInstance(ImmutableMap.<String, InterfaceConfig>of(), "Google\\NonCloud\\Sample\\Package\\V1", "", null);
    Truth.assertThat(pathMapper.getOutputPath(null, configWithGoogleNonCloud)).isEqualTo("prefix/NonCloud/Sample/Package/V1/suffix");
    GapicProductConfig configWithAlphabet = GapicProductConfig.createDummyInstance(ImmutableMap.<String, InterfaceConfig>of(), "Alphabet\\Google\\Cloud\\Sample\\Package\\V1", "", null);
    Truth.assertThat(pathMapper.getOutputPath(null, configWithAlphabet)).isEqualTo("prefix/Alphabet/Google/Cloud/Sample/Package/V1/suffix");
}
Also used : GapicProductConfig(com.google.api.codegen.config.GapicProductConfig) PhpGapicCodePathMapper(com.google.api.codegen.php.PhpGapicCodePathMapper) Test(org.junit.Test)

Aggregations

GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)9 GapicGeneratorConfig (com.google.api.codegen.gapic.GapicGeneratorConfig)4 PackageMetadataConfig (com.google.api.codegen.config.PackageMetadataConfig)3 GapicProvider (com.google.api.codegen.gapic.GapicProvider)3 ApiModel (com.google.api.codegen.config.ApiModel)2 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)2 Diag (com.google.api.tools.framework.model.Diag)2 Model (com.google.api.tools.framework.model.Model)2 Doc (com.google.api.tools.framework.snippet.Doc)2 ArrayList (java.util.ArrayList)2 TreeMap (java.util.TreeMap)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Adviser (com.google.api.codegen.advising.Adviser)1 DiscoApiModel (com.google.api.codegen.config.DiscoApiModel)1 GapicInterfaceConfig (com.google.api.codegen.config.GapicInterfaceConfig)1 InterfaceModel (com.google.api.codegen.config.InterfaceModel)1 MethodModel (com.google.api.codegen.config.MethodModel)1 ProtoInterfaceModel (com.google.api.codegen.config.ProtoInterfaceModel)1 ConfigYamlReader (com.google.api.codegen.configgen.ConfigYamlReader)1