Search in sources :

Example 1 with DiscoveryNode

use of com.google.api.codegen.discovery.DiscoveryNode 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 2 with DiscoveryNode

use of com.google.api.codegen.discovery.DiscoveryNode in project toolkit by googleapis.

the class DocumentGenerator method createDocument.

private static Document createDocument(String discoveryDocPath) throws IOException {
    if (!new File(discoveryDocPath).exists()) {
        throw new FileNotFoundException("Discovery document filepath not found.");
    }
    Reader reader = new InputStreamReader(new FileInputStream(new File(discoveryDocPath)));
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(reader);
    return Document.from(new DiscoveryNode(root));
}
Also used : DiscoveryNode(com.google.api.codegen.discovery.DiscoveryNode) InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonNode(com.fasterxml.jackson.databind.JsonNode) File(java.io.File) FileInputStream(java.io.FileInputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DiscoveryNode (com.google.api.codegen.discovery.DiscoveryNode)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 DiscoApiModel (com.google.api.codegen.config.DiscoApiModel)1 GapicProductConfig (com.google.api.codegen.config.GapicProductConfig)1 PackageMetadataConfig (com.google.api.codegen.config.PackageMetadataConfig)1 ConfigYamlReader (com.google.api.codegen.configgen.ConfigYamlReader)1 DiscoGapicProviderFactory (com.google.api.codegen.discogapic.DiscoGapicProviderFactory)1 GapicGeneratorConfig (com.google.api.codegen.gapic.GapicGeneratorConfig)1 SurfaceNamer (com.google.api.codegen.transformer.SurfaceNamer)1 JavaSurfaceNamer (com.google.api.codegen.transformer.java.JavaSurfaceNamer)1 JavaNameFormatter (com.google.api.codegen.util.java.JavaNameFormatter)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1