@@ -133,23 +133,34 @@ func (l *listGetter[K]) Get(ctx context.Context, tCtx K) (interface{}, error) {
133
133
return evaluated , nil
134
134
}
135
135
136
+ // TypeError represents that a value was not an expected type.
137
+ type TypeError string
138
+
139
+ func (t TypeError ) Error () string {
140
+ return string (t )
141
+ }
142
+
136
143
// StringGetter is a Getter that must return a string.
137
144
type StringGetter [K any ] interface {
138
- // Get retrieves a string value. If the value is not a string, an error is returned.
145
+ // Get retrieves a string value.
139
146
Get (ctx context.Context , tCtx K ) (string , error )
140
147
}
141
148
149
+ // StandardStringGetter is a basic implementation of StringGetter
142
150
type StandardStringGetter [K any ] struct {
143
151
Getter func (ctx context.Context , tCtx K ) (interface {}, error )
144
152
}
145
153
154
+ // Get retrieves a string value.
155
+ // If the value is not a string a new TypeError is returned.
156
+ // If there is an error getting the value it will be returned.
146
157
func (g StandardStringGetter [K ]) Get (ctx context.Context , tCtx K ) (string , error ) {
147
158
val , err := g .Getter (ctx , tCtx )
148
159
if err != nil {
149
- return "" , err
160
+ return "" , fmt . Errorf ( "error getting value in %T: %w" , g , err )
150
161
}
151
162
if val == nil {
152
- return "" , fmt . Errorf ("expected string but got nil" )
163
+ return "" , TypeError ("expected string but got nil" )
153
164
}
154
165
switch v := val .(type ) {
155
166
case string :
@@ -158,27 +169,33 @@ func (g StandardStringGetter[K]) Get(ctx context.Context, tCtx K) (string, error
158
169
if v .Type () == pcommon .ValueTypeStr {
159
170
return v .Str (), nil
160
171
}
161
- return "" , fmt .Errorf ("expected string but got %v" , v .Type ())
172
+ return "" , TypeError ( fmt .Sprintf ("expected string but got %v" , v .Type () ))
162
173
default :
163
- return "" , fmt .Errorf ("expected string but got %T" , val )
174
+ return "" , TypeError ( fmt .Sprintf ("expected string but got %T" , val ) )
164
175
}
165
176
}
166
177
178
+ // IntGetter is a Getter that must return an int64.
167
179
type IntGetter [K any ] interface {
180
+ // Get retrieves an int64 value.
168
181
Get (ctx context.Context , tCtx K ) (int64 , error )
169
182
}
170
183
184
+ // StandardIntGetter is a basic implementation of IntGetter
171
185
type StandardIntGetter [K any ] struct {
172
186
Getter func (ctx context.Context , tCtx K ) (interface {}, error )
173
187
}
174
188
189
+ // Get retrieves an int64 value.
190
+ // If the value is not an int64 a new TypeError is returned.
191
+ // If there is an error getting the value it will be returned.
175
192
func (g StandardIntGetter [K ]) Get (ctx context.Context , tCtx K ) (int64 , error ) {
176
193
val , err := g .Getter (ctx , tCtx )
177
194
if err != nil {
178
- return 0 , err
195
+ return 0 , fmt . Errorf ( "error getting value in %T: %w" , g , err )
179
196
}
180
197
if val == nil {
181
- return 0 , fmt . Errorf ("expected int64 but got nil" )
198
+ return 0 , TypeError ("expected int64 but got nil" )
182
199
}
183
200
switch v := val .(type ) {
184
201
case int64 :
@@ -187,27 +204,33 @@ func (g StandardIntGetter[K]) Get(ctx context.Context, tCtx K) (int64, error) {
187
204
if v .Type () == pcommon .ValueTypeInt {
188
205
return v .Int (), nil
189
206
}
190
- return 0 , fmt .Errorf ("expected int64 but got %v" , v .Type ())
207
+ return 0 , TypeError ( fmt .Sprintf ("expected int64 but got %v" , v .Type () ))
191
208
default :
192
- return 0 , fmt .Errorf ("expected int64 but got %T" , val )
209
+ return 0 , TypeError ( fmt .Sprintf ("expected int64 but got %T" , val ) )
193
210
}
194
211
}
195
212
213
+ // FloatGetter is a Getter that must return a float64.
196
214
type FloatGetter [K any ] interface {
215
+ // Get retrieves a float64 value.
197
216
Get (ctx context.Context , tCtx K ) (float64 , error )
198
217
}
199
218
219
+ // StandardFloatGetter is a basic implementation of FloatGetter
200
220
type StandardFloatGetter [K any ] struct {
201
221
Getter func (ctx context.Context , tCtx K ) (interface {}, error )
202
222
}
203
223
224
+ // Get retrieves a float64 value.
225
+ // If the value is not a float64 a new TypeError is returned.
226
+ // If there is an error getting the value it will be returned.
204
227
func (g StandardFloatGetter [K ]) Get (ctx context.Context , tCtx K ) (float64 , error ) {
205
228
val , err := g .Getter (ctx , tCtx )
206
229
if err != nil {
207
- return 0 , err
230
+ return 0 , fmt . Errorf ( "error getting value in %T: %w" , g , err )
208
231
}
209
232
if val == nil {
210
- return 0 , fmt . Errorf ("expected float64 but got nil" )
233
+ return 0 , TypeError ("expected float64 but got nil" )
211
234
}
212
235
switch v := val .(type ) {
213
236
case float64 :
@@ -216,27 +239,33 @@ func (g StandardFloatGetter[K]) Get(ctx context.Context, tCtx K) (float64, error
216
239
if v .Type () == pcommon .ValueTypeDouble {
217
240
return v .Double (), nil
218
241
}
219
- return 0 , fmt .Errorf ("expected float64 but got %v" , v .Type ())
242
+ return 0 , TypeError ( fmt .Sprintf ("expected float64 but got %v" , v .Type () ))
220
243
default :
221
- return 0 , fmt .Errorf ("expected float64 but got %T" , val )
244
+ return 0 , TypeError ( fmt .Sprintf ("expected float64 but got %T" , val ) )
222
245
}
223
246
}
224
247
248
+ // PMapGetter is a Getter that must return a pcommon.Map.
225
249
type PMapGetter [K any ] interface {
250
+ // Get retrieves a pcommon.Map value.
226
251
Get (ctx context.Context , tCtx K ) (pcommon.Map , error )
227
252
}
228
253
254
+ // StandardPMapGetter is a basic implementation of PMapGetter
229
255
type StandardPMapGetter [K any ] struct {
230
256
Getter func (ctx context.Context , tCtx K ) (interface {}, error )
231
257
}
232
258
259
+ // Get retrieves a pcommon.Map value.
260
+ // If the value is not a pcommon.Map a new TypeError is returned.
261
+ // If there is an error getting the value it will be returned.
233
262
func (g StandardPMapGetter [K ]) Get (ctx context.Context , tCtx K ) (pcommon.Map , error ) {
234
263
val , err := g .Getter (ctx , tCtx )
235
264
if err != nil {
236
- return pcommon.Map {}, err
265
+ return pcommon.Map {}, fmt . Errorf ( "error getting value in %T: %w" , g , err )
237
266
}
238
267
if val == nil {
239
- return pcommon.Map {}, fmt . Errorf ("expected pcommon.Map but got nil" )
268
+ return pcommon.Map {}, TypeError ("expected pcommon.Map but got nil" )
240
269
}
241
270
switch v := val .(type ) {
242
271
case pcommon.Map :
@@ -245,7 +274,7 @@ func (g StandardPMapGetter[K]) Get(ctx context.Context, tCtx K) (pcommon.Map, er
245
274
if v .Type () == pcommon .ValueTypeMap {
246
275
return v .Map (), nil
247
276
}
248
- return pcommon.Map {}, fmt .Errorf ("expected pcommon.Map but got %v" , v .Type ())
277
+ return pcommon.Map {}, TypeError ( fmt .Sprintf ("expected pcommon.Map but got %v" , v .Type () ))
249
278
case map [string ]any :
250
279
m := pcommon .NewMap ()
251
280
err = m .FromRaw (v )
@@ -254,7 +283,7 @@ func (g StandardPMapGetter[K]) Get(ctx context.Context, tCtx K) (pcommon.Map, er
254
283
}
255
284
return m , nil
256
285
default :
257
- return pcommon.Map {}, fmt .Errorf ("expected pcommon.Map but got %T" , val )
286
+ return pcommon.Map {}, TypeError ( fmt .Sprintf ("expected pcommon.Map but got %T" , val ) )
258
287
}
259
288
}
260
289
@@ -274,7 +303,7 @@ type StandardStringLikeGetter[K any] struct {
274
303
func (g StandardStringLikeGetter [K ]) Get (ctx context.Context , tCtx K ) (* string , error ) {
275
304
val , err := g .Getter (ctx , tCtx )
276
305
if err != nil {
277
- return nil , err
306
+ return nil , fmt . Errorf ( "error getting value in %T: %w" , g , err )
278
307
}
279
308
if val == nil {
280
309
return nil , nil
@@ -300,7 +329,7 @@ func (g StandardStringLikeGetter[K]) Get(ctx context.Context, tCtx K) (*string,
300
329
default :
301
330
result , err = jsoniter .MarshalToString (v )
302
331
if err != nil {
303
- return nil , fmt .Errorf ("unsupported type: %T" , v )
332
+ return nil , TypeError ( fmt .Sprintf ("unsupported type: %T" , v ) )
304
333
}
305
334
}
306
335
return & result , nil
@@ -322,7 +351,7 @@ type StandardFloatLikeGetter[K any] struct {
322
351
func (g StandardFloatLikeGetter [K ]) Get (ctx context.Context , tCtx K ) (* float64 , error ) {
323
352
val , err := g .Getter (ctx , tCtx )
324
353
if err != nil {
325
- return nil , err
354
+ return nil , fmt . Errorf ( "error getting value in %T: %w" , g , err )
326
355
}
327
356
if val == nil {
328
357
return nil , nil
@@ -362,10 +391,10 @@ func (g StandardFloatLikeGetter[K]) Get(ctx context.Context, tCtx K) (*float64,
362
391
result = float64 (0 )
363
392
}
364
393
default :
365
- return nil , fmt .Errorf ("unsupported value type: %v" , v .Type ())
394
+ return nil , TypeError ( fmt .Sprintf ("unsupported value type: %v" , v .Type () ))
366
395
}
367
396
default :
368
- return nil , fmt .Errorf ("unsupported type: %T" , v )
397
+ return nil , TypeError ( fmt .Sprintf ("unsupported type: %T" , v ) )
369
398
}
370
399
return & result , nil
371
400
}
@@ -386,7 +415,7 @@ type StandardIntLikeGetter[K any] struct {
386
415
func (g StandardIntLikeGetter [K ]) Get (ctx context.Context , tCtx K ) (* int64 , error ) {
387
416
val , err := g .Getter (ctx , tCtx )
388
417
if err != nil {
389
- return nil , err
418
+ return nil , fmt . Errorf ( "error getting value in %T: %w" , g , err )
390
419
}
391
420
if val == nil {
392
421
return nil , nil
@@ -426,10 +455,10 @@ func (g StandardIntLikeGetter[K]) Get(ctx context.Context, tCtx K) (*int64, erro
426
455
result = int64 (0 )
427
456
}
428
457
default :
429
- return nil , fmt .Errorf ("unsupported value type: %v" , v .Type ())
458
+ return nil , TypeError ( fmt .Sprintf ("unsupported value type: %v" , v .Type () ))
430
459
}
431
460
default :
432
- return nil , fmt .Errorf ("unsupported type: %T" , v )
461
+ return nil , TypeError ( fmt .Sprintf ("unsupported type: %T" , v ) )
433
462
}
434
463
return & result , nil
435
464
}
0 commit comments