use of com.google.api.codegen.config.TransportProtocol in project toolkit by googleapis.
the class GapicGeneratorApp method process.
@Override
protected void process() throws Exception {
String protoPackage = Strings.emptyToNull(options.get(PROTO_PACKAGE));
// Read the GAPIC config, if it was given, and convert it to proto.
List<String> configFileNames = options.get(GENERATOR_CONFIG_FILES);
ConfigProto configProto = null;
if (configFileNames.size() > 0) {
// Read the YAML config and convert it to proto.
ConfigSource configSource = loadConfigFromFiles(configFileNames, ConfigProto.getDescriptor().getFullName(), ConfigProto.getDefaultInstance());
if (configSource == null) {
return;
}
configProto = (ConfigProto) configSource.getConfig();
if (configProto == null) {
return;
}
}
// Consume gRPC Service Config if it is given with gapic_v2.
String gRPCServiceConfigPath = options.get(GRPC_SERVICE_CONFIG);
ServiceConfig gRPCServiceConfig = null;
if (!Strings.isNullOrEmpty(gRPCServiceConfigPath) && configProto.getConfigSchemaVersion().equals("2.0.0")) {
ServiceConfig.Builder builder = ServiceConfig.newBuilder();
FileReader file = new FileReader(gRPCServiceConfigPath);
JsonFormat.parser().merge(file, builder);
gRPCServiceConfig = builder.build();
}
// Read the sample configs, if they are given, and convert them to protos.
SampleConfigProto sampleConfigProto = null;
List<String> sampleConfigFileNames = options.get(SAMPLE_CONFIG_FILES);
if (sampleConfigFileNames.size() > 0) {
ConfigSource configSource = loadConfigFromFiles(SampleConfigSanitizer.sanitize(sampleConfigFileNames), SampleConfigProto.getDescriptor().getFullName(), SampleConfigProto.getDefaultInstance());
// TODO(hzyi): Verify this works for repeated fields as well
// TODO(hzyi): Allow users to put arbitrary top-level directives not
// used by gapic-generator
sampleConfigProto = (SampleConfigProto) configSource.getConfig();
}
model.establishStage(Merged.KEY);
if (model.getDiagReporter().getDiagCollector().getErrorCount() > 0) {
for (Diag diag : model.getDiagReporter().getDiagCollector().getDiags()) {
System.err.println(diag.toString());
}
return;
}
ApiDefaultsConfig apiDefaultsConfig = ApiDefaultsConfig.load();
DependenciesConfig dependenciesConfig = DependenciesConfig.load();
TargetLanguage language;
if (!Strings.isNullOrEmpty(options.get(LANGUAGE))) {
language = TargetLanguage.fromString(options.get(LANGUAGE).toUpperCase());
} else {
throw new IllegalArgumentException("Language not set by --language option.");
}
String clientPackage = Strings.emptyToNull(options.get(CLIENT_PACKAGE));
String transport = options.get(TRANSPORT).toLowerCase();
TransportProtocol tp;
if (transport.equals("grpc")) {
tp = TransportProtocol.GRPC;
} else if (transport.equals("rest")) {
tp = TransportProtocol.HTTP;
} else {
throw new IllegalArgumentException("Unknown transport protocol: " + transport);
}
GapicProductConfig productConfig = GapicProductConfig.create(model, configProto, sampleConfigProto, protoPackage, clientPackage, language, gRPCServiceConfig, tp);
if (productConfig == null) {
ToolUtil.reportDiags(model.getDiagReporter().getDiagCollector(), true);
return;
}
PackagingConfig packagingConfig;
if (!Strings.isNullOrEmpty(options.get(PACKAGE_CONFIG2_FILE))) {
packagingConfig = PackagingConfig.load(options.get(PACKAGE_CONFIG2_FILE));
} else {
packagingConfig = PackagingConfig.loadFromProductConfig(productConfig.getInterfaceConfigMap());
}
PackageMetadataConfig packageConfig = PackageMetadataConfig.createFromPackaging(apiDefaultsConfig, dependenciesConfig, packagingConfig);
// TODO(hzyi-google): Once we switch to sample configs, require an
// additional check to generate samples:
// `sampleConfigProto != null`
ArtifactFlags artifactFlags = new ArtifactFlags(options.get(ENABLED_ARTIFACTS), artifactType, options.get(DEV_SAMPLES));
List<CodeGenerator<?>> generators = GapicGeneratorFactory.create(language, model, productConfig, packageConfig, artifactFlags);
ImmutableMap.Builder<String, GeneratedResult<?>> generatedResults = ImmutableMap.builder();
for (CodeGenerator<?> generator : generators) {
Map<String, ? extends GeneratedResult<?>> generatorResult = generator.generate();
for (Map.Entry<String, ? extends GeneratedResult<?>> entry : generatorResult.entrySet()) {
generatedResults.put(entry.getKey(), entry.getValue());
}
}
gapicWriter.writeCodeGenOutput(generatedResults.build(), model.getDiagReporter().getDiagCollector());
}
use of com.google.api.codegen.config.TransportProtocol in project toolkit by googleapis.
the class JavaFeatureConfig method create.
public static JavaFeatureConfig create(GapicProductConfig productConfig) {
boolean enableStringFormatFunctions;
if (productConfig.enableStringFormattingFunctionsOverride() != null) {
enableStringFormatFunctions = productConfig.enableStringFormattingFunctionsOverride().booleanValue();
} else {
ResourceNameMessageConfigs resourceNameMessageConfigs = productConfig.getResourceNameMessageConfigs();
enableStringFormatFunctions = resourceNameMessageConfigs == null || resourceNameMessageConfigs.isEmpty();
}
TransportProtocol tp = productConfig.getTransportProtocol();
return JavaFeatureConfig.newBuilder().resourceNameTypesEnabled(true).enableMixins(tp != TransportProtocol.HTTP).enableGrpcStreaming(tp != TransportProtocol.HTTP).enableStringFormatFunctions(enableStringFormatFunctions).useStaticCreateMethodForOneofs(productConfig.getProtoParser().isProtoAnnotationsEnabled()).build();
}
Aggregations