Skip to content

Commit ad18940

Browse files
authored
Accept bytes as base64-encoded string by default (#631)
* Decode bytes as base64 by default Fall back to bytes-as-quoted-literals mode if base64 decoding fails, and log a warning. * e2e: extend testcases Test various cases, entering base64 or quoted literals without explicitly specifying --bytes-as-base64 or --bytes-as-quoted-literals. * fill/proto/interactive_filler_test: fix This test actually didn't succeed properly, but we didn't notice, as the error wasn't propagated properly.
1 parent 2799931 commit ad18940

15 files changed

+157
-56
lines changed

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,27 +263,41 @@ When <kbd>CTRL-D</kbd> is entered, inputting will be aborted.
263263
```
264264

265265
### Bytes type fields
266-
You can use byte literal and Unicode literal.
266+
You can pass bytes as a base64-encoded string.
267267

268268
```
269269
> call UnaryBytes
270+
data (TYPE_BYTES) => SGVsbG8gV29ybGQh
271+
{
272+
"message": "received: (bytes) 48 65 6c 6c 6f 20 57 6f 72 6c 64 21, (string) Hello World!"
273+
}
274+
```
275+
276+
⚠️ Warning: Previously, bytes were passed as a (quoted) byte literal or Unicode
277+
literal string.
278+
279+
While evans currenty still attempts to fall back to that encoding if decoding
280+
as base64 fails, it's discouraged, and might be dropped.
281+
282+
Please either encode bytes as base64, or pass `--bytes-as-quoted-literals`
283+
explicitly:
284+
285+
```
286+
> call UnaryBytes --bytes-as-quoted-literals
270287
data (TYPE_BYTES) => \x46\x6f\x6f
271288
{
272289
"message": "received: (bytes) 46 6f 6f, (string) Foo"
273290
}
274291
275-
> call UnaryBytes
292+
> call UnaryBytes --bytes-as-quoted-literals
276293
data (TYPE_BYTES) => \u65e5\u672c\u8a9e
277294
{
278295
"message": "received: (bytes) e6 97 a5 e6 9c ac e8 aa 9e, (string) 日本語"
279296
}
280297
```
281298

282-
Or add the flag `--bytes-as-base64` to pass bytes as a base64-encoded string
283-
```
284-
> call UnaryBytes --bytes-as-base64
285-
data (TYPE_BYTES) => SGVsbG8gV29ybGQh
286-
```
299+
You can also add the flag `--bytes-as-base64` to explicitly disable the
300+
fallback behaviour.
287301

288302
Or add the flag `--bytes-from-file` to read bytes from the provided relative path
289303
```

e2e/old_repl_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,21 @@ func TestE2E_OldREPL(t *testing.T) {
121121
input: []interface{}{"call UnaryEnum", 0},
122122
},
123123
"call UnaryBytes": {
124+
args: "testdata/test.proto",
125+
input: []interface{}{"call UnaryBytes", "44KE44Gv44KK5L+644Gu6Z2S5pil44Op44OW44Kz44Oh44Gv44G+44Gh44GM44Gj44Gm44GE44KL44CC"},
126+
},
127+
"call UnaryBytes (fallback)": {
124128
args: "testdata/test.proto",
125129
input: []interface{}{"call UnaryBytes", "\\u3084\\u306f\\u308a\\u4ffa\\u306e\\u9752\\u6625\\u30e9\\u30d6\\u30b3\\u30e1\\u306f\\u307e\\u3061\\u304c\\u3063\\u3066\\u3044\\u308b\\u3002"},
126130
},
131+
"call UnaryBytes --bytes-as-base64": {
132+
args: "testdata/test.proto",
133+
input: []interface{}{"call UnaryBytes --bytes-as-base64", "44KE44Gv44KK5L+644Gu6Z2S5pil44Op44OW44Kz44Oh44Gv44G+44Gh44GM44Gj44Gm44GE44KL44CC"},
134+
},
135+
"call UnaryBytes --bytes-as-quoted-literals": {
136+
args: "testdata/test.proto",
137+
input: []interface{}{"call UnaryBytes --bytes-as-quoted-literals", "\\u3084\\u306f\\u308a\\u4ffa\\u306e\\u9752\\u6625\\u30e9\\u30d6\\u30b3\\u30e1\\u306f\\u307e\\u3061\\u304c\\u3063\\u3066\\u3044\\u308b\\u3002"},
138+
},
127139
"call UnaryRepeatedEnum": {
128140
args: "testdata/test.proto",
129141
input: []interface{}{"call UnaryRepeatedEnum", 0, 0, 1, io.EOF},

e2e/repl_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,21 @@ func TestE2E_REPL(t *testing.T) {
128128
input: []interface{}{"call UnaryEnum", 0},
129129
},
130130
"call UnaryBytes": {
131+
commonFlags: "--proto testdata/test.proto",
132+
input: []interface{}{"call UnaryBytes", "44KE44Gv44KK5L+644Gu6Z2S5pil44Op44OW44Kz44Oh44Gv44G+44Gh44GM44Gj44Gm44GE44KL44CC"},
133+
},
134+
"call UnaryBytes (fallback)": {
131135
commonFlags: "--proto testdata/test.proto",
132136
input: []interface{}{"call UnaryBytes", "\\u3084\\u306f\\u308a\\u4ffa\\u306e\\u9752\\u6625\\u30e9\\u30d6\\u30b3\\u30e1\\u306f\\u307e\\u3061\\u304c\\u3063\\u3066\\u3044\\u308b\\u3002"},
133137
},
138+
"call UnaryBytes --bytes-as-base64": {
139+
commonFlags: "--proto testdata/test.proto",
140+
input: []interface{}{"call UnaryBytes --bytes-as-base64", "44KE44Gv44KK5L+644Gu6Z2S5pil44Op44OW44Kz44Oh44Gv44G+44Gh44GM44Gj44Gm44GE44KL44CC"},
141+
},
142+
"call UnaryBytes --bytes-as-quoted-literals": {
143+
commonFlags: "--proto testdata/test.proto",
144+
input: []interface{}{"call UnaryBytes --bytes-as-quoted-literals", "\\u3084\\u306f\\u308a\\u4ffa\\u306e\\u9752\\u6625\\u30e9\\u30d6\\u30b3\\u30e1\\u306f\\u307e\\u3061\\u304c\\u3063\\u3066\\u3044\\u308b\\u3002"},
145+
},
134146
"call UnaryRepeatedEnum": {
135147
commonFlags: "--proto testdata/test.proto",
136148
input: []interface{}{"call UnaryRepeatedEnum", 0, 0, 1, io.EOF},
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message": "received: (bytes) e3 82 84 e3 81 af e3 82 8a e4 bf ba e3 81 ae e9 9d 92 e6 98 a5 e3 83 a9 e3 83 96 e3 82 b3 e3 83 a1 e3 81 af e3 81 be e3 81 a1 e3 81 8c e3 81 a3 e3 81 a6 e3 81 84 e3 82 8b e3 80 82, (string) やはり俺の青春ラブコメはまちがっている。"
3+
}
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message": "received: (bytes) e3 82 84 e3 81 af e3 82 8a e4 bf ba e3 81 ae e9 9d 92 e6 98 a5 e3 83 a9 e3 83 96 e3 82 b3 e3 83 a1 e3 81 af e3 81 be e3 81 a1 e3 81 8c e3 81 a3 e3 81 a6 e3 81 84 e3 82 8b e3 80 82, (string) やはり俺の青春ラブコメはまちがっている。"
3+
}
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message": "received: (bytes) e3 82 84 e3 81 af e3 82 8a e4 bf ba e3 81 ae e9 9d 92 e6 98 a5 e3 83 a9 e3 83 96 e3 82 b3 e3 83 a1 e3 81 af e3 81 be e3 81 a1 e3 81 8c e3 81 a3 e3 81 a6 e3 81 84 e3 82 8b e3 80 82, (string) やはり俺の青春ラブコメはまちがっている。"
3+
}
4+
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
usage: call <method name>
22

