Skip to content

Commit a5b9e0b

Browse files
Move util/metautils to root-level package metadata, fixes #392 (#474)
1 parent ed43ab3 commit a5b9e0b

File tree

10 files changed

+155
-156
lines changed

10 files changed

+155
-156
lines changed

interceptors/auth/auth_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import (
1616
"google.golang.org/grpc"
1717
"google.golang.org/grpc/codes"
1818
"google.golang.org/grpc/credentials/oauth"
19-
"google.golang.org/grpc/metadata"
19+
grpcMetadata "google.golang.org/grpc/metadata"
2020
"google.golang.org/grpc/status"
2121

2222
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
23+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
2324
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
24-
"github.com/grpc-ecosystem/go-grpc-middleware/v2/util/metautils"
2525
)
2626

2727
var authedMarker struct{}
@@ -66,9 +66,8 @@ func (s *assertingPingService) PingList(ping *testpb.PingListRequest, stream tes
6666
}
6767

6868
func ctxWithToken(ctx context.Context, scheme string, token string) context.Context {
69-
md := metadata.Pairs("authorization", fmt.Sprintf("%s %v", scheme, token))
70-
nCtx := metautils.NiceMD(md).ToOutgoing(ctx)
71-
return nCtx
69+
md := grpcMetadata.Pairs("authorization", fmt.Sprintf("%s %v", scheme, token))
70+
return metadata.MD(md).ToOutgoing(ctx)
7271
}
7372

7473
func TestAuthTestSuite(t *testing.T) {

interceptors/auth/metadata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"google.golang.org/grpc/codes"
1111
"google.golang.org/grpc/status"
1212

13-
"github.com/grpc-ecosystem/go-grpc-middleware/v2/util/metautils"
13+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
1414
)
1515

