Skip to content

Commit 4bf0cd0

Browse files
authored
Add an Option to Match JSON Encoding (#3)
* use json encoding * encode to binary by default * update the example * simplify * fix the test assets as well * refactor * update examples
1 parent e4c9bf3 commit 4bf0cd0

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

example/user_add_comment.proto

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ message UserAddComment {
99
message User {
1010
string first_name = 1;
1111
string last_name = 2;
12+
bytes avatar = 3;
1213

1314
message Location {
1415
double longitude = 1;
1516
double latitude = 2;
1617
}
1718

18-
Location location = 3;
19+
Location location = 4;
1920
}
2021

2122
User user = 1;
2223
string comment = 2;
23-
Label label = 3;
24+
example.Label label = 3;
25+
2426
google.protobuf.Timestamp timestamp = 101;
2527
}

example/user_add_comment.pubsub.proto

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,30 @@ message UserAddComment {
55
message User {
66
string first_name = 1;
77
string last_name = 2;
8+
bytes avatar = 3;
89

910
message Location {
1011
double longitude = 1;
1112
double latitude = 2;
1213
}
13-
Location location = 3;
1414

15+
Location location = 4;
1516
}
16-
User user = 1;
1717

18+
User user = 1;
1819
string comment = 2;
1920

2021
message Label {
2122
string key = 1;
2223
string value = 2;
2324
}
24-
Label label = 3;
2525

26+
Label label = 3;
2627

2728
message Timestamp {
2829
int64 seconds = 1;
2930
int32 nanos = 2;
3031
}
31-
Timestamp timestamp = 101;
3232

33+
Timestamp timestamp = 101;
3334
}

response_builder.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ func (b responseBuilder) buildField(output io.Writer, field *descriptorpb.FieldD
9191
fieldType := strings.ToLower(strings.TrimPrefix(field.GetType().String(), "TYPE_"))
9292

9393
if field.GetType() == descriptorpb.FieldDescriptorProto_TYPE_MESSAGE {
94-
fmt.Fprintln(output)
95-
defer fmt.Fprintln(output)
96-
b.buildMessage(output, b.messageTypes[field.GetTypeName()], level)
97-
fieldType = getShortTypeName(field.GetTypeName())
94+
fieldType = b.buildFieldType(output, field.GetTypeName(), level)
9895
}
9996

10097
fmt.Fprint(output, buildIndent(level))
@@ -104,10 +101,21 @@ func (b responseBuilder) buildField(output io.Writer, field *descriptorpb.FieldD
104101
fmt.Fprintf(output, "%s %s = %d;\n", fieldType, field.GetName(), field.GetNumber())
105102
}
106103

107-
func buildIndent(level int) string {
108-
return strings.Repeat(" ", level)
109-
}
104+
func (b responseBuilder) buildFieldType(output io.Writer, typeName string, level int) string {
105+
if typeName, ok := wktMapping[typeName]; ok && b.hasJSONEncoding() {
106+
return typeName
107+
}
110108

111-
func getShortTypeName(typeName string) string {
109+
fmt.Fprintln(output)
110+
b.buildMessage(output, b.messageTypes[typeName], level)
111+
fmt.Fprintln(output)
112112
return typeName[strings.LastIndexByte(typeName, '.')+1:]
113113
}
114+
115+
func (b responseBuilder) hasJSONEncoding() bool {
116+
return strings.Contains(b.request.GetParameter(), "encoding=json")
117+
}
118+
119+
func buildIndent(level int) string {
120+
return strings.Repeat(" ", level)
121+
}

test/user_add_comment.pubsub.protobuf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ message UserAddComment {
1111
double longitude = 1;
1212
double latitude = 2;
1313
}
14-
Location location = 3;
1514

15+
Location location = 3;
1616
}
17-
User user = 1;
1817

18+
User user = 1;
1919
string comment = 2;
2020

2121
message Label {
2222
string key = 1;
2323
string value = 2;
2424
}
25-
Label label = 3;
2625

26+
Label label = 3;
2727

2828
message Timestamp {
2929
int64 seconds = 1;
3030
int32 nanos = 2;
3131
}
32-
Timestamp timestamp = 101;
3332

33+
Timestamp timestamp = 101;
3434
}

wkt.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
var wktMapping = map[string]string{
4+
".google.protobuf.Int32Value": "int32",
5+
".google.protobuf.Int64Value": "int64",
6+
".google.protobuf.UInt32Value": "uint32",
7+
".google.protobuf.UInt64Value": "uint64",
8+
".google.protobuf.DoubleValue": "double",
9+
".google.protobuf.FloatValue": "float",
10+
".google.protobuf.BoolValue": "bool",
11+
".google.protobuf.StringValue": "string",
12+
".google.protobuf.BytesValue": "bytes",
13+
".google.protobuf.Duration": "string",
14+
".google.protobuf.Timestamp": "string",
15+
}

0 commit comments

Comments
 (0)