2929import com .google .fhir .proto .ProtoGeneratorAnnotations ;
3030import com .google .fhir .proto .ProtogenConfig ;
3131import com .google .protobuf .DescriptorProtos .DescriptorProto ;
32+ import com .google .protobuf .DescriptorProtos .Edition ;
3233import com .google .protobuf .DescriptorProtos .EnumDescriptorProto ;
3334import com .google .protobuf .DescriptorProtos .EnumOptions ;
3435import com .google .protobuf .DescriptorProtos .EnumValueDescriptorProto ;
5051/** A utility to turn protocol message descriptors into .proto files. */
5152public class ProtoFilePrinter {
5253
53- /** Enum represting the proto Syntax */
54- public static enum Syntax {
55- PROTO2 ,
56- PROTO3
57- };
58-
5954 private static final String APACHE_LICENSE =
6055 "// Copyright %1$s Google Inc.\n "
6156 + "//\n "
@@ -77,8 +72,6 @@ public static enum Syntax {
7772 private final PackageInfo .License license ;
7873 private final String licenseDate ;
7974
80- private final Syntax syntax ;
81-
8275 // All Message options in this list will appear in this order, before any options not in this
8376 // list. The remainder will be alphabetized.
8477 // This list exists largely to maintain backwards compatibility in ordering.
@@ -105,27 +98,19 @@ public static enum Syntax {
10598
10699 /** Creates a ProtoFilePrinter with default parameters. */
107100 public ProtoFilePrinter (PackageInfo packageInfo ) {
108- this (packageInfo , Syntax .PROTO3 );
109- }
110-
111- /** Creates a ProtoFilePrinter with default parameters. */
112- public ProtoFilePrinter (PackageInfo packageInfo , Syntax syntax ) {
113101 license = packageInfo .getLicense ();
114102 licenseDate = packageInfo .getLicenseDate ();
115- this .syntax = syntax ;
116103 }
117104
118105 /** Creates a ProtoFilePrinter with default parameters. */
119106 public ProtoFilePrinter (ProtogenConfig protogenConfig ) {
120107 license = PackageInfo .License .APACHE ;
121108 licenseDate = protogenConfig .getLicenseDate ();
122- this .syntax = Syntax .PROTO3 ;
123109 }
124110
125111 public ProtoFilePrinter (String licenseDate ) {
126112 license = PackageInfo .License .APACHE ;
127113 this .licenseDate = licenseDate ;
128- this .syntax = Syntax .PROTO3 ;
129114 }
130115
131116 /** Generate a .proto file corresponding to the provided FileDescriptorProto. */
@@ -151,7 +136,17 @@ public String print(FileDescriptorProto fileDescriptor) {
151136 private String printHeader (FileDescriptorProto fileDescriptor ) {
152137 StringBuilder header = new StringBuilder ();
153138 if (fileDescriptor .hasSyntax ()) {
154- header .append ("syntax = \" " ).append (fileDescriptor .getSyntax ()).append ("\" ;\n \n " );
139+ if (fileDescriptor .getSyntax ().equals ("editions" )) {
140+ if (fileDescriptor .getEdition ().equals (Edition .EDITION_2023 )) {
141+ header .append ("edition = \" 2023\" ;\n \n " );
142+ } else if (fileDescriptor .getEdition ().equals (Edition .EDITION_2024 )) {
143+ header .append ("edition = \" 2024\" ;\n \n " );
144+ } else {
145+ header .append ("edition = \" unknown\" ;\n \n " );
146+ }
147+ } else {
148+ header .append ("syntax = \" " ).append (fileDescriptor .getSyntax ()).append ("\" ;\n \n " );
149+ }
155150 }
156151 if (fileDescriptor .hasPackage ()) {
157152 header .append ("package " ).append (fileDescriptor .getPackage ()).append (";\n " );
@@ -170,6 +165,9 @@ private String printImports(FileDescriptorProto fileDescriptor) {
170165 private String printOptions (FileDescriptorProto fileDescriptor , String packageName ) {
171166 StringBuilder options = new StringBuilder ();
172167 FileOptions fileOptions = fileDescriptor .getOptions ();
168+ if (fileDescriptor .getSyntax ().equals ("editions" )) {
169+ options .append ("option features.field_presence = IMPLICIT;\n " );
170+ }
173171 if (fileOptions .hasJavaMultipleFiles ()) {
174172 options
175173 .append ("option java_multiple_files = " )
@@ -279,7 +277,7 @@ private String printMessage(DescriptorProto descriptor, String typePrefix, Strin
279277 descriptor , field , typePrefix , packageName , printedNestedTypeDefinitions ));
280278 message .append (
281279 printField (
282- fieldBuilder .build (), fullName , fieldIndent , packageName , /* inOneof= */ false ));
280+ fieldBuilder .build (), fullName , fieldIndent , packageName ));
283281 if (i != descriptor .getFieldCount () - 1 ) {
284282 message .append ("\n " );
285283 }
@@ -327,7 +325,7 @@ private String printMessage(DescriptorProto descriptor, String typePrefix, Strin
327325 }
328326 message .append (
329327 printField (
330- fieldBuilder .build (), fullName , oneofIndent , packageName , /* inOneof= */ true ));
328+ fieldBuilder .build (), fullName , oneofIndent , packageName ));
331329 // If this oneof field had a description, and is not the last field, add a newline after.
332330 if (field .getOptions ().hasExtension (ProtoGeneratorAnnotations .fieldDescription )
333331 && i != descriptor .getFieldCount () - 1 ) {
@@ -441,20 +439,14 @@ private String printField(
441439 FieldDescriptorProto field ,
442440 String containingType ,
443441 String indent ,
444- String packageName ,
445- boolean inOneof ) {
442+ String packageName ) {
446443 StringBuilder message = new StringBuilder ();
447444 message .append (indent );
448445
449- // Add the "repeated" or "optional" keywords , if necessary.
446+ // Add the "repeated" keyword , if necessary.
450447 if (field .getLabel () == FieldDescriptorProto .Label .LABEL_REPEATED ) {
451448 message .append ("repeated " );
452449 }
453- if (!inOneof
454- && field .getLabel () == FieldDescriptorProto .Label .LABEL_OPTIONAL
455- && syntax == Syntax .PROTO2 ) {
456- message .append ("optional " );
457- }
458450
459451 // Add the type of the field.
460452 if ((field .getType () == FieldDescriptorProto .Type .TYPE_MESSAGE
0 commit comments