-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreflect_test.go
171 lines (158 loc) · 4.07 KB
/
reflect_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package rfc5424
import (
"reflect"
"time"
. "gopkg.in/check.v1"
)
var _ = Suite(&ReflectTest{})
type ReflectTest struct {
}
type struct1 struct {
Severity Severity `log:"error"`
Facility Facility `log:"local2"`
Timestamp time.Time
Hostname string
AppName string `log:"myAppName"`
ProcessID int
MessageID string
Message []byte
MyCustomInt int `log:"9999@custom"`
MyCustomString string
MyCustomBool bool
myUnexportedValue string
myUnexportedTaggedValue string `log:"myUnexportedTaggedValue"`
}
var expectedReflection1 = reflection{
Type: reflect.TypeOf(struct1{}),
SeverityFieldIndex: 0,
SeverityDefault: Error,
FacilityFieldIndex: 1,
FacilityDefault: Local2,
TimestampFieldIndex: 2,
HostnameFieldIndex: 3,
AppNameFieldIndex: 4,
AppNameDefault: "myAppName",
ProcessIDFieldIndex: 5,
MessageIDFieldIndex: 6,
MessageIDDefault: "struct1",
MessageFieldIndex: 7,
StructuredDataFieldReflections: []structuredDataFieldReflection{
structuredDataFieldReflection{
FieldIndex: 8,
FieldName: "myCustomInt",
SdID: "9999@custom",
},
structuredDataFieldReflection{
FieldIndex: 9,
FieldName: "myCustomString",
SdID: "0@local",
},
structuredDataFieldReflection{
FieldIndex: 10,
FieldName: "myCustomBool",
SdID: "0@local",
},
structuredDataFieldReflection{
FieldIndex: 12,
FieldName: "myUnexportedTaggedValue",
SdID: "0@local",
},
},
}
type struct2 struct {
SDID int `log:"1234@demo"`
MyCustomInt int
MyCustomString string
MyCustomBool bool
myUnexportedValue string
myUnexportedTaggedValue string `log:"5516@sbc myUnexportedTaggedValue"`
}
var expectedReflection2 = reflection{
Type: reflect.TypeOf(struct2{}),
SeverityFieldIndex: -1,
SeverityDefault: Info,
FacilityFieldIndex: -1,
FacilityDefault: Local0,
TimestampFieldIndex: -1,
HostnameFieldIndex: -1,
AppNameFieldIndex: -1,
AppNameDefault: "rfc5424.test",
ProcessIDFieldIndex: -1,
MessageIDFieldIndex: -1,
MessageIDDefault: "struct2",
MessageFieldIndex: -1,
SDIDDefault: "1234@demo",
StructuredDataFieldReflections: []structuredDataFieldReflection{
structuredDataFieldReflection{
FieldIndex: 1,
FieldName: "myCustomInt",
SdID: "1234@demo",
},
structuredDataFieldReflection{
FieldIndex: 2,
FieldName: "myCustomString",
SdID: "1234@demo",
},
structuredDataFieldReflection{
FieldIndex: 3,
FieldName: "myCustomBool",
SdID: "1234@demo",
},
structuredDataFieldReflection{
FieldIndex: 5,
FieldName: "myUnexportedTaggedValue",
SdID: "5516@sbc",
},
},
}
type struct3 struct {
Severity Severity
Facility Facility
Timestamp time.Time
Hostname string
AppName string
ProcessID int
MessageID string
Message []byte // To test the tag below
RealMessage []byte `log:",message"`
MyCustomString string
}
var expectedReflection3 = reflection{
Type: reflect.TypeOf(struct3{}),
SeverityFieldIndex: 0,
SeverityDefault: Info,
FacilityFieldIndex: 1,
FacilityDefault: Local0,
TimestampFieldIndex: 2,
HostnameFieldIndex: 3,
AppNameFieldIndex: 4,
AppNameDefault: "rfc5424.test",
ProcessIDFieldIndex: 5,
MessageIDFieldIndex: 6,
MessageIDDefault: "struct3",
MessageFieldIndex: 8,
StructuredDataFieldReflections: []structuredDataFieldReflection{
structuredDataFieldReflection{
FieldIndex: 9,
FieldName: "myCustomString",
SdID: "0@local",
},
},
}
func (s *ReflectTest) TestCanReflect(c *C) {
{
v := struct1{}
r := Reflect(reflect.TypeOf(v))
c.Assert(r, DeepEquals, &expectedReflection1)
}
{
v := struct2{}
r := Reflect(reflect.TypeOf(v))
c.Assert(r, DeepEquals, &expectedReflection2)
}
{
v := struct3{}
r := Reflect(reflect.TypeOf(v))
c.Assert(r, DeepEquals, &expectedReflection3)
}
}