Skip to content

Commit 2c3cb4c

Browse files
authored
Default Output to use proto2 syntax (#4)
* use proto2 syntax by default * pass field labels * update the example * fix test * fix test * update readme
1 parent 7187d92 commit 2c3cb4c

File tree

6 files changed

+75
-56
lines changed

6 files changed

+75
-56
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ To use this plugin, just run `protoc` with an option `--pubsub-schema_out`.
2323
`protoc` and `protoc-gen-pubsub-schema` must be found in shell's `$PATH`.
2424

2525
```sh
26-
# generate assembled proto files that accept binary encoding
26+
# generate assembled proto files with proto2 syntax that accept binary encoding
2727
protoc PROTO_FILES --pubsub-schema_out=OUT_DIR
2828

29-
# generate assembled proto files that accept JSON encoding
29+
# generate assembled proto files with proto2 syntax that accept JSON encoding
3030
protoc PROTO_FILES --pubsub-schema_out=OUT_DIR --pubsub-schema_opt=encoding=json
31+
32+
# generate assembled proto files with proto3 syntax that accept JSON encoding
33+
protoc PROTO_FILES --pubsub-schema_out=OUT_DIR --pubsub-schema_opt=syntax=proto3 --pubsub-schema_opt=encoding=json
3134
```
3235

3336
## Example

example/user_add_comment.proto

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
syntax = "proto3";
1+
syntax = "proto2";
22

33
package example;
44

@@ -7,21 +7,21 @@ import "google/protobuf/timestamp.proto";
77

88
message UserAddComment {
99
message User {
10-
string first_name = 1;
11-
string last_name = 2;
12-
bytes avatar = 3;
10+
required string first_name = 1;
11+
optional string last_name = 2;
12+
optional bytes avatar = 3;
1313

1414
message Location {
15-
double longitude = 1;
16-
double latitude = 2;
15+
required double longitude = 1;
16+
required double latitude = 2;
1717
}
1818

19-
Location location = 4;
19+
optional Location location = 4;
2020
}
2121

22-
User user = 1;
23-
string comment = 2;
24-
example.Label label = 3;
22+
required User user = 1;
23+
required string comment = 2;
24+
repeated example.Label labels = 3;
2525

26-
google.protobuf.Timestamp timestamp = 101;
26+
required google.protobuf.Timestamp timestamp = 101;
2727
}
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
syntax = "proto3";
1+
syntax = "proto2";
22

33
message UserAddComment {
44

55
message User {
6-
string first_name = 1;
7-
string last_name = 2;
8-
bytes avatar = 3;
6+
required string first_name = 1;
7+
optional string last_name = 2;
8+
optional bytes avatar = 3;
99

1010
message Location {
11-
double longitude = 1;
12-
double latitude = 2;
11+
required double longitude = 1;
12+
required double latitude = 2;
1313
}
1414

15-
Location location = 4;
15+
optional Location location = 4;
1616
}
1717

18-
User user = 1;
19-
string comment = 2;
18+
required User user = 1;
19+
required string comment = 2;
2020

2121
message Label {
22-
string key = 1;
23-
string value = 2;
22+
optional string key = 1;
23+
optional string value = 2;
2424
}
2525

26-
Label label = 3;
26+
repeated Label labels = 3;
2727

2828
message Timestamp {
29-
int64 seconds = 1;
30-
int32 nanos = 2;
29+
optional int64 seconds = 1;
30+
optional int32 nanos = 2;
3131
}
3232

33-
Timestamp timestamp = 101;
33+
required Timestamp timestamp = 101;
3434
}

response_builder.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,41 +74,56 @@ func (b responseBuilder) buildContent(protoFile *descriptorpb.FileDescriptorProt
7474
}
7575

7676
contentBuilder := new(strings.Builder)
77-
fmt.Fprint(contentBuilder, "syntax = \"proto3\";\n\n")
77+
b.buildSyntax(contentBuilder)
7878
b.buildMessage(contentBuilder, protoFile.GetMessageType()[0], 0)
7979
return contentBuilder.String(), nil
8080
}
8181

