From 187a254ff6aee784642c858ea18936d627a2fcd9 Mon Sep 17 00:00:00 2001 From: Michal Liziciar <51174405+luborco@users.noreply.github.com> Date: Fri, 8 Nov 2024 09:23:16 +0100 Subject: [PATCH] Add possibility enable useProtoNames in encoder. (#12) * Add possibility enable useProtoNames in encoder. * Upgrade all workflows to go 1.23. --- .github/workflows/build.yaml | 2 +- .github/workflows/lint.yaml | 4 ++-- .github/workflows/test.yaml | 2 +- .github/workflows/vulncheck.yaml | 4 ++-- Dockerfile | 2 +- README.md | 1 + cmd/service/main.go | 2 ++ config-example.yaml | 2 +- go.mod | 2 +- pkg/service/jsonencoder/encoder.go | 2 ++ pkg/transport/status/status.go | 4 ++-- 11 files changed, 16 insertions(+), 11 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index ab6e7f9..8fc9cb7 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -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 diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 810b145..d1f2133 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -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 diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 26e796e..ec09f31 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -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 diff --git a/.github/workflows/vulncheck.yaml b/.github/workflows/vulncheck.yaml index 5be908f..e343d20 100644 --- a/.github/workflows/vulncheck.yaml +++ b/.github/workflows/vulncheck.yaml @@ -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: ./... diff --git a/Dockerfile b/Dockerfile index bf5e4f1..b9c245e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/README.md b/README.md index 25a36ae..a33393d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmd/service/main.go b/cmd/service/main.go index 819bbc1..121dbb1 100644 --- a/cmd/service/main.go +++ b/cmd/service/main.go @@ -30,6 +30,7 @@ const ( grpcServerAddr = "0.0.0.0:50051" tls = false tlsSkipverify = false + defaultUseProtoNames = false defaultEmitUnpopulated = false defaultEmitDefaultValues = false ) @@ -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 diff --git a/config-example.yaml b/config-example.yaml index f8aec5d..70b3dc7 100644 --- a/config-example.yaml +++ b/config-example.yaml @@ -24,5 +24,5 @@ gateways: client: targetAddr: "0.0.0.0:50051" requestTimeout: 5s - tls: true + tls: false tlsSkipverify: true diff --git a/go.mod b/go.mod index 4a3ec4c..4a88531 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/pkg/service/jsonencoder/encoder.go b/pkg/service/jsonencoder/encoder.go index a763243..85c7c8f 100644 --- a/pkg/service/jsonencoder/encoder.go +++ b/pkg/service/jsonencoder/encoder.go @@ -12,6 +12,7 @@ import ( ) type Config struct { + UseProtoNames bool `mapstructure:"UseProtoNames"` EmitUnpopulated bool `mapstructure:"emitUnpopulated"` EmitDefaultValues bool `mapstructure:"emitDefaultValues"` } @@ -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, diff --git a/pkg/transport/status/status.go b/pkg/transport/status/status.go index 4ff0d3e..cdae612 100644 --- a/pkg/transport/status/status.go +++ b/pkg/transport/status/status.go @@ -11,7 +11,7 @@ import ( func FromHTTPCode(code int) *Error { return &Error{ - Code: int32(code), + Code: int32(code), //nolint:gosec Message: http.StatusText(code), } } @@ -30,7 +30,7 @@ func FromGRPC(status *grpcStatus.Status) *Error { } return &Error{ - Code: int32(httpStatus), + Code: int32(httpStatus), //nolint:gosec Message: msg, Details: details, }