@@ -51,6 +51,18 @@ func TestGetFieldValue(t *testing.T) {
51
51
Path : "syntax" ,
52
52
Expectation : Expectation {Found : true , Val : "0" },
53
53
},
54
+ {
55
+ Name : "bool field" ,
56
+ Message : & apipb.Method {RequestStreaming : true },
57
+ Path : "request_streaming" ,
58
+ Expectation : Expectation {Found : true , Val : "t" },
59
+ },
60
+ {
61
+ Name : "empty bool field" ,
62
+ Message : & apipb.Method {},
63
+ Path : "request_streaming" ,
64
+ Expectation : Expectation {Found : true , Val : "f" },
65
+ },
54
66
{
55
67
Name : "non-existent field" ,
56
68
Message : & apipb.Api {},
@@ -79,6 +91,41 @@ func TestGetFieldValue(t *testing.T) {
79
91
}
80
92
}
81
93
94
+ func TestFieldAccessKey (t * testing.T ) {
95
+ type Expectation struct {
96
+ Val string
97
+ Err error
98
+ }
99
+ tests := []struct {
100
+ Name string
101
+ Message proto.Message
102
+ Key string
103
+ Expectation Expectation
104
+ }{
105
+ {
106
+ Name : "composite key" ,
107
+ Message : & apipb.Api {
108
+ SourceContext : & sourcecontextpb.SourceContext {
109
+ FileName : "bar" ,
110
+ },
111
+ Syntax : typepb .Syntax_SYNTAX_PROTO3 ,
112
+ },
113
+ Key : "source_context.file_name,syntax" ,
114
+ Expectation : Expectation {Val : "|bar|1" , Err : nil },
115
+ },
116
+ }
117
+ for _ , test := range tests {
118
+ t .Run (test .Name , func (t * testing.T ) {
119
+ var act Expectation
120
+ keyFn := fieldAccessKey (test .Key )
121
+ act .Val , act .Err = keyFn (test .Message )
122
+ if diff := cmp .Diff (test .Expectation , act ); diff != "" {
123
+ t .Errorf ("unexpected fieldAccessKey (-want +got):\n %s" , diff )
124
+ }
125
+ })
126
+ }
127
+ }
128
+
82
129
func BenchmarkGetFieldValue (b * testing.B ) {
83
130
msg := apipb.Api {
84
131
SourceContext : & sourcecontextpb.SourceContext {
@@ -92,3 +139,56 @@ func BenchmarkGetFieldValue(b *testing.B) {
92
139
getFieldValue (msgr , path )
93
140
}
94
141
}
142
+
143
+ func BenchmarkFieldAccessKey_String (b * testing.B ) {
144
+ msg := & apipb.Api {
145
+ Name : "bar" ,
146
+ }
147
+ keyFn := fieldAccessKey ("name" )
148
+ for n := 0 ; n < b .N ; n ++ {
149
+ if _ , err := keyFn (msg ); err != nil {
150
+ b .Logf ("failed to access key: %v" , err )
151
+ b .Fail ()
152
+ }
153
+ }
154
+ }
155
+
156
+ func BenchmarkFieldAccessKey_Enum (b * testing.B ) {
157
+ msg := & apipb.Api {
158
+ Syntax : typepb .Syntax_SYNTAX_PROTO3 ,
159
+ }
160
+ keyFn := fieldAccessKey ("syntax" )
161
+ for n := 0 ; n < b .N ; n ++ {
162
+ if _ , err := keyFn (msg ); err != nil {
163
+ b .Logf ("failed to access key: %v" , err )
164
+ b .Fail ()
165
+ }
166
+ }
167
+ }
168
+
169
+ func BenchmarkFieldAccessKey_Bool (b * testing.B ) {
170
+ msg := & apipb.Method {
171
+ RequestStreaming : true ,
172
+ }
173
+ keyFn := fieldAccessKey ("request_streaming" )
174
+ for n := 0 ; n < b .N ; n ++ {
175
+ if _ , err := keyFn (msg ); err != nil {
176
+ b .Logf ("failed to access key: %v" , err )
177
+ b .Fail ()
178
+ }
179
+ }
180
+ }
181
+
182
+ func BenchmarkFieldAccessKey_Composite (b * testing.B ) {
183
+ msg := & apipb.Method {
184
+ Name : "bar" ,
185
+ RequestStreaming : true ,
186
+ }
187
+ keyFn := fieldAccessKey ("name,request_streaming" )
188
+ for n := 0 ; n < b .N ; n ++ {
189
+ if _ , err := keyFn (msg ); err != nil {
190
+ b .Logf ("failed to access key: %v" , err )
191
+ b .Fail ()
192
+ }
193
+ }
194
+ }
0 commit comments