Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
johanbrandhorst committed Feb 18, 2025
1 parent 565d5c2 commit ecfff06
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 71 deletions.
10 changes: 4 additions & 6 deletions examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/grpc-ecosystem/go-grpc-middleware/examples/v2

go 1.22

toolchain go1.23.1
go 1.23

require (
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.0
Expand Down Expand Up @@ -33,9 +31,9 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
github.com/stretchr/testify v1.10.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 // indirect
google.golang.org/protobuf v1.36.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
12 changes: 6 additions & 6 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H
go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0=
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA=
golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 h1:QCqS/PdaHTSWGvupk2F/ehwHtGc0/GYkT+3GAcR1CCc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
Expand Down
6 changes: 3 additions & 3 deletions interceptors/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"google.golang.org/grpc/status"
)

var authedMarker struct{}
type authedMarker struct{}

var (
commonAuthToken = "some_good_token"
Expand All @@ -41,12 +41,12 @@ func buildDummyAuthFunction(expectedScheme string, expectedToken string) func(ct
if token != expectedToken {
return nil, status.Error(codes.PermissionDenied, "buildDummyAuthFunction bad token")
}
return context.WithValue(ctx, authedMarker, "marker_exists"), nil
return context.WithValue(ctx, authedMarker{}, "marker_exists"), nil
}
}

func assertAuthMarkerExists(t *testing.T, ctx context.Context) {
assert.Equal(t, "marker_exists", ctx.Value(authedMarker).(string), "auth marker from buildDummyAuthFunction must be passed around")
assert.Equal(t, "marker_exists", ctx.Value(authedMarker{}).(string), "auth marker from buildDummyAuthFunction must be passed around")
}

type assertingPingService struct {
Expand Down
4 changes: 2 additions & 2 deletions interceptors/auth/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"google.golang.org/grpc/status"
)

var tokenInfoKey struct{}
type tokenInfoKey struct{}

