Skip to content

Commit

Permalink
Generate Proto messages in stable, alphabetical ordering by message n…
Browse files Browse the repository at this point in the history
…ame.

For new profiles whose names don't name match existing resources, this can result in a change in ordering of resources in ContainedResources when generating protos.

PiperOrigin-RevId: 466443054
  • Loading branch information
kevineger authored and nickgeorge committed Aug 10, 2022
1 parent 37ef49f commit 31c3b61
Show file tree
Hide file tree
Showing 31 changed files with 8,668 additions and 8,699 deletions.
6 changes: 5 additions & 1 deletion java/com/google/fhir/protogen/ProtoFilePrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.fhir.protogen;


import com.google.common.base.Ascii;
import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
Expand All @@ -40,6 +41,7 @@
import com.google.protobuf.Descriptors.FieldDescriptor;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -126,7 +128,9 @@ public String print(FileDescriptorProto fileDescriptor) {
contents.append(printHeader(fileDescriptor)).append("\n");
contents.append(printImports(fileDescriptor)).append("\n");
contents.append(printOptions(fileDescriptor, fullyQualifiedPackageName)).append("\n");
for (DescriptorProto descriptor : fileDescriptor.getMessageTypeList()) {
for (DescriptorProto descriptor :
ImmutableList.sortedCopyOf(
Comparator.comparing(DescriptorProto::getName), fileDescriptor.getMessageTypeList())) {
contents
.append(printMessage(descriptor, fullyQualifiedPackageName, fullyQualifiedPackageName))
.append("\n");
Expand Down
7 changes: 3 additions & 4 deletions java/com/google/fhir/protogen/ProtoGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,8 @@ public FileDescriptorProto addContainedResource(
}
}
int tagNumber = Iterables.getLast(baseContainedResource.getFields()).getNumber() + 1;
for (String resourceType : resourcesToInclude) {

for (String resourceType : ImmutableList.sortedCopyOf(resourcesToInclude)) {
contained.addField(
FieldDescriptorProto.newBuilder()
.setName(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, resourceType))
Expand Down Expand Up @@ -2394,9 +2395,7 @@ private static void logDiscrepancies(String msg) {
private static StructureDefinition reconcileSnapshotAndDifferential(StructureDefinition def) {
// Generate a map from (element id) -> (element) for all elements in the Differential view.
Map<String, ElementDefinition> diffs =
def.getDifferential()
.getElementList()
.stream()
def.getDifferential().getElementList().stream()
.collect(
Collectors.toMap((element) -> element.getId().getValue(), Function.identity()));

Expand Down
2 changes: 2 additions & 0 deletions javatests/com/google/fhir/protogen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ java_test(
"//proto/google/fhir/proto/r4/core/resources:testdata",
"//spec:fhir_r4_filegroup",
"//spec:fhir_r4_package.zip",
"//spec:fhir_stu3_filegroup",
"//spec:fhir_stu3_package.zip",
"//testdata/r4:descriptors",
],
test_class = "com.google.fhir.protogen.ProtoGeneratorTest",
Expand Down
14 changes: 7 additions & 7 deletions javatests/com/google/fhir/protogen/ProtoGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ private String createNonCorePackage(String protoPackage) throws IOException {
}

@Test
public void addContainedResource_derivedResources_unsortedFileDescriptor()
public void addContainedResource_derivedResources_sorted()
throws IOException, InvalidFhirException {
ProtoGenerator generator =
ProtoGeneratorTestUtils.makeProtoGenerator(
Expand Down Expand Up @@ -562,27 +562,27 @@ public void addContainedResource_derivedResources_unsortedFileDescriptor()
.setOneofIndex(0))
.addField(
FieldDescriptorProto.newBuilder()
.setName("foo")
.setName("bar")
.setNumber(147)
.setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL)
.setType(FieldDescriptorProto.Type.TYPE_MESSAGE)
.setTypeName(".google.foo.Foo")
.setTypeName(".google.foo.Bar")
.setOneofIndex(0))
.addField(
FieldDescriptorProto.newBuilder()
.setName("bar")
.setName("baz")
.setNumber(148)
.setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL)
.setType(FieldDescriptorProto.Type.TYPE_MESSAGE)
.setTypeName(".google.foo.Bar")
.setTypeName(".google.foo.Baz")
.setOneofIndex(0))
.addField(
FieldDescriptorProto.newBuilder()
.setName("baz")
.setName("foo")
.setNumber(149)
.setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL)
.setType(FieldDescriptorProto.Type.TYPE_MESSAGE)
.setTypeName(".google.foo.Baz")
.setTypeName(".google.foo.Foo")
.setOneofIndex(0))
.addOneofDecl(OneofDescriptorProto.newBuilder().setName("oneof_resource")))
.build());
Expand Down
Loading

0 comments on commit 31c3b61

Please sign in to comment.