Skip to content

Commit

Permalink
Add possibility enable useProtoNames in encoder. (#12)
Browse files Browse the repository at this point in the history
* Add possibility enable useProtoNames in encoder.

* Upgrade all workflows to go 1.23.
  • Loading branch information
luborco authored Nov 8, 2024
1 parent d17703f commit 187a254
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.22"
go-version: "1.23"

- name: Build
run: make build-only
4 changes: 2 additions & 2 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:

- uses: actions/setup-go@v5
with:
go-version: "1.22"
go-version: "1.23"

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.59
version: v1.61
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.22"
go-version: "1.23"

- name: Test
run: make test-only race
4 changes: 2 additions & 2 deletions .github/workflows/vulncheck.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:

- uses: actions/setup-go@v5
with:
go-version: "1.22"
go-version: "1.23"

- id: govulncheck
uses: golang/govulncheck-action@v1
with:
go-version-input: "1.22"
go-version-input: "1.23"
go-package: ./...
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22-alpine AS build
FROM golang:1.23-alpine AS build

RUN apk add --no-cache --no-progress git make

Expand Down
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: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/eset/grpc-rest-proxy

go 1.22.5
go 1.23

require (
github.com/go-chi/chi/v5 v5.0.8
Expand Down
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
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
Message: msg,
Details: details,
}
Expand Down

0 comments on commit 187a254

Please sign in to comment.