Skip to content

Commit

Permalink
[draft] set go_package for generated FHIR protos
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 561330049
  • Loading branch information
nikklassen authored and copybara-github committed Aug 30, 2023
1 parent ce405fd commit 9ed52e5
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 366 deletions.
656 changes: 317 additions & 339 deletions go/proto/google/fhir/proto/profile_config_go_proto/profile_config.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions java/com/google/fhir/protogen/GeneratorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private GeneratorUtils() {}

// The path for annotation definition of proto options.
public static final String ANNOTATION_PATH = "proto/google/fhir/proto";
public static final String GO_PACKAGE_PREFIX = "github.com/google/fhir/go";

private static final Pattern WORD_BREAK_PATTERN = Pattern.compile("[^A-Za-z0-9]+([A-Za-z0-9])");
private static final Pattern ACRONYM_PATTERN = Pattern.compile("([A-Z])([A-Z]+)(?![a-z])");
Expand Down
3 changes: 3 additions & 0 deletions java/com/google/fhir/protogen/ProtoFilePrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ private String printOptions(FileDescriptorProto fileDescriptor, String packageNa
.append(fileOptions.getExtension(Annotations.fhirVersion))
.append(";\n");
}
if (fileOptions.hasGoPackage()) {
options.append("option go_package = \"").append(fileOptions.getGoPackage()).append("\";\n");
}
return options.toString();
}