1616
var (
@@ -23,7 +23,7 @@ var (
2323
// case-insensitive format (see rfc2617, sec 1.2). If no such authorization is found, or the token
2424
// is of wrong scheme, an error with gRPC status `Unauthenticated` is returned.
2525
func AuthFromMD(ctx context.Context, expectedScheme string) (string, error) {
26-
val := metautils.ExtractIncoming(ctx).Get(headerAuthorize)
26+
val := metadata.ExtractIncoming(ctx).Get(headerAuthorize)
2727
if val == "" {
2828
return "", status.Errorf(codes.Unauthenticated, "Request unauthenticated with "+expectedScheme)
2929
}

interceptors/auth/metadata_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,60 @@ import (
99

1010
"github.com/stretchr/testify/assert"
1111
"google.golang.org/grpc/codes"
12-
"google.golang.org/grpc/metadata"
12+
grpcMetadata "google.golang.org/grpc/metadata"
1313
"google.golang.org/grpc/status"
1414

15-
"github.com/grpc-ecosystem/go-grpc-middleware/v2/util/metautils"
15+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
1616
)
1717

1818
func TestAuthFromMD(t *testing.T) {
1919
for _, run := range []struct {
20-
md metadata.MD
20+
md grpcMetadata.MD
2121
value string
2222
errCode codes.Code
2323
msg string
2424
}{
2525
{
26-
md: metadata.Pairs("authorization", "bearer some_token"),
26+
md: grpcMetadata.Pairs("authorization", "bearer some_token"),
2727
value: "some_token",
2828
msg: "must extract simple bearer tokens without case checking",
2929
},
3030
{
31-
md: metadata.Pairs("authorization", "Bearer some_token"),
31+
md: grpcMetadata.Pairs("authorization", "Bearer some_token"),
3232
value: "some_token",
3333
msg: "must extract simple bearer tokens with case checking",
3434
},
3535
{
36-
md: metadata.Pairs("authorization", "Bearer some multi string bearer"),
36+
md: grpcMetadata.Pairs("authorization", "Bearer some multi string bearer"),
3737
value: "some multi string bearer",
3838
msg: "must handle string based bearers",
3939
},
4040
{
41-
md: metadata.Pairs("authorization", "Basic login:passwd"),
41+
md: grpcMetadata.Pairs("authorization", "Basic login:passwd"),
4242
value: "",
4343
errCode: codes.Unauthenticated,
4444
msg: "must check authentication type",
4545
},
4646
{
47-
md: metadata.Pairs("authorization", "Basic login:passwd", "authorization", "bearer some_token"),
47+
md: grpcMetadata.Pairs("authorization", "Basic login:passwd", "authorization", "bearer some_token"),
4848
value: "",
4949
errCode: codes.Unauthenticated,
5050
msg: "must not allow multiple authentication methods",
5151
},
5252
{
53-
md: metadata.Pairs("authorization", ""),
53+
md: grpcMetadata.Pairs("authorization", ""),
5454
value: "",
5555
errCode: codes.Unauthenticated,
5656
msg: "authorization string must not be empty",
5757
},
5858
{
59-
md: metadata.Pairs("authorization", "Bearer"),
59+
md: grpcMetadata.Pairs("authorization", "Bearer"),
6060
value: "",
6161
errCode: codes.Unauthenticated,
6262
msg: "bearer token must not be empty",
6363
},
6464
} {
65-
ctx := metautils.NiceMD(run.md).ToIncoming(context.TODO())
65+
ctx := metadata.MD(run.md).ToIncoming(context.TODO())
6666
out, err := AuthFromMD(ctx, "bearer")
6767
if run.errCode != codes.OK {
6868
assert.Equal(t, run.errCode, status.Code(err), run.msg)

interceptors/retry/retry.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"golang.org/x/net/trace"
1414
"google.golang.org/grpc"
1515
"google.golang.org/grpc/codes"
16-
"google.golang.org/grpc/metadata"
16+
grpcMetadata "google.golang.org/grpc/metadata"
1717
"google.golang.org/grpc/status"
1818

19-
"github.com/grpc-ecosystem/go-grpc-middleware/v2/util/metautils"
19+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
2020
)
2121

2222
const (
@@ -170,11 +170,11 @@ func (s *serverStreamingRetryingStream) CloseSend() error {
170170
return s.getStream().CloseSend()
171171
}
172172

173-
func (s *serverStreamingRetryingStream) Header() (metadata.MD, error) {
173+
func (s *serverStreamingRetryingStream) Header() (grpcMetadata.MD, error) {
174174
return s.getStream().Header()
175175
}
176176

177-
func (s *serverStreamingRetryingStream) Trailer() metadata.MD {
177+
func (s *serverStreamingRetryingStream) Trailer() grpcMetadata.MD {
178178
return s.getStream().Trailer()
179179
}
180180

@@ -296,7 +296,7 @@ func perCallContext(parentCtx context.Context, callOpts *options, attempt uint)
296296
ctx, cancel = context.WithTimeout(ctx, callOpts.perCallTimeout)
297297
}
298298
if attempt > 0 && callOpts.includeHeader {
299-
mdClone := metautils.ExtractOutgoing(ctx).Clone().Set(AttemptMetadataKey, fmt.Sprintf("%d", attempt))
299+
mdClone := metadata.ExtractOutgoing(ctx).Clone().Set(AttemptMetadataKey, fmt.Sprintf("%d", attempt))
300300
ctx = mdClone.ToOutgoing(ctx)
301301
}
302302
return ctx, cancel

interceptors/skip/interceptor_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/stretchr/testify/suite"
1515
"google.golang.org/grpc"
1616
"google.golang.org/grpc/codes"
17-
"google.golang.org/grpc/metadata"
17+
grpcMetadata "google.golang.org/grpc/metadata"
1818
"google.golang.org/grpc/status"
1919

2020
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
@@ -42,7 +42,7 @@ type skipPingService struct {
4242
}
4343

4444
func checkMetadata(ctx context.Context, grpcType interceptors.GRPCType, service string, method string) error {
45-
m, _ := metadata.FromIncomingContext(ctx)
45+
m, _ := grpcMetadata.FromIncomingContext(ctx)
4646
if typeFromMetadata := m.Get(keyGRPCType)[0]; typeFromMetadata != string(grpcType) {
4747
return status.Errorf(codes.Internal, fmt.Sprintf("expected grpc type %s, got: %s", grpcType, typeFromMetadata))
4848
}
@@ -82,7 +82,7 @@ func (s *skipPingService) PingList(_ *testpb.PingListRequest, stream testpb.Test
8282
}
8383

8484
func filter(ctx context.Context, gRPCType interceptors.GRPCType, service string, method string) bool {
85-
m, _ := metadata.FromIncomingContext(ctx)
85+
m, _ := grpcMetadata.FromIncomingContext(ctx)
8686
// Set parameters into metadata
8787
m.Set(keyGRPCType, string(gRPCType))
8888
m.Set(keyService, service)
@@ -144,14 +144,14 @@ func (s *SkipSuite) TestPing() {
144144

145145
for _, tc := range testCases {
146146
t.Run(tc.name, func(t *testing.T) {
147-
var m metadata.MD
147+
var m grpcMetadata.MD
148148
if tc.skip {
149-
m = metadata.New(map[string]string{
149+
m = grpcMetadata.New(map[string]string{
150150
"skip": "true",
151151
})
152152
}
153153

154-
resp, err := s.Client.Ping(metadata.NewOutgoingContext(s.SimpleCtx(), m), testpb.GoodPing)
154+
resp, err := s.Client.Ping(grpcMetadata.NewOutgoingContext(s.SimpleCtx(), m), testpb.GoodPing)
155155
require.NoError(t, err)
156156

157157
var value string
@@ -182,14 +182,14 @@ func (s *SkipSuite) TestPingList() {
182182

183183
for _, tc := range testCases {
184184
t.Run(tc.name, func(t *testing.T) {
185-
var m metadata.MD
185+
var m grpcMetadata.MD
186186
if tc.skip {
187-
m = metadata.New(map[string]string{
187+
m = grpcMetadata.New(map[string]string{
188188
"skip": "true",
189189
})
190190
}
191191

192-
stream, err := s.Client.PingList(metadata.NewOutgoingContext(s.SimpleCtx(), m), testpb.GoodPingList)
192+
stream, err := s.Client.PingList(grpcMetadata.NewOutgoingContext(s.SimpleCtx(), m), testpb.GoodPingList)
193193
require.NoError(t, err)
194194

195195
for {

util/metautils/doc.go renamed to metadata/doc.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
// Licensed under the Apache License 2.0.
33

44
/*
5-
Package `metautils` provides convenience functions for dealing with gRPC metadata.MD objects inside
5+
Package `metadata` provides convenience functions for dealing with gRPC metadata.MD objects inside
66
Context handlers.
77
88
While the upstream grpc-go package contains decent functionality (see https://github.com/grpc/grpc-go/blob/master/Documentation/grpc-metadata.md)
99
they are hard to use.
1010
11-
The majority of functions center around the NiceMD, which is a convenience wrapper around metadata.MD. For example
11+
The majority of functions center around the MD, which is a convenience wrapper around metadata.MD. For example
1212
the following code allows you to easily extract incoming metadata (server handler) and put it into a new client context
1313
metadata.
1414
15-
nmd := metautils.ExtractIncoming(serverCtx).Clone(":authorization", ":custom")
16-
clientCtx := nmd.Set("x-client-header", "2").Set("x-another", "3").ToOutgoing(ctx)
15+
md := metadata.ExtractIncoming(serverCtx).Clone(":authorization", ":custom")
16+
clientCtx := md.Set("x-client-header", "2").Set("x-another", "3").ToOutgoing(ctx)
1717
*/
1818

19-
package metautils
19+
package metadata

util/metautils/nicemd.go renamed to metadata/metadata.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
// Copyright (c) The go-grpc-middleware Authors.
22
// Licensed under the Apache License 2.0.
33

4-
package metautils
4+
package metadata
55

66
import (
77
"context"
88
"strings"
99

10-
"google.golang.org/grpc/metadata"
10+
grpcMetadata "google.golang.org/grpc/metadata"
1111
)
1212

13-
// NiceMD is a convenience wrapper defining extra functions on the metadata.
14-
type NiceMD metadata.MD
13+
// MD is a convenience wrapper defining extra functions on the metadata.
14+
type MD grpcMetadata.MD
1515

1616
// ExtractIncoming extracts an inbound metadata from the server-side context.
1717
//
18-
// This function always returns a NiceMD wrapper of the metadata.MD, in case the context doesn't have metadata it returns
19-
// a new empty NiceMD.
20-
func ExtractIncoming(ctx context.Context) NiceMD {
21-
md, ok := metadata.FromIncomingContext(ctx)
18+
// This function always returns a MD wrapper of the grpcMetadata.MD, in case the context doesn't have metadata it returns
19+
// a new empty MD.
20+
func ExtractIncoming(ctx context.Context) MD {
21+
md, ok := grpcMetadata.FromIncomingContext(ctx)
2222
if !ok {
23-
return NiceMD(metadata.Pairs())
23+
return MD(grpcMetadata.Pairs())
2424
}
25-
return NiceMD(md)
25+
return MD(md)
2626
}
2727

2828
// ExtractOutgoing extracts an outbound metadata from the client-side context.
2929
//
30-
// This function always returns a NiceMD wrapper of the metadata.MD, in case the context doesn't have metadata it returns
31-
// a new empty NiceMD.
32-
func ExtractOutgoing(ctx context.Context) NiceMD {
33-
md, ok := metadata.FromOutgoingContext(ctx)
30+
// This function always returns a MD wrapper of the grpcMetadata.MD, in case the context doesn't have metadata it returns
31+
// a new empty MD.
32+
func ExtractOutgoing(ctx context.Context) MD {
33+
md, ok := grpcMetadata.FromOutgoingContext(ctx)
3434
if !ok {
35-
return NiceMD(metadata.Pairs())
35+
return MD(grpcMetadata.Pairs())
3636
}
37-
return NiceMD(md)
37+
return MD(md)
3838
}
3939

40-
// Clone performs a *deep* copy of the metadata.MD.
40+
// Clone performs a *deep* copy of the grpcMetadata.MD.
4141
//
4242
// You can specify the lower-case copiedKeys to only copy certain whitelisted keys. If no keys are explicitly whitelisted
4343
// all keys get copied.
44-
func (m NiceMD) Clone(copiedKeys ...string) NiceMD {
45-
newMd := NiceMD(metadata.Pairs())
44+
func (m MD) Clone(copiedKeys ...string) MD {
45+
newMd := MD(grpcMetadata.Pairs())
4646
for k, vv := range m {
4747
found := false
4848
if len(copiedKeys) == 0 {
@@ -64,16 +64,16 @@ func (m NiceMD) Clone(copiedKeys ...string) NiceMD {
6464
return newMd
6565
}
6666

67-
// ToOutgoing sets the given NiceMD as a client-side context for dispatching.
68-
func (m NiceMD) ToOutgoing(ctx context.Context) context.Context {
69-
return metadata.NewOutgoingContext(ctx, metadata.MD(m))
67+
// ToOutgoing sets the given MD as a client-side context for dispatching.
68+
func (m MD) ToOutgoing(ctx context.Context) context.Context {
69+
return grpcMetadata.NewOutgoingContext(ctx, grpcMetadata.MD(m))
7070
}
7171

72-
// ToIncoming sets the given NiceMD as a server-side context for dispatching.
72+
// ToIncoming sets the given MD as a server-side context for dispatching.
7373
//
7474
// This is mostly useful in ServerInterceptors.
75-
func (m NiceMD) ToIncoming(ctx context.Context) context.Context {
76-
return metadata.NewIncomingContext(ctx, metadata.MD(m))
75+
func (m MD) ToIncoming(ctx context.Context) context.Context {
76+
return grpcMetadata.NewIncomingContext(ctx, grpcMetadata.MD(m))
7777
}
7878

7979
// Get retrieves a single value from the metadata.
@@ -82,7 +82,7 @@ func (m NiceMD) ToIncoming(ctx context.Context) context.Context {
8282
// an empty string is returned.
8383
//
8484
// The function is binary-key safe.
85-
func (m NiceMD) Get(key string) string {
85+
func (m MD) Get(key string) string {
8686
k, _ := encodeKeyValue(key, "")
8787
vv, ok := m[k]
8888
if !ok {
@@ -97,7 +97,7 @@ func (m NiceMD) Get(key string) string {
9797
//
9898
// The function is binary-key safe.
9999

100-
func (m NiceMD) Del(key string) NiceMD {
100+
func (m MD) Del(key string) MD {
101101
k, _ := encodeKeyValue(key, "")
102102
delete(m, k)
103103
return m
@@ -108,7 +108,7 @@ func (m NiceMD) Del(key string) NiceMD {
108108
// It works analogously to http.Header.Set, overwriting all previous metadata values.
109109
//
110110
// The function is binary-key safe.
111-
func (m NiceMD) Set(key string, value string) NiceMD {
111+
func (m MD) Set(key string, value string) MD {
112112
k, v := encodeKeyValue(key, value)
113113
m[k] = []string{v}
114114
return m
@@ -119,7 +119,7 @@ func (m NiceMD) Set(key string, value string) NiceMD {
119119
// It works analogously to http.Header.Add, as it appends to any existing values associated with key.
120120
//
121121
// The function is binary-key safe.
122-
func (m NiceMD) Add(key string, value string) NiceMD {
122+
func (m MD) Add(key string, value string) MD {
123123
k, v := encodeKeyValue(key, value)
124124
m[k] = append(m[k], v)
125125
return m

0 commit comments

Comments
 (0)