Skip to content

Commit

Permalink
Add possibility enable useProtoNames in encoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
luborco committed Nov 7, 2024
1 parent d17703f commit be893e3
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Usage of ./grpc-rest-proxy:
--transport.http.server.gracefulTimeout duration graceful timeout (default 5s)
--transport.http.server.readHeaderTimeout duration read header timeout (default 5s)
--transport.http.server.readTimeout duration read timeout (default 10s)
--service.jsonencoder.useProtoNames use proto names in JSON response (instead of camel case)
--service.jsonencoder.emitUnpopulated emit unpopulated fields in JSON response for empty gRPC values
--service.jsonencoder.emitDefaultValues include default values in JSON response for empty gRPC values
-v, --version print version
Expand Down
2 changes: 2 additions & 0 deletions cmd/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
grpcServerAddr = "0.0.0.0:50051"
tls = false
tlsSkipverify = false
defaultUseProtoNames = false
defaultEmitUnpopulated = false
defaultEmitDefaultValues = false
)
Expand Down Expand Up @@ -61,6 +62,7 @@ func main() {
pflag.Bool("gateways.grpc.client.tls", tls, "use TLS for gRPC connection")
pflag.Bool("gateways.grpc.client.tlsSkipverify", tlsSkipverify, "skip TLS verification")

pflag.Bool("service.jsonencoder.useProtoNames", defaultUseProtoNames, "use proto names in JSON response (instead of camel case)")
pflag.Bool("service.jsonencoder.emitUnpopulated", defaultEmitUnpopulated, "emit unpopulated fields in JSON response for empty gRPC values")
pflag.Bool("service.jsonencoder.emitDefaultValues", defaultEmitDefaultValues, "include default values in JSON response for empty gRPC values") //nolint:lll

Expand Down
2 changes: 1 addition & 1 deletion config-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ gateways:
client:
targetAddr: "0.0.0.0:50051"
requestTimeout: 5s
tls: true
tls: false
tlsSkipverify: true
2 changes: 2 additions & 0 deletions pkg/service/jsonencoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

type Config struct {
UseProtoNames bool `mapstructure:"UseProtoNames"`
EmitUnpopulated bool `mapstructure:"emitUnpopulated"`
EmitDefaultValues bool `mapstructure:"emitDefaultValues"`
}
Expand All @@ -25,6 +26,7 @@ type Encoder struct {
func New(cfg *Config, typeResolver *protoregistry.Types) Encoder {
return Encoder{
opts: protojson.MarshalOptions{
UseProtoNames: cfg.UseProtoNames,
EmitUnpopulated: cfg.EmitUnpopulated,
EmitDefaultValues: cfg.EmitDefaultValues,
Resolver: typeResolver,
Expand Down
4 changes: 2 additions & 2 deletions pkg/transport/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func FromHTTPCode(code int) *Error {
return &Error{
Code: int32(code),
Code: int32(code), //nolint:gosec

Check failure on line 14 in pkg/transport/status/status.go

View workflow job for this annotation

GitHub Actions / golangci

directive `//nolint:gosec` is unused for linter "gosec" (nolintlint)
Message: http.StatusText(code),
}
}
Expand All @@ -30,7 +30,7 @@ func FromGRPC(status *grpcStatus.Status) *Error {
}

return &Error{
Code: int32(httpStatus),
Code: int32(httpStatus), //nolint:gosec

Check failure on line 33 in pkg/transport/status/status.go

View workflow job for this annotation

GitHub Actions / golangci

directive `//nolint:gosec` is unused for linter "gosec" (nolintlint)
Message: msg,
Details: details,
}
Expand Down

0 comments on commit be893e3

Please sign in to comment.