82-
func (b responseBuilder) buildMessage(output io.Writer, message *descriptorpb.DescriptorProto, level int) {
83-
fmt.Fprintf(output, "%smessage %s {\n", buildIndent(level), message.GetName())
82+
func (b responseBuilder) buildSyntax(output io.StringWriter) {
83+
output.WriteString(`syntax = "` + b.getOutputSyntax() + `";` + "\n\n")
84+
}
85+
86+
func (b responseBuilder) getOutputSyntax() string {
87+
if strings.Contains(b.request.GetParameter(), "syntax=proto3") {
88+
return "proto3"
89+
}
90+
return "proto2"
91+
}
92+
93+
func (b responseBuilder) buildMessage(output io.StringWriter, message *descriptorpb.DescriptorProto, level int) {
94+
output.WriteString(buildIndent(level) + "message " + message.GetName() + " {\n")
8495
for _, field := range message.GetField() {
8596
b.buildField(output, field, level+1)
8697
}
87-
fmt.Fprintf(output, "%s}\n", buildIndent(level))
98+
output.WriteString(buildIndent(level) + "}\n")
8899
}
89100

90-
func (b responseBuilder) buildField(output io.Writer, field *descriptorpb.FieldDescriptorProto, level int) {
101+
func (b responseBuilder) buildField(output io.StringWriter, field *descriptorpb.FieldDescriptorProto, level int) {
91102
fieldType := strings.ToLower(strings.TrimPrefix(field.GetType().String(), "TYPE_"))
92-
93103
if field.GetType() == descriptorpb.FieldDescriptorProto_TYPE_MESSAGE {
94104
fieldType = b.buildFieldType(output, field.GetTypeName(), level)
95105
}
106+
output.WriteString(buildIndent(level))
107+
b.buildFieldLabel(output, field.GetLabel())
108+
output.WriteString(fmt.Sprintf("%s %s = %d;\n", fieldType, field.GetName(), field.GetNumber()))
109+
}
96110

97-
fmt.Fprint(output, buildIndent(level))
98-
if field.GetLabel() == descriptorpb.FieldDescriptorProto_LABEL_REPEATED {
99-
fmt.Fprint(output, "repeated ")
111+
func (b responseBuilder) buildFieldLabel(output io.StringWriter, label descriptorpb.FieldDescriptorProto_Label) {
112+
if label == descriptorpb.FieldDescriptorProto_LABEL_REPEATED {
113+
output.WriteString("repeated ")
114+
} else if b.getOutputSyntax() == "proto2" {
115+
output.WriteString(strings.ToLower(strings.TrimPrefix(label.String(), "LABEL_")) + " ")
100116
}
101-
fmt.Fprintf(output, "%s %s = %d;\n", fieldType, field.GetName(), field.GetNumber())
102117
}
103118

104-
func (b responseBuilder) buildFieldType(output io.Writer, typeName string, level int) string {
119+
func (b responseBuilder) buildFieldType(output io.StringWriter, typeName string, level int) string {
105120
if typeName, ok := wktMapping[typeName]; ok && b.hasJSONEncoding() {
106121
return typeName
107122
}
108123

109-
fmt.Fprintln(output)
124+
output.WriteString("\n")
110125
b.buildMessage(output, b.messageTypes[typeName], level)
111-
fmt.Fprintln(output)
126+
output.WriteString("\n")
112127
return typeName[strings.LastIndexByte(typeName, '.')+1:]
113128
}
114129

test/user_add_comment.protobuf

237 Bytes
Binary file not shown.
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
z�
2-
%example/user_add_comment.pubsub.protoz�syntax = "proto3";
1+
z�
2+
%example/user_add_comment.pubsub.protoz�syntax = "proto2";
33

44
message UserAddComment {
55

66
message User {
7-
string first_name = 1;
8-
string last_name = 2;
7+
required string first_name = 1;
8+
optional string last_name = 2;
9+
optional bytes avatar = 3;
910

1011
message Location {
11-
double longitude = 1;
12-
double latitude = 2;
12+
required double longitude = 1;
13+
required double latitude = 2;
1314
}
1415

15-
Location location = 3;
16+
optional Location location = 4;
1617
}
1718

18-
User user = 1;
19-
string comment = 2;
19+
required User user = 1;
20+
required string comment = 2;
2021

2122
message Label {
22-
string key = 1;
23-
string value = 2;
23+
optional string key = 1;
24+
optional string value = 2;
2425
}
2526

26-
Label label = 3;
27+
repeated Label labels = 3;
2728

2829
message Timestamp {
29-
int64 seconds = 1;
30-
int32 nanos = 2;
30+
optional int64 seconds = 1;
31+
optional int32 nanos = 2;
3132
}
3233

33-
Timestamp timestamp = 101;
34+
required Timestamp timestamp = 101;
3435
}

0 commit comments

Comments
 (0)