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

feat: add -keep-proto-names flag #360

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions cmd/grpcurl/grpcurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ var (
will accept. If not specified, defaults to 4,194,304 (4 megabytes).`))
emitDefaults = flags.Bool("emit-defaults", false, prettify(`
Emit default values for JSON-encoded responses.`))
keepProtoNames = flags.Bool("keep-proto-names", false, prettify(`
Use the original protobuf name for fields instead of lowerCamelCase, for JSON-encoded responses.`))
protosetOut = flags.String("protoset-out", "", prettify(`
The name of a file to be written that will contain a FileDescriptorSet
proto. With the list and describe verbs, the listed or described
Expand Down Expand Up @@ -299,6 +301,9 @@ func main() {
if *emitDefaults && *format != "json" {
warn("The -emit-defaults is only used when using json format.")
}
if *keepProtoNames && *format != "json" {
warn("The -keep-proto-names is only used when using json format.")
}

args := flags.Args()

Expand Down Expand Up @@ -691,6 +696,7 @@ func main() {
includeSeparators := verbosityLevel == 0
options := grpcurl.FormatOptions{
EmitJSONDefaultFields: *emitDefaults,
OrigName: *keepProtoNames,
IncludeTextSeparator: includeSeparators,
AllowUnknownFields: *allowUnknownFields,
}
Expand Down
8 changes: 6 additions & 2 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,12 @@ type Formatter func(proto.Message) (string, error)
// include empty/default values (instead of just omitted them) if emitDefaults
// is true. The given resolver is used to assist with encoding of
// google.protobuf.Any messages.
func NewJSONFormatter(emitDefaults bool, resolver jsonpb.AnyResolver) Formatter {
func NewJSONFormatter(emitDefaults, origName bool, resolver jsonpb.AnyResolver) Formatter {
marshaler := jsonpb.Marshaler{
EmitDefaults: emitDefaults,
Indent: " ",
AnyResolver: resolver,
OrigName: origName,
}
return marshaler.MarshalToString
}
Expand Down Expand Up @@ -380,6 +381,9 @@ type FormatOptions struct {
// It might be useful when the output is piped to another grpcurl process.
// FormatText only flag.
IncludeTextSeparator bool

// OrigName specifies whether to use the original protobuf name for fields.
OrigName bool
}

// RequestParserAndFormatter returns a request parser and formatter for the
Expand All @@ -394,7 +398,7 @@ func RequestParserAndFormatter(format Format, descSource DescriptorSource, in io
case FormatJSON:
resolver := AnyResolverFromDescriptorSource(descSource)
unmarshaler := jsonpb.Unmarshaler{AnyResolver: resolver, AllowUnknownFields: opts.AllowUnknownFields}
return NewJSONRequestParserWithUnmarshaler(in, unmarshaler), NewJSONFormatter(opts.EmitJSONDefaultFields, anyResolverWithFallback{AnyResolver: resolver}), nil
return NewJSONRequestParserWithUnmarshaler(in, unmarshaler), NewJSONFormatter(opts.EmitJSONDefaultFields, opts.OrigName, anyResolverWithFallback{AnyResolver: resolver}), nil
case FormatText:
return NewTextRequestParser(in), NewTextFormatter(opts.IncludeTextSeparator), nil
default:
Expand Down