Skip to content
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

[pull] master from fullstorydev:master #14

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/grpcui/grpcui.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ var (
maxMsgSz = flags.Int("max-msg-sz", 0, prettify(`
The maximum encoded size of a message that grpcui will accept. If not
specified, defaults to 4mb.`))
emitDefaults = flags.Bool("emit-defaults", true, prettify(`
Emit default values for JSON-encoded responses.`))
debug optionalBoolFlag
verbose = flags.Bool("v", false, prettify(`
Enable verbose output.`))
Expand Down Expand Up @@ -625,6 +627,7 @@ func main() {
if examplesOpt != nil {
handlerOpts = append(handlerOpts, examplesOpt)
}
handlerOpts = append(handlerOpts, standalone.EmitDefaults(*emitDefaults))
handlerOpts = append(handlerOpts, configureJSandCSS(extraJS, standalone.AddJSFile)...)
handlerOpts = append(handlerOpts, configureJSandCSS(extraCSS, standalone.AddCSSFile)...)
handlerOpts = append(handlerOpts, configureAssets(otherAssets)...)
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ require (
github.com/golang/protobuf v1.5.3
github.com/jhump/protoreflect v1.15.4
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
golang.org/x/net v0.19.0
golang.org/x/term v0.15.0
golang.org/x/net v0.20.0
golang.org/x/term v0.16.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19
google.golang.org/grpc v1.57.1
google.golang.org/protobuf v1.32.0
Expand All @@ -26,7 +26,7 @@ require (
github.com/envoyproxy/protoc-gen-validate v0.10.1 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.7.0 h1:qe6s0zUXlPX80/dITx3440hWZ7GwMwgDDyrSGTPJG/g=
golang.org/x/oauth2 v0.7.0/go.mod h1:hPLQkd9LyjfXTiRohC/41GhcFqxisoUQ99sCUOHO9x4=
Expand All @@ -67,10 +67,10 @@ golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
Expand Down
32 changes: 18 additions & 14 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type InvokeOptions struct {
// includes conflicting metadata, the values in the HTTP request headers
// will override, and the values in the request will not be sent.
PreserveHeaders []string
// Whether or not default values should be emitted in the JSON response
EmitDefaults bool
// If verbosity is greater than zero, the handler may log events, such as
// cases where the request included metadata that conflicts with the
// ExtraMetadata and PreserveHeaders fields above. It is an int, instead
Expand Down Expand Up @@ -457,8 +459,9 @@ func invokeRPC(ctx context.Context, methodName string, ch grpc.ClientConnInterfa
}

result := rpcResult{
descSource: descSource,
Requests: &reqStats,
descSource: descSource,
emitDefaults: options.EmitDefaults,
Requests: &reqStats,
}
if err := grpcurl.InvokeRPC(ctx, descSource, ch, methodName, invokeHdrs, &result, requestFunc); err != nil {
return nil, err
Expand Down Expand Up @@ -539,12 +542,13 @@ type rpcError struct {
}

type rpcResult struct {
descSource grpcurl.DescriptorSource
Headers []rpcMetadata `json:"headers"`
Error *rpcError `json:"error"`
Responses []rpcResponseElement `json:"responses"`
Requests *rpcRequestStats `json:"requests"`
Trailers []rpcMetadata `json:"trailers"`
descSource grpcurl.DescriptorSource
emitDefaults bool
Headers []rpcMetadata `json:"headers"`
Error *rpcError `json:"error"`
Responses []rpcResponseElement `json:"responses"`
Requests *rpcRequestStats `json:"requests"`
Trailers []rpcMetadata `json:"trailers"`
}

func (*rpcResult) OnResolveMethod(*desc.MethodDescriptor) {}
Expand All @@ -556,12 +560,12 @@ func (r *rpcResult) OnReceiveHeaders(md metadata.MD) {
}

func (r *rpcResult) OnReceiveResponse(m proto.Message) {
r.Responses = append(r.Responses, responseToJSON(r.descSource, m))
r.Responses = append(r.Responses, responseToJSON(r.descSource, m, r.emitDefaults))
}

func (r *rpcResult) OnReceiveTrailers(stat *status.Status, md metadata.MD) {
r.Trailers = responseMetadata(md)
r.Error = toRpcError(r.descSource, stat)
r.Error = toRpcError(r.descSource, stat, r.emitDefaults)
}

func responseMetadata(md metadata.MD) []rpcMetadata {
Expand All @@ -583,15 +587,15 @@ func responseMetadata(md metadata.MD) []rpcMetadata {
return ret
}

func toRpcError(descSource grpcurl.DescriptorSource, stat *status.Status) *rpcError {
func toRpcError(descSource grpcurl.DescriptorSource, stat *status.Status, emitDefaults bool) *rpcError {
if stat.Code() == codes.OK {
return nil
}

details := stat.Proto().Details
msgs := make([]rpcResponseElement, len(details))
for i, d := range details {
msgs[i] = responseToJSON(descSource, d)
msgs[i] = responseToJSON(descSource, d, emitDefaults)
}
return &rpcError{
Code: uint32(stat.Code()),
Expand All @@ -601,9 +605,9 @@ func toRpcError(descSource grpcurl.DescriptorSource, stat *status.Status) *rpcEr
}
}

func responseToJSON(descSource grpcurl.DescriptorSource, msg proto.Message) rpcResponseElement {
func responseToJSON(descSource grpcurl.DescriptorSource, msg proto.Message, emitDefaults bool) rpcResponseElement {
anyResolver := grpcurl.AnyResolverFromDescriptorSourceWithFallback(descSource)
jsm := jsonpb.Marshaler{EmitDefaults: true, OrigName: true, Indent: " ", AnyResolver: anyResolver}
jsm := jsonpb.Marshaler{EmitDefaults: emitDefaults, OrigName: true, Indent: " ", AnyResolver: anyResolver}
var b bytes.Buffer
if err := jsm.Marshal(&b, msg); err == nil {
return rpcResponseElement{Data: json.RawMessage(b.Bytes())}
Expand Down
8 changes: 8 additions & 0 deletions standalone/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ func PreserveHeaders(headerNames []string) HandlerOption {
})
}

// EmitDefaults tells gRPCurl whether or not default values should be emitted
func EmitDefaults(emit bool) HandlerOption {
return optFunc(func(opts *handlerOptions) {
opts.emitDefaults = emit
})
}

// WithInvokeVerbosity indicates the level of log output from the gRPC UI server
// handler that performs RPC invocations.
func WithInvokeVerbosity(verbosity int) HandlerOption {
Expand Down Expand Up @@ -229,6 +236,7 @@ type handlerOptions struct {
defaultMetadata []string
extraMetadata []string
preserveHeaders []string
emitDefaults bool
invokeVerbosity int
debug *bool
}
Expand Down
1 change: 1 addition & 0 deletions standalone/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func Handler(ch grpcdynamic.Channel, target string, methods []*desc.MethodDescri
invokeOpts := grpcui.InvokeOptions{
ExtraMetadata: uiOpts.extraMetadata,
PreserveHeaders: uiOpts.preserveHeaders,
EmitDefaults: uiOpts.emitDefaults,
Verbosity: uiOpts.invokeVerbosity,
}
rpcInvokeHandler := http.StripPrefix("/invoke", grpcui.RPCInvokeHandlerWithOptions(ch, methods, invokeOpts))
Expand Down