Skip to content

Commit

Permalink
add --emit-defaults option (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktr0731 authored Apr 22, 2021
1 parent c798fa9 commit fb471a3
Show file tree
Hide file tree
Showing 34 changed files with 128 additions and 78 deletions.
14 changes: 11 additions & 3 deletions app/cli_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (

func newCLICallCommand(flags *flags, ui cui.UI) *cobra.Command {
var (
out string
enrich bool
out string
enrich bool
emitDefaults bool
)
cmd := &cobra.Command{
Use: "call [options ...] <method>",
Expand All @@ -34,7 +35,13 @@ func newCLICallCommand(flags *flags, ui cui.UI) *cobra.Command {
if len(args) == 0 {
return errors.New("method is required")
}
invoker, err := mode.NewCallCLIInvoker(ui, args[0], cfg.file, cfg.Config.Request.Header, enrich, out)
invoker, err := mode.NewCallCLIInvoker(ui, args[0], &mode.CallCLIInvokerOption{
Headers: cfg.Config.Request.Header,
Enrich: enrich,
EmitDefaults: emitDefaults,
FilePath: cfg.file,
FormatType: out,
})
if err != nil {
return err
}
Expand All @@ -50,6 +57,7 @@ func newCLICallCommand(flags *flags, ui cui.UI) *cobra.Command {
f := cmd.Flags()
initFlagSet(f, ui.Writer())
f.BoolVar(&enrich, "enrich", false, `enrich response output includes header, message, trailer and status`)
f.BoolVar(&emitDefaults, "emit-defaults", false, `render fields with default values`)
f.StringVarP(&out, "output", "o", "curl", `output format. one of "json" or "curl". "curl" is a curl-like format.`)

cmd.SetHelpFunc(usageFunc(ui.Writer(), []string{"file"}))
Expand Down
10 changes: 8 additions & 2 deletions app/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ func newOldCommand(flags *flags, ui cui.UI) *command {
if cfg.repl || !isCLIMode {
return runREPLCommand(cfg, ui)
}
invoker, err := mode.NewCallCLIInvoker(ui, cfg.call, cfg.file, cfg.Config.Request.Header, false, "")
invoker, err := mode.NewCallCLIInvoker(ui, cfg.call, &mode.CallCLIInvokerOption{
Headers: cfg.Config.Request.Header,
FilePath: cfg.file,
})
if err != nil {
return err
}
Expand Down Expand Up @@ -207,7 +210,10 @@ func newCLICommand(flags *flags, ui cui.UI) *cobra.Command {
}
call = args[0]
}
invoker, err := mode.NewCallCLIInvoker(ui, call, cfg.file, cfg.Config.Request.Header, false, "")
invoker, err := mode.NewCallCLIInvoker(ui, call, &mode.CallCLIInvokerOption{
Headers: cfg.Config.Request.Header,
FilePath: cfg.file,
})
if err != nil {
return err
}
Expand Down
38 changes: 22 additions & 16 deletions e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,39 +161,39 @@ func TestE2E_CLI(t *testing.T) {
cmd: "call",
args: "--file testdata/unary_call.in Unary",
deprecatedUsage: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC with an input file": {
commonFlags: "--proto testdata/test.proto",
cmd: "call",
args: "--file testdata/unary_call.in api.Example.Unary",
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call fully-qualified unary RPC with an input file": {
commonFlags: "--proto testdata/test.proto",
cmd: "call",
args: "--file testdata/unary_call.in api.Example.Unary",
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC with --call flag (backward-compatibility)": {
commonFlags: "--package api --service Example --proto testdata/test.proto",
cmd: "",
args: "--file testdata/unary_call.in --call Unary",
deprecatedUsage: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC without package name because the size of packages is 1 (backward-compatibility)": {
commonFlags: "--service Example --proto testdata/test.proto",
cmd: "call",
args: "--file testdata/unary_call.in Unary",
deprecatedUsage: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC without package and service name because the size of packages and services are 1": {
commonFlags: "--proto testdata/test.proto",
cmd: "call",
args: "--file testdata/unary_call.in Unary",
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC with an input reader": {
commonFlags: "--proto testdata/test.proto",
Expand All @@ -206,7 +206,13 @@ func TestE2E_CLI(t *testing.T) {
mode.DefaultCLIReader = old
}
},
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC with --emit-defaults": {
commonFlags: "--proto testdata/test.proto",
cmd: "call",
args: "--emit-defaults --file testdata/unary_empty_call.in api.Example.Unary",
expectedOut: `{ "message": "" }`,
},
"call client streaming RPC": {
commonFlags: "--proto testdata/test.proto",
Expand Down Expand Up @@ -271,7 +277,7 @@ func TestE2E_CLI(t *testing.T) {
commonFlags: "--header grpc-timeout=1s --proto testdata/test.proto",
cmd: "call",
args: "--file testdata/unary_call.in api.Example.Unary",
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"client streaming call timed out": {
commonFlags: "--header grpc-timeout=0s --proto testdata/test.proto",
Expand Down Expand Up @@ -344,14 +350,14 @@ func TestE2E_CLI(t *testing.T) {
args: "--file testdata/unary_call.in Unary",
reflection: true,
deprecatedUsage: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC with reflection with an input file": {
commonFlags: "--reflection",
cmd: "call",
args: "--file testdata/unary_call.in api.Example.Unary",
reflection: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},

// call command with TLS
Expand Down Expand Up @@ -389,7 +395,7 @@ func TestE2E_CLI(t *testing.T) {
cmd: "call",
args: "--file testdata/unary_call.in api.Example.Unary",
tls: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"cannot launch with TLS and reflection because server didn't enable TLS": {
commonFlags: "--tls -r --host localhost --cacert testdata/rootCA.pem",
Expand All @@ -405,14 +411,14 @@ func TestE2E_CLI(t *testing.T) {
args: "--file testdata/unary_call.in api.Example.Unary",
tls: true,
reflection: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC with TLS and --servername": {
commonFlags: "--tls --servername localhost --cacert testdata/rootCA.pem --proto testdata/test.proto",
cmd: "call",
args: "--file testdata/unary_call.in api.Example.Unary",
tls: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"cannot launch with mutual TLS auth because --certkey is missing": {
commonFlags: "--tls --host localhost --cacert testdata/rootCA.pem --cert testdata/localhost.pem --proto testdata/test.proto",
Expand All @@ -433,7 +439,7 @@ func TestE2E_CLI(t *testing.T) {
cmd: "call",
args: "--file testdata/unary_call.in api.Example.Unary",
tls: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},

// call command with gRPC-Web
Expand All @@ -450,15 +456,15 @@ func TestE2E_CLI(t *testing.T) {
cmd: "call",
args: "--file testdata/unary_call.in api.Example.Unary",
web: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC with an input file and reflection against to gRPC-Web server": {
commonFlags: "--web -r",
cmd: "call",
args: "--file testdata/unary_call.in api.Example.Unary",
web: true,
reflection: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call client streaming RPC against to gRPC-Web server": {
commonFlags: "--web --proto testdata/test.proto",
Expand Down
22 changes: 11 additions & 11 deletions e2e/old_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ func TestE2E_OldCLI(t *testing.T) {
},
"call unary RPC with an input file by CLI mode": {
args: "--package api --service Example --call Unary --file testdata/unary_call.in testdata/test.proto",
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC without package name because the size of packages is 1": {
args: "--service Example --call Unary --file testdata/unary_call.in testdata/test.proto",
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC without package and service name because the size of packages and services are 1": {
args: "--call Unary --file testdata/unary_call.in testdata/test.proto",
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC with an input reader by CLI mode": {
args: "--package api --service Example --call Unary testdata/test.proto",
Expand All @@ -135,7 +135,7 @@ func TestE2E_OldCLI(t *testing.T) {
mode.DefaultCLIReader = old
}
},
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call client streaming RPC by CLI mode": {
args: "--package api --service Example --call ClientStreaming --file testdata/client_streaming.in testdata/test.proto",
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestE2E_OldCLI(t *testing.T) {
"call unary RPC by CLI mode with reflection with an input file": {
args: "--reflection --package api --service Example --call Unary --file testdata/unary_call.in",
reflection: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},

// CLI mode with TLS
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestE2E_OldCLI(t *testing.T) {
"call unary RPC with TLS by CLI mode": {
args: "--tls --host localhost --cacert testdata/rootCA.pem --service Example --call Unary --file testdata/unary_call.in testdata/test.proto",
tls: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"cannot launch CLI mode with TLS and reflection by CLI mode because server didn't enable TLS": {
args: "--tls -r --host localhost --cacert testdata/rootCA.pem --service Example --call Unary --file testdata/unary_call.in",
Expand All @@ -235,12 +235,12 @@ func TestE2E_OldCLI(t *testing.T) {
args: "--tls -r --host localhost --cacert testdata/rootCA.pem --service Example --call Unary --file testdata/unary_call.in",
tls: true,
reflection: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC with TLS and --servername by CLI mode": {
args: "--tls --servername localhost --cacert testdata/rootCA.pem --service Example --call Unary --file testdata/unary_call.in testdata/test.proto",
tls: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"cannot launch CLI mode with mutual TLS auth because --certkey is missing": {
args: "--tls --host localhost --cacert testdata/rootCA.pem --cert testdata/localhost.pem --service Example --call Unary --file testdata/unary_call.in testdata/test.proto",
Expand All @@ -255,7 +255,7 @@ func TestE2E_OldCLI(t *testing.T) {
"call unary RPC with mutual TLS auth by CLI mode": {
args: "--tls --host localhost --cacert testdata/rootCA.pem --cert testdata/localhost.pem --certkey testdata/localhost-key.pem --service Example --call Unary --file testdata/unary_call.in testdata/test.proto",
tls: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},

// CLI mode with gRPC-Web
Expand All @@ -268,13 +268,13 @@ func TestE2E_OldCLI(t *testing.T) {
"call unary RPC with an input file by CLI mode against to gRPC-Web server": {
args: "--web --package api --service Example --call Unary --file testdata/unary_call.in testdata/test.proto testdata/test.proto",
web: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call unary RPC with an input file by CLI mode and reflection against to gRPC-Web server": {
args: "--web -r --service Example --call Unary --file testdata/unary_call.in",
web: true,
reflection: true,
expectedOut: `{ "message": "hello, oumae" }`,
expectedOut: `{ "message": "oumae" }`,
},
"call client streaming RPC by CLI mode against to gRPC-Web server": {
args: "--web --service Example --call ClientStreaming --file testdata/client_streaming.in testdata/test.proto",
Expand Down
4 changes: 4 additions & 0 deletions e2e/repl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func TestE2E_REPL(t *testing.T) {
commonFlags: "--service Example --proto testdata/test.proto",
input: []interface{}{"call Unary", "kaguya"},
},
"call Unary with --emit-defaults": {
commonFlags: "--proto testdata/test.proto",
input: []interface{}{"package api", "service Example", "call --emit-defaults Unary", ""},
},
"call ClientStreaming": {
commonFlags: "--proto testdata/test.proto",
// io.EOF means end of inputting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Examples:

Options:
--enrich enrich response output includes header, message, trailer and status (default "false")
--emit-defaults render fields with default values (default "false")
--output, -o string output format. one of "json" or "curl". "curl" is a curl-like format. (default "curl")
--file, -f string a script file that will be executed by (used only CLI mode)
--help, -h display help text and exit (default "false")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

{
"message": "hello, kaguya"
"message": "kaguya"
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


{
"message": "hello, kaguya"
"message": "kaguya"
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"message": "hello, kaguya"
"message": "kaguya"
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"message": "hello, kaguya shinomiya"
"message": "kaguya shinomiya"
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"message": "hello, ai hayasaka"
"message": "ai hayasaka"
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"message": "hello, miyuki, kaguya, chika, yu"
"message": "miyuki, kaguya, chika, yu"
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
{
"message": "hello, "
}
{}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"message": "hello, mumei "
"message": "mumei "
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"message": "hello, kanade hisaishi, kumiko "
"message": "kanade hisaishi, kumiko "
}

1 change: 1 addition & 0 deletions e2e/testdata/fixtures/teste2e_repl-call_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ usage: call <method name>
Options:
--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
--emit-defaults render fields with default values
--enrich enrich response output includes header, message, trailer and status

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

{
"message": "hello, kaguya"
"message": "kaguya"
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


{
"message": "hello, kaguya"
"message": "kaguya"
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ header_key1: header_val1
header_key2: header_val2

{
"message": "hello, kaguya"
"message": "kaguya"
}

trailer_key1: trailer_val1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"message": "hello, kaguya"
"message": "kaguya"
}

Loading

0 comments on commit fb471a3

Please sign in to comment.