Skip to content

Commit

Permalink
Enhancement: in REPL mode allow user to specify path to file for byte…
Browse files Browse the repository at this point in the history
…s field
  • Loading branch information
BenSlabbert committed Apr 30, 2020
1 parent 2134fc2 commit e9e25bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ data (TYPE_BYTES) => \u65e5\u672c\u8a9e
}
```

Or specify the full path to a file using the `file://` prefix to the full path.
```
data (TYPE_BYTES) => file:///full/path/to/file.txt
```

### Client streaming RPC
Client streaming RPC accepts some requests and then returns only one response.
Finish request inputting with <kbd>CTRL-D</kbd>
Expand Down
9 changes: 9 additions & 0 deletions fill/proto/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package proto

import (
"fmt"
"io/ioutil"
"strconv"
"strings"

"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/pkg/errors"
Expand Down Expand Up @@ -80,6 +82,13 @@ func convertValue(pv string, fieldType descriptor.FieldDescriptorProto_Type) (in
// His expects "ogiso" in string, but backslashes in the input are not interpreted as an escape sequence.
// So, we need to call strconv.Unquote to interpret backslashes as an escape sequence.
case descriptor.FieldDescriptorProto_TYPE_BYTES:
const filePrefix = "file://"
if strings.HasPrefix(pv, filePrefix) {
pv = strings.TrimPrefix(pv, filePrefix)
v, err = ioutil.ReadFile(pv)
break
}

pv, err = strconv.Unquote(`"` + pv + `"`)
v = []byte(pv)

Expand Down
5 changes: 5 additions & 0 deletions fill/proto/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func Test_convertValue(t *testing.T) {
fieldType: descriptor.FieldDescriptorProto_TYPE_BYTES,
expected: []byte("小木曽"),
},
"bytes (read from file)": {
v: "file://proto.go",
fieldType: descriptor.FieldDescriptorProto_TYPE_BYTES,
expected: []byte("// Package proto provides a filler implementation for Protocol Buffers.\npackage proto\n"),
},
"bytes (Unicode literals)": {
v: "\u5c0f\u6728\u66fd",
fieldType: descriptor.FieldDescriptorProto_TYPE_BYTES,
Expand Down

0 comments on commit e9e25bb

Please sign in to comment.