func parseToken(token string) (struct{}, error) {
return struct{}{}, nil
Expand All @@ -40,7 +40,7 @@ func exampleAuthFunc(ctx context.Context) (context.Context, error) {
ctx = logging.InjectFields(ctx, logging.Fields{"auth.sub", userClaimFromToken(tokenInfo)})

// WARNING: In production define your own type to avoid context collisions.
return context.WithValue(ctx, tokenInfoKey, tokenInfo), nil
return context.WithValue(ctx, tokenInfoKey{}, tokenInfo), nil
}

// Simple example of server initialization code.
Expand Down
7 changes: 1 addition & 6 deletions interceptors/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,10 @@ func (s *ClientInterceptorTestSuite) SetupSuite() {
s.stopped <- s.server.Serve(s.serverListener)
}()

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

// This is the point where we hook up the interceptor.
s.clientConn, err = grpc.DialContext(
ctx,
s.clientConn, err = grpc.NewClient(
s.serverListener.Addr().String(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
grpc.WithUnaryInterceptor(UnaryClientInterceptor(s.mock)),
grpc.WithStreamInterceptor(StreamClientInterceptor(s.mock)),
)
Expand Down
10 changes: 4 additions & 6 deletions interceptors/logging/examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/grpc-ecosystem/go-grpc-middleware/interceptors/logging/examples

go 1.21.1

toolchain go1.21.13
go 1.23

require (
github.com/go-kit/log v0.2.1
Expand All @@ -21,9 +19,9 @@ require (
github.com/mattn/go-isatty v0.0.14 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7 // indirect
google.golang.org/protobuf v1.36.4 // indirect
)
Expand Down
12 changes: 6 additions & 6 deletions interceptors/logging/examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7 h1:2035KHhUv+EpyB+hWgJnaWKJOdX1E95w2S8Rr4uWKTs=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
Expand Down
2 changes: 1 addition & 1 deletion interceptors/protovalidate/protovalidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func startGrpcServer(t *testing.T, called *bool, ignoreMessages ...protoreflect.
return lis.Dial()
}

conn, err := grpc.DialContext(context.Background(),
conn, err := grpc.NewClient(
"bufnet",
grpc.WithContextDialer(dialer),
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down
6 changes: 2 additions & 4 deletions interceptors/ratelimit/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ func ExampleUnaryClientInterceptor() {
// Create stream rateLimiter, based on token bucket here.
// You can implement your own rate-limiter for the interface.
limiter := &alwaysPassLimiter{}
_, _ = grpc.DialContext(
context.Background(),
_, _ = grpc.NewClient(
":8080",
grpc.WithUnaryInterceptor(
ratelimit.UnaryClientInterceptor(limiter),
Expand All @@ -73,8 +72,7 @@ func ExampleStreamClientInterceptor() {
// Create stream rateLimiter, based on token bucket here.
// You can implement your own rate-limiter for the interface.
limiter := &alwaysPassLimiter{}
_, _ = grpc.DialContext(
context.Background(),
_, _ = grpc.NewClient(
":8080",
grpc.WithChainStreamInterceptor(
ratelimit.StreamClientInterceptor(limiter),
Expand Down
6 changes: 3 additions & 3 deletions interceptors/retry/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var cc *grpc.ClientConn

// Simple example of using the default interceptor configuration.
func Example_initialization() {
_, _ = grpc.Dial("myservice.example.com",
_, _ = grpc.NewClient("myservice.example.com",
grpc.WithStreamInterceptor(StreamClientInterceptor()),
grpc.WithUnaryInterceptor(UnaryClientInterceptor()),
)
Expand All @@ -29,7 +29,7 @@ func Example_initializationWithOptions() {
WithBackoff(BackoffLinear(100 * time.Millisecond)),
WithCodes(codes.NotFound, codes.Aborted),
}
_, _ = grpc.Dial("myservice.example.com",
_, _ = grpc.NewClient("myservice.example.com",
grpc.WithStreamInterceptor(StreamClientInterceptor(opts...)),
grpc.WithUnaryInterceptor(UnaryClientInterceptor(opts...)),
)
Expand All @@ -42,7 +42,7 @@ func Example_initializationWithExponentialBackoff() {
opts := []CallOption{
WithBackoff(BackoffExponential(100 * time.Millisecond)),
}
_, _ = grpc.Dial("myservice.example.com",
_, _ = grpc.NewClient("myservice.example.com",
grpc.WithStreamInterceptor(StreamClientInterceptor(opts...)),
grpc.WithUnaryInterceptor(UnaryClientInterceptor(opts...)),
)
Expand Down
4 changes: 2 additions & 2 deletions interceptors/selector/selector_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Example_ratelimit() {
)
}

var tokenInfoKey struct{}
type tokenInfoKey struct{}

func parseToken(token string) (struct{}, error) {
return struct{}{}, nil
Expand All @@ -65,7 +65,7 @@ func exampleAuthFunc(ctx context.Context) (context.Context, error) {
ctx = logging.InjectFields(ctx, logging.Fields{"auth.sub", userClaimFromToken(tokenInfo)})

// WARNING: In production define your own type to avoid context collisions.
return context.WithValue(ctx, tokenInfoKey, tokenInfo), nil
return context.WithValue(ctx, tokenInfoKey{}, tokenInfo), nil
}

func loginSkip(_ context.Context, c interceptors.CallMeta) bool {
Expand Down
5 changes: 1 addition & 4 deletions interceptors/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ func (s *ServerInterceptorTestSuite) SetupSuite() {
_ = s.server.Serve(s.serverListener)
}()

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

s.clientConn, err = grpc.DialContext(ctx, s.serverListener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
s.clientConn, err = grpc.NewClient(s.serverListener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(s.T(), err, "must not error on client Dial")
s.testClient = testpb.NewTestServiceClient(s.clientConn)
}
Expand Down
2 changes: 1 addition & 1 deletion interceptors/timeout/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// Initialization shows an initialization sequence with a custom client request timeout.
func Example_initialization() {
clientConn, err := grpc.Dial(
clientConn, err := grpc.NewClient(
"ServerAddr",
grpc.WithUnaryInterceptor(
// Set your client request timeout.
Expand Down
6 changes: 3 additions & 3 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
grpcMetadata "google.golang.org/grpc/metadata"
)

var parentKey struct{}
type parentKey struct{}

var (
testPairs = []string{"singlekey", "uno", "multikey", "one", "multikey", "two", "multikey", "three"}
parentCtx = context.WithValue(context.TODO(), parentKey, "parentValue")
parentCtx = context.WithValue(context.TODO(), parentKey{}, "parentValue")
)

func assertRetainsParentContext(t *testing.T, ctx context.Context) {
x := ctx.Value(parentKey)
x := ctx.Value(parentKey{})
assert.EqualValues(t, "parentValue", x, "context must contain parentCtx")
}

Expand Down
9 changes: 3 additions & 6 deletions testing/testpb/interceptor_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,18 @@ func (s *InterceptorTestSuite) RestartServer(delayedStart time.Duration) <-chan
}

func (s *InterceptorTestSuite) NewClient(dialOpts ...grpc.DialOption) TestServiceClient {
newDialOpts := append(dialOpts, grpc.WithBlock())
var err error
if *flagTls {
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(certPEM) {
s.T().Fatal("failed to append certificate")
}
creds := credentials.NewTLS(&tls.Config{ServerName: "localhost", RootCAs: cp})
newDialOpts = append(newDialOpts, grpc.WithTransportCredentials(creds))
dialOpts = append(dialOpts, grpc.WithTransportCredentials(creds))
} else {
newDialOpts = append(newDialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
s.clientConn, err = grpc.DialContext(ctx, s.ServerAddr(), newDialOpts...)
s.clientConn, err = grpc.NewClient(s.ServerAddr(), dialOpts...)
require.NoError(s.T(), err, "must not error on client Dial")
return NewTestServiceClient(s.clientConn)
}
Expand Down
8 changes: 1 addition & 7 deletions testing/testpb/pingservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"net"
"testing"
"time"

"github.com/stretchr/testify/require"
"google.golang.org/grpc"
Expand All @@ -34,15 +33,10 @@ func TestPingServiceOnWire(t *testing.T) {
<-stopped
}()

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

// This is the point where we hook up the interceptor.
clientConn, err := grpc.DialContext(
ctx,
clientConn, err := grpc.NewClient(
serverListener.Addr().String(),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
)
require.NoError(t, err, "must not error on client Dial")

Expand Down
10 changes: 5 additions & 5 deletions wrappers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import (
"google.golang.org/grpc/status"
)

var (
type (
someKey struct{}
other struct{}
)

func TestWrapServerStream(t *testing.T) {
ctx := context.WithValue(context.TODO(), someKey, 1)
ctx := context.WithValue(context.TODO(), someKey{}, 1)
fake := &fakeServerStream{ctx: ctx}
wrapped := WrapServerStream(fake)
assert.NotNil(t, wrapped.Context().Value(someKey), "values from fake must propagate to wrapper")
wrapped.WrappedContext = context.WithValue(wrapped.Context(), other, 2)
assert.NotNil(t, wrapped.Context().Value(other), "values from wrapper must be set")
assert.NotNil(t, wrapped.Context().Value(someKey{}), "values from fake must propagate to wrapper")
wrapped.WrappedContext = context.WithValue(wrapped.Context(), other{}, 2)
assert.NotNil(t, wrapped.Context().Value(other{}), "values from wrapper must be set")
}

type fakeServerStream struct {
Expand Down

0 comments on commit ecfff06

Please sign in to comment.