Expand Down
3 changes: 0 additions & 3 deletions java/com/google/fhir/protogen/ProtoGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1827,9 +1827,6 @@ public FileDescriptorProto generateFileDescriptor(
if (!packageInfo.getJavaProtoPackage().isEmpty()) {
options.setJavaPackage(packageInfo.getJavaProtoPackage()).setJavaMultipleFiles(true);
}
if (!packageInfo.getGoProtoPackage().isEmpty()) {
options.setGoPackage(packageInfo.getGoProtoPackage());
}
options.setExtension(Annotations.fhirVersion, FhirVersion.R4.toAnnotation());
builder.setOptions(options);
for (StructureDefinition def : defs) {
Expand Down
16 changes: 16 additions & 0 deletions java/com/google/fhir/protogen/ProtoGeneratorMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.fhir.r4.core.TypeDerivationRuleCode;
import com.google.protobuf.DescriptorProtos.DescriptorProto;
import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
import com.google.protobuf.DescriptorProtos.FileOptions;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -382,6 +383,7 @@ private void addZipEntry(
ProtoFilePrinter printer,
ZipOutputStream zipOutputStream)
throws IOException {
fileProto = updateGoPackage(fileProto, filename);
try {
zipOutputStream.putNextEntry(new ZipEntry(filename));
byte[] entryBytes = printer.print(maybeSortFile(fileProto)).getBytes(UTF_8);
Expand All @@ -391,6 +393,20 @@ private void addZipEntry(
}
}

private FileDescriptorProto updateGoPackage(FileDescriptorProto fileProto, String fileName) {
// Strip .proto suffix
String fileBaseName = fileName.substring(0, fileName.length() - 6);
String goPackage =
GeneratorUtils.GO_PACKAGE_PREFIX
+ "/"
+ args.directoryInSource
+ "/"
+ fileBaseName
+ "_go_proto";
FileOptions options = fileProto.getOptions().toBuilder().setGoPackage(goPackage).build();
return fileProto.toBuilder().setOptions(options).build();
}

String resourceNameToFileName(String resourceName, ProtoGenerator generator) {
return CaseFormat.UPPER_CAMEL.to(
CaseFormat.LOWER_UNDERSCORE,
Expand Down
25 changes: 23 additions & 2 deletions java/com/google/fhir/protogen/ProtoGeneratorMainV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.fhir.r4.core.StructureDefinitionKindCode;
import com.google.fhir.r4.core.TypeDerivationRuleCode;
import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
import com.google.protobuf.DescriptorProtos.FileOptions;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -129,8 +130,8 @@ void run() throws IOException, InvalidFhirException {
try (ZipOutputStream zipOutputStream =
new ZipOutputStream(new FileOutputStream(new File(args.outputDirectory, "output.zip")))) {
try {
addEntry(zipOutputStream, printer, codesFileDescriptor, "codes.proto");
addEntry(zipOutputStream, printer, valueSetsFileDescriptor, "valuesets.proto");
addEntry(zipOutputStream, printer, config, codesFileDescriptor, "codes.proto");
addEntry(zipOutputStream, printer, config, valueSetsFileDescriptor, "valuesets.proto");

List<String> resourceNames = new ArrayList<>();
StructureDefinition bundleDefinition = null;
Expand All @@ -150,6 +151,7 @@ void run() throws IOException, InvalidFhirException {
addEntry(
zipOutputStream,
printer,
config,
generator.generateResourceFileDescriptor(structDef),
"resources/" + GeneratorUtils.resourceNameToFileName(resourceName));
}
Expand All @@ -160,6 +162,7 @@ void run() throws IOException, InvalidFhirException {
addEntry(
zipOutputStream,
printer,
config,
generator.generateBundleAndContainedResource(
bundleDefinition, resourceNames, args.containedResourceOffset),
"resources/bundle_and_contained_resource.proto");
Expand All @@ -169,6 +172,7 @@ void run() throws IOException, InvalidFhirException {
addEntry(
zipOutputStream,
printer,
config,
generator.generateDatatypesFileDescriptor(resourceNames),
"datatypes.proto");
} finally {
Expand All @@ -177,12 +181,29 @@ void run() throws IOException, InvalidFhirException {
}
}

private static FileDescriptorProto updateGoPackage(
ProtogenConfig config, FileDescriptorProto fileProto, String fileName) {
// Strip .proto suffix
String fileBaseName = fileName.substring(0, fileName.length() - 6);
String goPackage =
GeneratorUtils.GO_PACKAGE_PREFIX
+ "/"
+ config.getSourceDirectory()
+ "/"
+ fileBaseName
+ "_go_proto";
FileOptions options = fileProto.getOptions().toBuilder().setGoPackage(goPackage).build();
return fileProto.toBuilder().setOptions(options).build();
}

private static void addEntry(
ZipOutputStream zipOutputStream,
ProtoFilePrinter printer,
ProtogenConfig config,
FileDescriptorProto fileDescriptor,
String name)
throws IOException {
fileDescriptor = updateGoPackage(config, fileDescriptor, name);
zipOutputStream.putNextEntry(new ZipEntry(name));
byte[] entryBytes = printer.print(fileDescriptor).getBytes(UTF_8);
zipOutputStream.write(entryBytes, 0, entryBytes.length);
Expand Down
3 changes: 0 additions & 3 deletions java/com/google/fhir/protogen/ProtoGeneratorV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -1419,9 +1419,6 @@ private FileDescriptorProto.Builder generateFileDescriptor(List<StructureDefinit
if (!protogenConfig.getJavaProtoPackage().isEmpty()) {
options.setJavaPackage(protogenConfig.getJavaProtoPackage()).setJavaMultipleFiles(true);
}
if (!protogenConfig.getGoProtoPackage().isEmpty()) {
options.setGoPackage(protogenConfig.getGoProtoPackage());
}

// TODO(b/267772954292116008): Don't rely on precompiled version enum.
options.setExtension(Annotations.fhirVersion, protogenConfig.getFhirVersion());
Expand Down
6 changes: 0 additions & 6 deletions java/com/google/fhir/protogen/ValueSetGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ private FileDescriptorProto generateCodeSystemFile(Collection<CodeSystem> codeSy
if (!packageInfo.getJavaProtoPackage().isEmpty()) {
options.setJavaPackage(packageInfo.getJavaProtoPackage()).setJavaMultipleFiles(true);
}
if (!packageInfo.getGoProtoPackage().isEmpty()) {
options.setGoPackage(packageInfo.getGoProtoPackage());
}
builder.setOptions(options);
List<DescriptorProto> messages = new ArrayList<>();
for (CodeSystem codeSystem : codeSystemsToGenerate) {
Expand All @@ -195,9 +192,6 @@ private FileDescriptorProto generateValueSetFile(Collection<ValueSet> valueSetsT
if (!packageInfo.getJavaProtoPackage().isEmpty()) {
options.setJavaPackage(packageInfo.getJavaProtoPackage()).setJavaMultipleFiles(true);
}
if (!packageInfo.getGoProtoPackage().isEmpty()) {
options.setGoPackage(packageInfo.getGoProtoPackage());
}
builder.setOptions(options);
valueSetsToGenerate.stream()
.filter(vs -> !getOneToOneCodeSystem(vs).isPresent())
Expand Down
6 changes: 0 additions & 6 deletions java/com/google/fhir/protogen/ValueSetGeneratorV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ private FileDescriptorProto generateCodeSystemFile(Collection<CodeSystem> codeSy
if (!protogenConfig.getJavaProtoPackage().isEmpty()) {
options.setJavaPackage(protogenConfig.getJavaProtoPackage()).setJavaMultipleFiles(true);
}
if (!protogenConfig.getGoProtoPackage().isEmpty()) {
options.setGoPackage(protogenConfig.getGoProtoPackage());
}
builder.setOptions(options);
List<DescriptorProto> messages = new ArrayList<>();
for (CodeSystem codeSystem : codeSystemsToGenerate) {
Expand All @@ -128,9 +125,6 @@ private FileDescriptorProto generateValueSetFile(Collection<ValueSet> valueSetsT
if (!protogenConfig.getJavaProtoPackage().isEmpty()) {
options.setJavaPackage(protogenConfig.getJavaProtoPackage()).setJavaMultipleFiles(true);
}
if (!protogenConfig.getGoProtoPackage().isEmpty()) {
options.setGoPackage(protogenConfig.getGoProtoPackage());
}
builder.setOptions(options);
valueSetsToGenerate.stream()
.filter(vs -> !getOneToOneCodeSystem(vs).isPresent())
Expand Down
8 changes: 4 additions & 4 deletions proto/google/fhir/proto/profile_config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ option java_multiple_files = true;
// package) and therefore not contian information that can be found in the FHIR
// definitions themselves.
message ProtogenConfig {
reserved 3;

// Package for the generated protos for the resources.
string proto_package = 1;
// Java package for the generated protos for the resources.
string java_proto_package = 2;
// Go package for the generated protos for the resources.
string go_proto_package = 3;

// Directory to place generated package. This is needed for intra-package
// imports.
Expand All @@ -52,6 +52,8 @@ message ProtogenConfig {

// Metadata for generating FHIR packages
message PackageInfo {
reserved 5;

// FHIR version for the package (e.g., STU3, R4, R5, etc).
proto.FhirVersion fhir_version = 1;
// Base url to use for all profiles defined here.
Expand All @@ -62,8 +64,6 @@ message PackageInfo {
string proto_package = 3;
// Java package for the generated protos for the resources.
string java_proto_package = 4;
// Go package for the generated protos for the resources.
string go_proto_package = 5;

// Defines whether to generate a local ContainedResource proto or reuse one.
// If neither of these fields are set, use the core contained resource on
Expand Down
1 change: 0 additions & 1 deletion testdata/r4/profiles/split/package_info.prototxt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
base_url: "http://test/url/base"
proto_package: "google.fhir.r4.splittesting"
java_proto_package: "com.google.fhir.r4.splittesting"
go_proto_package: "google/fhir/r4/splittesting"
publisher: "Testington Inc."
fhir_version: R4
telcom_url: "http://www.testington.com"
Expand Down
1 change: 0 additions & 1 deletion testdata/r4/profiles/test_package_info.prototxt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ base_url: "http://test/url/base"
version: "123"
proto_package: "google.fhir.r4.testing"
java_proto_package: "com.google.fhir.r4.testing"
go_proto_package: "google/fhir/r4/testing"
publisher: "Testington Inc."
fhir_version: R4
telcom_url: "http://www.testington.com"
Expand Down
1 change: 0 additions & 1 deletion testdata/stu3/profiles/test_package_info.prototxt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
base_url: "http://test/url/base"
proto_package: "google.fhir.stu3.testing"
java_proto_package: "com.google.fhir.stu3.testing"
go_proto_package: "google/fhir/stu3/testing"
publisher: "Testington Inc."
fhir_version: STU3
telcom_url: "http://www.testington.com"
Expand Down

0 comments on commit 9ed52e5

Please sign in to comment.