@@ -8,74 +8,105 @@ package grpclogging_test
8
8
9
9
import (
10
10
"encoding/json"
11
- "errors"
12
11
13
12
"github.com/golang/protobuf/jsonpb"
14
13
"github.com/hyperledger/fabric/common/grpclogging"
15
14
"github.com/hyperledger/fabric/common/grpclogging/testpb"
16
15
. "github.com/onsi/ginkgo"
17
16
. "github.com/onsi/gomega"
17
+ "github.com/pkg/errors"
18
18
"go.uber.org/zap"
19
19
"go.uber.org/zap/zapcore"
20
20
)
21
21
22
- var _ = Describe ("ProtoMessage" , func () {
23
- var message * testpb.Message
22
+ var _ = Describe ("Fields" , func () {
23
+ Describe ("ProtoMessage" , func () {
24
+ var message * testpb.Message
24
25
25
- BeforeEach (func () {
26
- message = & testpb.Message {
27
- Message : "Je suis une pizza avec du fromage." ,
28
- Sequence : 1337 ,
29
- }
30
- })
26
+ BeforeEach (func () {
27
+ message = & testpb.Message {
28
+ Message : "Je suis une pizza avec du fromage." ,
29
+ Sequence : 1337 ,
30
+ }
31
+ })
31
32
32
- It ("creates a reflect field for zap" , func () {
33
- field := grpclogging .ProtoMessage ("field-key" , message )
34
- Expect (field .Key ).To (Equal ("field-key" ))
35
- _ , ok := field .Interface .(json.Marshaler )
36
- Expect (ok ).To (BeTrue ())
37
- })
33
+ It ("creates a reflect field for zap" , func () {
34
+ field := grpclogging .ProtoMessage ("field-key" , message )
35
+ Expect (field .Key ).To (Equal ("field-key" ))
36
+ _ , ok := field .Interface .(json.Marshaler )
37
+ Expect (ok ).To (BeTrue ())
38
+ })
38
39
39
- It ("marshals messages compatible with jsonpb" , func () {
40
- field := grpclogging .ProtoMessage ("field-key" , message )
41
- marshaler := field .Interface .(json.Marshaler )
40
+ It ("marshals messages compatible with jsonpb" , func () {
41
+ field := grpclogging .ProtoMessage ("field-key" , message )
42
+ marshaler := field .Interface .(json.Marshaler )
42
43
43
- marshaled , err := marshaler .MarshalJSON ()
44
- Expect (err ).NotTo (HaveOccurred ())
44
+ marshaled , err := marshaler .MarshalJSON ()
45
+ Expect (err ).NotTo (HaveOccurred ())
45
46
46
- protoMarshaler := & jsonpb.Marshaler {}
47
- protoJson , err := protoMarshaler .MarshalToString (message )
48
- Expect (err ).NotTo (HaveOccurred ())
47
+ protoMarshaler := & jsonpb.Marshaler {}
48
+ protoJson , err := protoMarshaler .MarshalToString (message )
49
+ Expect (err ).NotTo (HaveOccurred ())
49
50
50
- Expect (marshaled ).To (MatchJSON (protoJson ))
51
- })
51
+ Expect (marshaled ).To (MatchJSON (protoJson ))
52
+ })
52
53
53
- It ("works with zap's json encoder" , func () {
54
- encoder := zapcore .NewJSONEncoder (zapcore.EncoderConfig {
55
- MessageKey : "message" ,
54
+ It ("works with zap's json encoder" , func () {
55
+ encoder := zapcore .NewJSONEncoder (zapcore.EncoderConfig {
56
+ MessageKey : "message" ,
57
+ })
58
+ buf , err := encoder .EncodeEntry (
59
+ zapcore.Entry {Message : "Oh là là" },
60
+ []zapcore.Field {grpclogging .ProtoMessage ("proto-message" , message )},
61
+ )
62
+ Expect (err ).NotTo (HaveOccurred ())
63
+ Expect (buf .String ()).To (MatchJSON (`{"message": "Oh là là", "proto-message": {"message": "Je suis une pizza avec du fromage.", "sequence": 1337}}` ))
56
64
})
57
- buf , err := encoder .EncodeEntry (
58
- zapcore.Entry {Message : "Oh là là" },
59
- []zapcore.Field {grpclogging .ProtoMessage ("proto-message" , message )},
60
- )
61
- Expect (err ).NotTo (HaveOccurred ())
62
- Expect (buf .String ()).To (MatchJSON (`{"message": "Oh là là", "proto-message": {"message": "Je suis une pizza avec du fromage.", "sequence": 1337}}` ))
63
- })
64
65
65
- Context ("when marshaling the message fails" , func () {
66
- It ("it returns the error from marshaling" , func () {
67
- field := grpclogging .ProtoMessage ("field-key" , badProto {err : errors .New ("Boom!" )})
68
- marshaler := field .Interface .(json.Marshaler )
66
+ Context ("when marshaling the message fails" , func () {
67
+ It ("it returns the error from marshaling" , func () {
68
+ field := grpclogging .ProtoMessage ("field-key" , badProto {err : errors .New ("Boom!" )})
69
+ marshaler := field .Interface .(json.Marshaler )
70
+
71
+ _ , err := marshaler .MarshalJSON ()
72
+ Expect (err ).To (MatchError ("Boom!" ))
73
+ })
74
+ })
69
75
70
- _ , err := marshaler .MarshalJSON ()
71
- Expect (err ).To (MatchError ("Boom!" ))
76
+ Context ("when something other than a proto.Message is provided" , func () {
77
+ It ("creates an any field" , func () {
78
+ field := grpclogging .ProtoMessage ("field-key" , "Je ne suis pas une pizza." )
79
+ Expect (field ).To (Equal (zap .Any ("field-key" , "Je ne suis pas une pizza." )))
80
+ })
72
81
})
73
82
})
74
83
75
- Context ("when something other than a proto.Message is provided" , func () {
76
- It ("creates an any field" , func () {
77
- field := grpclogging .ProtoMessage ("field-key" , "Je ne suis pas une pizza." )
78
- Expect (field ).To (Equal (zap .Any ("field-key" , "Je ne suis pas une pizza." )))
84
+ Describe ("Error" , func () {
85
+ It ("creates an error field for zap" , func () {
86
+ err := errors .New ("error" )
87
+ field := grpclogging .Error (err )
88
+ Expect (field .Key ).To (Equal ("error" ))
89
+ Expect (field .Type ).To (Equal (zapcore .ErrorType ))
90
+ Expect (field .Interface ).To (Equal (struct { error }{err }))
91
+ })
92
+
93
+ Context ("when the error is nil" , func () {
94
+ It ("creates a skip field" , func () {
95
+ field := grpclogging .Error (nil )
96
+ Expect (field .Type ).To (Equal (zapcore .SkipType ))
97
+ })
98
+ })
99
+
100
+ It ("omits the verboseError field" , func () {
101
+ encoder := zapcore .NewJSONEncoder (zapcore.EncoderConfig {
102
+ MessageKey : "message" ,
103
+ })
104
+ buf , err := encoder .EncodeEntry (
105
+ zapcore.Entry {Message : "the message" },
106
+ []zapcore.Field {grpclogging .Error (errors .New ("the error" ))},
107
+ )
108
+ Expect (err ).NotTo (HaveOccurred ())
109
+ Expect (buf .String ()).To (MatchJSON (`{"message": "the message", "error": "the error"}` ))
79
110
})
80
111
})
81
112
})
0 commit comments