-
-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement: in REPL mode allow user to specify path to file for bytes field #280
Changes from 3 commits
d6b5032
501b849
c122cc5
34ca795
039a445
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
usage: call <method name> | ||
|
||
Options: | ||
--dig-manually prompt asks whether to dig down if it encountered to a message field | ||
--enrich enrich response output includes header, message, trailer and status | ||
--bytes-from-file interpret TYPE_BYTES input as a relative path to a file | ||
--dig-manually prompt asks whether to dig down if it encountered to a message field | ||
--enrich enrich response output includes header, message, trailer and status | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ package proto | |
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/golang/protobuf/protoc-gen-go/descriptor" | ||
|
@@ -20,7 +22,8 @@ type InteractiveFiller struct { | |
prefixFormat string | ||
state promptInputterState | ||
|
||
digManually bool | ||
digManually bool | ||
bytesFromFile bool | ||
} | ||
|
||
// NewInteractiveFiller instantiates a new filler that fills each field interactively. | ||
|
@@ -35,8 +38,9 @@ func NewInteractiveFiller(prompt prompt.Prompt, prefixFormat string) *Interactiv | |
// Fill let you input each field interactively by using a prompt. v will be set field values inputted by a prompt. | ||
// | ||
// Note that Fill resets the previous state when it is called again. | ||
func (f *InteractiveFiller) Fill(v interface{}, digManually bool) error { | ||
f.digManually = digManually | ||
func (f *InteractiveFiller) Fill(v interface{}, opts fill.InteractiveFillerOpts) error { | ||
f.digManually = opts.DigManually | ||
f.bytesFromFile = opts.BytesFromFile | ||
|
||
msg, ok := v.(*dynamic.Message) | ||
if !ok { | ||
|
@@ -213,7 +217,7 @@ func (f *InteractiveFiller) inputField(dmsg *dynamic.Message, field *desc.FieldD | |
f.state.color.Next() | ||
default: // Normal fields. | ||
f.prompt.SetPrefix(f.makePrefix(field)) | ||
v, err := f.inputPrimitiveField(field) | ||
v, err := f.inputPrimitiveField(descriptor.FieldDescriptorProto_Type(descriptor.FieldDescriptorProto_Type_value[field.GetType().String()])) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -301,7 +305,7 @@ func (f *InteractiveFiller) inputRepeatedField(dmsg *dynamic.Message, field *des | |
|
||
// inputPrimitiveField reads an input and converts it to a Go type. | ||
// If CTRL+d is entered, inputPrimitiveField returns io.EOF. | ||
func (f *InteractiveFiller) inputPrimitiveField(field *desc.FieldDescriptor) (interface{}, error) { | ||
func (f *InteractiveFiller) inputPrimitiveField(fieldType descriptor.FieldDescriptorProto_Type) (interface{}, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
in, err := f.prompt.Input() | ||
if errors.Is(err, io.EOF) { | ||
return "", io.EOF | ||
|
@@ -310,7 +314,28 @@ func (f *InteractiveFiller) inputPrimitiveField(field *desc.FieldDescriptor) (in | |
return "", errors.Wrap(err, "failed to read user input") | ||
} | ||
|
||
return convertValue(in, descriptor.FieldDescriptorProto_Type(descriptor.FieldDescriptorProto_Type_value[field.GetType().String()])) | ||
v, err := convertValue(in, fieldType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if fieldType == descriptor.FieldDescriptorProto_TYPE_BYTES { | ||
return f.processBytesInput(v) | ||
} | ||
|
||
return v, err | ||
} | ||
|
||
func (f *InteractiveFiller) processBytesInput(v interface{}) (interface{}, error) { | ||
if _, ok := v.([]byte); !ok { | ||
return nil, errors.New("value is not of type bytes") | ||
} | ||
|
||
if f.bytesFromFile { | ||
return readFileFromRelativePath(string(v.([]byte))) | ||
} | ||
|
||
return v, nil | ||
} | ||
|
||
func (f *InteractiveFiller) isSelectedOneOf(field *desc.FieldDescriptor) bool { | ||
|
@@ -438,3 +463,16 @@ func makePrefix(s string, field *desc.FieldDescriptor, ancestor []string, ancest | |
func isOneOfField(field *desc.FieldDescriptor) bool { | ||
return field.GetOneOf() != nil | ||
} | ||
|
||
func readFileFromRelativePath(path string) ([]byte, error) { | ||
if path == "" { | ||
return nil, nil | ||
} | ||
|
||
absPath, err := filepath.Abs(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These lines are unnecessary because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. didn't know that :) |
||
|
||
return ioutil.ReadFile(absPath) | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,16 +1,87 @@ | ||||||||||||||||||
package proto_test | ||||||||||||||||||
package proto | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dropped the |
||||||||||||||||||
|
||||||||||||||||||
import ( | ||||||||||||||||||
"testing" | ||||||||||||||||||
|
||||||||||||||||||
"github.com/golang/protobuf/protoc-gen-go/descriptor" | ||||||||||||||||||
"github.com/ktr0731/evans/fill" | ||||||||||||||||||
"github.com/ktr0731/evans/fill/proto" | ||||||||||||||||||
"github.com/ktr0731/evans/prompt" | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
type testPrompt struct { | ||||||||||||||||||
input string | ||||||||||||||||||
} | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you embed
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great suggestion thank you! |
||||||||||||||||||
|
||||||||||||||||||
func (t *testPrompt) Input() (string, error) { | ||||||||||||||||||
return t.input, nil | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
func (t *testPrompt) Select(message string, options []string) (selected string, _ error) { | ||||||||||||||||||
panic("implement me") | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
func (t *testPrompt) SetPrefix(prefix string) { | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
func (t *testPrompt) SetPrefixColor(color prompt.Color) { | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
func (t *testPrompt) SetCompleter(c prompt.Completer) { | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
func (t *testPrompt) GetCommandHistory() []string { | ||||||||||||||||||
return []string{} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
func TestInteractiveProtoFiller(t *testing.T) { | ||||||||||||||||||
f := proto.NewInteractiveFiller(nil, "") | ||||||||||||||||||
err := f.Fill("invalid type", false) | ||||||||||||||||||
f := NewInteractiveFiller(nil, "") | ||||||||||||||||||
err := f.Fill("invalid type", fill.InteractiveFillerOpts{}) | ||||||||||||||||||
if err != fill.ErrCodecMismatch { | ||||||||||||||||||
t.Errorf("must return fill.ErrCodecMismatch because the arg is invalid type, but got: %s", err) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
tp := &testPrompt{ | ||||||||||||||||||
input: "../../go.mod", | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
f = NewInteractiveFiller(tp, "") | ||||||||||||||||||
f.bytesFromFile = true | ||||||||||||||||||
|
||||||||||||||||||
var v interface{} | ||||||||||||||||||
v, err = f.inputPrimitiveField(descriptor.FieldDescriptorProto_TYPE_BYTES) | ||||||||||||||||||
if err != nil { | ||||||||||||||||||
t.Error(err) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
if _, ok := v.([]byte); !ok { | ||||||||||||||||||
t.Errorf("value should be of type []byte") | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
fileContent, err := readFileFromRelativePath(tp.input) | ||||||||||||||||||
if err != nil { | ||||||||||||||||||
t.Error(err) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
if len(v.([]byte)) != len(fileContent) { | ||||||||||||||||||
t.Error("contents should have the same length") | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
tp = &testPrompt{ | ||||||||||||||||||
input: "\\x6f\\x67\\x69\\x73\\x6f", | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
f = NewInteractiveFiller(tp, "") | ||||||||||||||||||
|
||||||||||||||||||
v, err = f.inputPrimitiveField(descriptor.FieldDescriptorProto_TYPE_BYTES) | ||||||||||||||||||
if err != nil { | ||||||||||||||||||
t.Error(err) | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
if _, ok := v.([]byte); !ok { | ||||||||||||||||||
t.Errorf("value should be of type []byte") | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
if string(v.([]byte)) != "ogiso" { | ||||||||||||||||||
t.Error("unequal content") | ||||||||||||||||||
} | ||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.