Skip to content

Commit

Permalink
skip the rest of fields if CTRL-C is entered (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktr0731 authored Jun 9, 2019
1 parent f21a3ef commit 6551092
Show file tree
Hide file tree
Showing 22 changed files with 291 additions and 166 deletions.
52 changes: 50 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ So, you can format it by any commands like `jq`. Also, if you want to use the sa
- [Installation](#installation)
- [From GitHub Releases](#from-github-releases)
- [macOS](#macos)
- [[Deprecated] go get](#deprecated-go-get)
- [[Not-recommended] go get](#not-recommended-go-get)
- [Usage (REPL)](#usage-repl)
- [Basic usage](#basic-usage)
- [Repeated fields](#repeated-fields)
Expand All @@ -46,6 +46,7 @@ So, you can format it by any commands like `jq`. Also, if you want to use the sa
- [Client streaming RPC](#client-streaming-rpc)
- [Server streaming RPC](#server-streaming-rpc)
- [Bidirectional streaming RPC](#bidirectional-streaming-rpc)
- [Skip the rest of fields](#skip-the-rest-of-fields)
- [Usage (CLI)](#usage-cli)
- [Basic usage](#basic-usage-1)
- [Repeated fields](#repeated-fields-1)
Expand Down Expand Up @@ -76,7 +77,7 @@ $ brew tap ktr0731/evans
$ brew install evans
```

### **[Deprecated]** go get
### **[Not-recommended]** go get
Go v1.12 (with mod-aware mode) or later required.
`go get` installation is not supported officially.
``` sh
Expand Down Expand Up @@ -314,6 +315,53 @@ name (TYPE_STRING) => bar
name (TYPE_STRING) =>
```

### Skip the rest of fields
Evans recognizes <kbd>CTRL-C</kbd> as a special key that skips the rest of fields in the current message type.
For example, we assume that we are inputting `Request` described in the following message:

``` proto
message FullName {
string first_name = 1;
string last_name = 2;
}
message Request {
string nickname = 1;
FullName full_name = 2;
}
```

If we enter <kbd>CTRL-C</kbd> at the following moment, `full_name` field will be skipped.

```
nickname (TYPE_STRING) =>
```

The actual request value is just like this.

``` json
{}
```

If we enter <kbd>CTRL-C</kbd> at the following moment, `last_name` field will be skipped.

```
nickname (TYPE_STRING) => myamori
full_name::first_name (TYPE_STRING) => aoi
full_name::last_name (TYPE_STRING) =>
```

The actual request value is just like this.

``` json
{
"nickname": "myamori",
"fullName": {
"firstName": "aoi"
}
}
```

## Usage (CLI)
### Basic usage
You can input requests from `stdin` or files.
Expand Down
8 changes: 4 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ type App struct {
cfg *mergedConfig
}

// New instantiates a new App instance. If cui is nil, the default UI will be used.
// New instantiates a new App instance. ui must not be a nil.
// Note that cui is also used for the REPL UI if the mode is REPL mode.
func New(cui cui.UI) *App {
func New(ui cui.UI) *App {
return &App{
cui: cui,
cui: ui,
}
}

Expand Down Expand Up @@ -120,7 +120,7 @@ func (a *App) run(args []string) error {
return errors.Wrap(err, "failed to update Evans")
}

if err := mode.RunAsREPLMode(a.cfg.Config, a.cui); err != nil {
if err := mode.RunAsREPLMode(a.cfg.Config, a.cui, cache); err != nil {
return errors.Wrap(err, "failed to run REPL mode")
}

Expand Down
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ coverage:
patch: no
project:
default:
target: 50
target: 80

parsers:
gcov:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

[meta]
autoupdate = false
configversion = "0.7.3"
configversion = "0.8.0"
updatelevel = "patch"

[repl]
Expand Down
9 changes: 5 additions & 4 deletions e2e/e2egen/e2egen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (
"strconv"
"strings"

goprompt "github.com/c-bata/go-prompt"
"github.com/ktr0731/evans/app"
"github.com/ktr0731/evans/cui"
"github.com/ktr0731/evans/prompt"
goprompt "github.com/ktr0731/go-prompt"
"github.com/pkg/errors"
"golang.org/x/tools/go/ast/astutil"
)
Expand All @@ -54,16 +55,16 @@ func main() {
defer f.Close()

p := &recorderPrompt{Prompt: prompt.New()}
prompt.New = func() prompt.Prompt {
prompt.New = func(...prompt.Option) prompt.Prompt {
return p
}
code := app.New(nil).Run(args)
code := app.New(cui.New()).Run(args)

if code != 0 {
os.Exit(code)
}

testCaseName := goprompt.Input("testcase name: ", func(goprompt.Document) []goprompt.Suggest { return nil })
testCaseName, _ := goprompt.Input("testcase name: ", func(goprompt.Document) []goprompt.Suggest { return nil })
if testCaseName == "" {
fmt.Println("abort")
if _, err := f.Write(src); err != nil {
Expand Down
82 changes: 37 additions & 45 deletions e2e/e2egen/mock_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 23 additions & 6 deletions e2e/repl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ func TestE2E_REPL(t *testing.T) {
args: "testdata/test.proto",
input: []interface{}{"call UnaryRepeated", "miyuki", "kaguya", "chika", "yu", io.EOF},
},
"call UnaryRepeated which ends with two empty input": {
args: "testdata/test.proto",
input: []interface{}{"call UnaryRepeated", "miyuki", "kaguya", "", ""},
},
"call UnarySelf": {
args: "testdata/test.proto",
input: []interface{}{"call UnarySelf", "dig down", "ohana", "matsumae", "ohana", "dig down", "nako", "oshimizu", "nakochi", "finish", "dig down", "minko", "tsurugi", "minchi", "finish", "finish"},
Expand Down Expand Up @@ -248,6 +244,25 @@ func TestE2E_REPL(t *testing.T) {
input: []interface{}{"quit"},
skipGolden: true,
},

// special keys.

"ctrl-c skips the rest of fields if there are no message type fields": {
args: "testdata/test.proto",
input: []interface{}{"call Unary", prompt.ErrAbort},
},
"ctrl-c skips the rest of the current message": {
args: "testdata/test.proto",
input: []interface{}{"call UnaryMessage", "mumei", prompt.ErrAbort},
},
"ctrl-c skips the rest of the current message and exits the repeated field": {
args: "testdata/test.proto",
input: []interface{}{"call UnaryRepeatedMessage", "kanade", "hisaishi", "kumiko", prompt.ErrAbort},
},
"ctrl-c is also enabled in streaming RPCs": {
args: "testdata/test.proto",
input: []interface{}{"call BidiStreaming", "kanade", "ririka", prompt.ErrAbort, io.EOF},
},
}
oldNewPrompt := prompt.New
defer func() {
Expand All @@ -261,10 +276,11 @@ func TestE2E_REPL(t *testing.T) {
defer stopServer()

stubPrompt := &stubPrompt{
t: t,
Prompt: oldNewPrompt(),
input: append(c.input, "exit"),
}
prompt.New = func() prompt.Prompt {
prompt.New = func(...prompt.Option) prompt.Prompt {
return stubPrompt
}

Expand Down Expand Up @@ -301,14 +317,15 @@ func TestE2E_REPL(t *testing.T) {
}

type stubPrompt struct {
t *testing.T
prompt.Prompt

input []interface{}
}

func (p *stubPrompt) Input() (string, error) {
if len(p.input) == 0 {
panic("p.input is empty, but testing is continued yet. Are you forgot to use io.EOF for finishing inputting?")
p.t.Fatal("p.input is empty, but testing is continued yet. Are you forgot to use io.EOF for finishing inputting?")
}
s := p.input[0]
p.input = p.input[1:]
Expand Down

This file was deleted.

Loading

0 comments on commit 6551092

Please sign in to comment.