33
Options:
4-
--add-repeated-manually prompt asks whether to add a value if it encountered to a repeated field
5-
--bytes-as-base64 interpret TYPE_BYTES input as base64-encoded string (mutually exclusive with --bytes-from-file)
6-
--bytes-from-file interpret TYPE_BYTES input as a relative path to a file (mutually exclusive with --bytes-as-base64)
7-
--dig-manually prompt asks whether to dig down if it encountered to a message field
8-
--emit-defaults render fields with default values
9-
--enrich enrich response output includes header, message, trailer and status
10-
-r, --repeat repeat previous unary or server streaming request (if exists)
4+
--add-repeated-manually prompt asks whether to add a value if it encountered to a repeated field
5+
--bytes-as-base64 explicitly interpret TYPE_BYTES input as base64-encoded string (mutually exclusive with --bytes-from-file and --bytes-as-quoted-literals)
6+
--bytes-as-quoted-literals interpret TYPE_BYTES input as a string of (quoted) byte literal or Unicode (mutually exclusive with --bytes-from-file and --bytes-as-base64)
7+
--bytes-from-file interpret TYPE_BYTES input as a relative path to a file (mutually exclusive with --bytes-as-base64)
8+
--dig-manually prompt asks whether to dig down if it encountered to a message field
9+
--emit-defaults render fields with default values
10+
--enrich enrich response output includes header, message, trailer and status
11+
-r, --repeat repeat previous unary or server streaming request (if exists)
1112

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message": "received: (bytes) e3 82 84 e3 81 af e3 82 8a e4 bf ba e3 81 ae e9 9d 92 e6 98 a5 e3 83 a9 e3 83 96 e3 82 b3 e3 83 a1 e3 81 af e3 81 be e3 81 a1 e3 81 8c e3 81 a3 e3 81 a6 e3 81 84 e3 82 8b e3 80 82, (string) やはり俺の青春ラブコメはまちがっている。"
3+
}
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message": "received: (bytes) e3 82 84 e3 81 af e3 82 8a e4 bf ba e3 81 ae e9 9d 92 e6 98 a5 e3 83 a9 e3 83 96 e3 82 b3 e3 83 a1 e3 81 af e3 81 be e3 81 a1 e3 81 8c e3 81 a3 e3 81 a6 e3 81 84 e3 82 8b e3 80 82, (string) やはり俺の青春ラブコメはまちがっている。"
3+
}
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"message": "received: (bytes) e3 82 84 e3 81 af e3 82 8a e4 bf ba e3 81 ae e9 9d 92 e6 98 a5 e3 83 a9 e3 83 96 e3 82 b3 e3 83 a1 e3 81 af e3 81 be e3 81 a1 e3 81 8c e3 81 a3 e3 81 a6 e3 81 84 e3 82 8b e3 80 82, (string) やはり俺の青春ラブコメはまちがっている。"
3+
}
4+

0 commit comments

Comments
 (0)