From 71db189b99461641addb30ff3fe3484b887ff62d Mon Sep 17 00:00:00 2001 From: Mikhail Katychev Date: Thu, 14 May 2020 18:03:40 -0500 Subject: [PATCH] share formatter between request reader and reponse writer for -format-error --- cmd/grpcurl/grpcurl.go | 2 +- format.go | 18 ------------------ format_test.go | 16 +++++++++++----- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/cmd/grpcurl/grpcurl.go b/cmd/grpcurl/grpcurl.go index adf734b..8bc6f62 100644 --- a/cmd/grpcurl/grpcurl.go +++ b/cmd/grpcurl/grpcurl.go @@ -656,7 +656,7 @@ func main() { } if h.Status.Code() != codes.OK { if *formatError { - grpcurl.HandleFormatAndPrintStatus(grpcurl.Format(*format), os.Stderr, h.Status, *emitDefaults, includeSeparators) + grpcurl.PrintFormattedStatus(os.Stderr, h.Status, formatter) } else { grpcurl.PrintStatus(os.Stderr, h.Status, formatter) } diff --git a/format.go b/format.go index 65aecb1..43d1c37 100644 --- a/format.go +++ b/format.go @@ -429,24 +429,6 @@ func (h *DefaultEventHandler) OnReceiveTrailers(stat *status.Status, md metadata } } -// HandleFormatAndPrintStatus passes the writer, status object, and formatter (if applicable) -// to the appropriate status printing function -func HandleFormatAndPrintStatus(format Format, w io.Writer, stat *status.Status, emitJSONDefaultFields, includeTextSeparator bool) { - var formatter Formatter - switch format { - case FormatJSON: - formatter = NewJSONFormatter( - emitJSONDefaultFields, - anyResolverWithFallback{AnyResolver: jsonpb.Marshaler{Indent: " "}.AnyResolver}) - case FormatText: - formatter = NewTextFormatter(includeTextSeparator) - default: - fmt.Errorf("unknown format: %s", format) - return - } - PrintFormattedStatus(w, stat, formatter) -} - // PrintStatus prints details about the given status to the given writer. The given // formatter is used to print any detail messages that may be included in the status. // If the given status has a code of OK, "OK" is printed and that is all. Otherwise, diff --git a/format_test.go b/format_test.go index a59d305..4d1fd02 100644 --- a/format_test.go +++ b/format_test.go @@ -176,26 +176,30 @@ func TestHandler(t *testing.T) { } func TestPrintFormattedStatus(t *testing.T) { + jsonFormatter := NewJSONFormatter( + true, + anyResolverWithFallback{AnyResolver: jsonpb.Marshaler{Indent: " "}.AnyResolver}) + textFormatter := NewTextFormatter(true) testCases := []struct { input *status.Status - format Format + formatter Formatter expectedOutput string }{ { input: status.New(codes.InvalidArgument, "Missing Argument"), - format: FormatJSON, + formatter: jsonFormatter, expectedOutput: statusAsJSON, }, { input: status.New(codes.InvalidArgument, "Missing Argument"), - format: FormatText, + formatter: textFormatter, expectedOutput: statusAsText, }, } for _, tc := range testCases { var b bytes.Buffer - HandleFormatAndPrintStatus(tc.format, &b, tc.input, false, true) + PrintFormattedStatus(&b, tc.input, tc.formatter) got := b.String() if !compare(tc.expectedOutput, got) { t.Errorf("Incorrect output. Expected:\n%s\nGot:\n%s", tc.expectedOutput, got) @@ -276,7 +280,9 @@ Response contents: ` statusAsJSON = `{ "code": 3, - "message": "Missing Argument" + "message": "Missing Argument", + "details": [ + ] }` statusAsText = `code: 3 message: "Missing Argument"`