|
| 1 | +package log |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "go.uber.org/zap" |
| 9 | + "go.uber.org/zap/zapcore" |
| 10 | + "go.uber.org/zap/zaptest/observer" |
| 11 | + "google.golang.org/protobuf/reflect/protoreflect" |
| 12 | + "google.golang.org/protobuf/runtime/protoimpl" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // Verify that this generated code is sufficiently up-to-date. |
| 17 | + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) |
| 18 | + // Verify that runtime/protoimpl is sufficiently up-to-date. |
| 19 | + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) |
| 20 | +) |
| 21 | + |
| 22 | +type ProtoTest struct { |
| 23 | + state protoimpl.MessageState |
| 24 | + sizeCache protoimpl.SizeCache |
| 25 | + unknownFields protoimpl.UnknownFields |
| 26 | + |
| 27 | + Foo string `protobuf:"bytes,1,opt,name=foo,proto3" json:"foo,omitempty"` |
| 28 | +} |
| 29 | + |
| 30 | +func (x *ProtoTest) Reset() { |
| 31 | + *x = ProtoTest{} |
| 32 | + if protoimpl.UnsafeEnabled { |
| 33 | + mi := &file_p_proto_msgTypes[0] |
| 34 | + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) |
| 35 | + ms.StoreMessageInfo(mi) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (x *ProtoTest) String() string { |
| 40 | + return protoimpl.X.MessageStringOf(x) |
| 41 | +} |
| 42 | + |
| 43 | +func (*ProtoTest) ProtoMessage() {} |
| 44 | + |
| 45 | +func (x *ProtoTest) ProtoReflect() protoreflect.Message { |
| 46 | + mi := &file_p_proto_msgTypes[0] |
| 47 | + if protoimpl.UnsafeEnabled && x != nil { |
| 48 | + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) |
| 49 | + if ms.LoadMessageInfo() == nil { |
| 50 | + ms.StoreMessageInfo(mi) |
| 51 | + } |
| 52 | + return ms |
| 53 | + } |
| 54 | + return mi.MessageOf(x) |
| 55 | +} |
| 56 | + |
| 57 | +// Deprecated: Use ProtoTest.ProtoReflect.Descriptor instead. |
| 58 | +func (*ProtoTest) Descriptor() ([]byte, []int) { |
| 59 | + return file_p_proto_rawDescGZIP(), []int{0} |
| 60 | +} |
| 61 | + |
| 62 | +func (x *ProtoTest) GetFoo() string { |
| 63 | + if x != nil { |
| 64 | + return x.Foo |
| 65 | + } |
| 66 | + return "" |
| 67 | +} |
| 68 | + |
| 69 | +var File_p_proto protoreflect.FileDescriptor |
| 70 | + |
| 71 | +var file_p_proto_rawDesc = []byte{ |
| 72 | + 0x0a, 0x07, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x1d, 0x0a, 0x09, 0x50, 0x72, 0x6f, |
| 73 | + 0x74, 0x6f, 0x54, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x66, 0x6f, 0x6f, 0x18, 0x01, 0x20, |
| 74 | + 0x01, 0x28, 0x09, 0x52, 0x03, 0x66, 0x6f, 0x6f, 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x70, 0x72, |
| 75 | + 0x6f, 0x74, 0x6f, 0x6c, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, |
| 76 | +} |
| 77 | + |
| 78 | +var ( |
| 79 | + file_p_proto_rawDescOnce sync.Once |
| 80 | + file_p_proto_rawDescData = file_p_proto_rawDesc |
| 81 | +) |
| 82 | + |
| 83 | +func file_p_proto_rawDescGZIP() []byte { |
| 84 | + file_p_proto_rawDescOnce.Do(func() { |
| 85 | + file_p_proto_rawDescData = protoimpl.X.CompressGZIP(file_p_proto_rawDescData) |
| 86 | + }) |
| 87 | + return file_p_proto_rawDescData |
| 88 | +} |
| 89 | + |
| 90 | +var file_p_proto_msgTypes = make([]protoimpl.MessageInfo, 1) |
| 91 | +var file_p_proto_goTypes = []interface{}{ |
| 92 | + (*ProtoTest)(nil), // 0: ProtoTest |
| 93 | +} |
| 94 | +var file_p_proto_depIdxs = []int32{ |
| 95 | + 0, // [0:0] is the sub-list for method output_type |
| 96 | + 0, // [0:0] is the sub-list for method input_type |
| 97 | + 0, // [0:0] is the sub-list for extension type_name |
| 98 | + 0, // [0:0] is the sub-list for extension extendee |
| 99 | + 0, // [0:0] is the sub-list for field type_name |
| 100 | +} |
| 101 | + |
| 102 | +func init() { file_p_proto_init() } |
| 103 | +func file_p_proto_init() { |
| 104 | + if File_p_proto != nil { |
| 105 | + return |
| 106 | + } |
| 107 | + if !protoimpl.UnsafeEnabled { |
| 108 | + file_p_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { |
| 109 | + switch v := v.(*ProtoTest); i { |
| 110 | + case 0: |
| 111 | + return &v.state |
| 112 | + case 1: |
| 113 | + return &v.sizeCache |
| 114 | + case 2: |
| 115 | + return &v.unknownFields |
| 116 | + default: |
| 117 | + return nil |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + type x struct{} |
| 122 | + out := protoimpl.TypeBuilder{ |
| 123 | + File: protoimpl.DescBuilder{ |
| 124 | + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), |
| 125 | + RawDescriptor: file_p_proto_rawDesc, |
| 126 | + NumEnums: 0, |
| 127 | + NumMessages: 1, |
| 128 | + NumExtensions: 0, |
| 129 | + NumServices: 0, |
| 130 | + }, |
| 131 | + GoTypes: file_p_proto_goTypes, |
| 132 | + DependencyIndexes: file_p_proto_depIdxs, |
| 133 | + MessageInfos: file_p_proto_msgTypes, |
| 134 | + }.Build() |
| 135 | + File_p_proto = out.File |
| 136 | + file_p_proto_rawDesc = nil |
| 137 | + file_p_proto_goTypes = nil |
| 138 | + file_p_proto_depIdxs = nil |
| 139 | +} |
| 140 | + |
| 141 | +func TestProtoAsJSON(t *testing.T) { |
| 142 | + enabler := zap.NewAtomicLevelAt(zap.InfoLevel) |
| 143 | + core, logs := observer.New(enabler) |
| 144 | + |
| 145 | + logger, err := configureLogger(zap.New(core), "test") |
| 146 | + if err != nil { |
| 147 | + t.Fatal(err) |
| 148 | + } |
| 149 | + defer logger.Close() |
| 150 | + |
| 151 | + var val ProtoTest |
| 152 | + val.Foo = "bar" |
| 153 | + |
| 154 | + logger.With("proto_field", ProtoAsJSON(&val)).Info("ignore this message") |
| 155 | + |
| 156 | + if cnt := logs.Len(); cnt != 1 { |
| 157 | + t.Errorf("unexpected entry count: %d", cnt) |
| 158 | + t.FailNow() |
| 159 | + } |
| 160 | + |
| 161 | + entries := logs.TakeAll() |
| 162 | + |
| 163 | + if cnt := len(entries[0].Context); cnt != 2 { |
| 164 | + t.Errorf("unexpected field count: %d", cnt) |
| 165 | + t.FailNow() |
| 166 | + } |
| 167 | + |
| 168 | + if name := entries[0].Context[1].Key; name != "proto_field" { |
| 169 | + t.Errorf("unexpected field name: %s", name) |
| 170 | + } |
| 171 | + |
| 172 | + if typ := entries[0].Context[1].Type; typ != zapcore.ReflectType { |
| 173 | + t.Errorf("unexpected field type: %v", typ) |
| 174 | + } |
| 175 | +} |
0 commit comments