@@ -291,248 +291,171 @@ func (c *Context) MustGet(key string) any {
291291 panic ("Key \" " + key + "\" does not exist" )
292292}
293293
294- // GetString returns the value associated with the key as a string.
295- func (c * Context ) GetString (key string ) (s string ) {
294+ func getTyped [T any ](c * Context , key string ) (res T ) {
296295 if val , ok := c .Get (key ); ok && val != nil {
297- s , _ = val .(string )
296+ res , _ = val .(T )
298297 }
299298 return
300299}
301300
301+ // GetString returns the value associated with the key as a string.
302+ func (c * Context ) GetString (key string ) (s string ) {
303+ return getTyped [string ](c , key )
304+ }
305+
302306// GetBool returns the value associated with the key as a boolean.
303307func (c * Context ) GetBool (key string ) (b bool ) {
304- if val , ok := c .Get (key ); ok && val != nil {
305- b , _ = val .(bool )
306- }
307- return
308+ return getTyped [bool ](c , key )
308309}
309310
310311// GetInt returns the value associated with the key as an integer.
311312func (c * Context ) GetInt (key string ) (i int ) {
312- if val , ok := c .Get (key ); ok && val != nil {
313- i , _ = val .(int )
314- }
315- return
313+ return getTyped [int ](c , key )
316314}
317315
318316// GetInt8 returns the value associated with the key as an integer 8.
319317func (c * Context ) GetInt8 (key string ) (i8 int8 ) {
320- if val , ok := c .Get (key ); ok && val != nil {
321- i8 , _ = val .(int8 )
322- }
323- return
318+ return getTyped [int8 ](c , key )
324319}
325320
326321// GetInt16 returns the value associated with the key as an integer 16.
327322func (c * Context ) GetInt16 (key string ) (i16 int16 ) {
328- if val , ok := c .Get (key ); ok && val != nil {
329- i16 , _ = val .(int16 )
330- }
331- return
323+ return getTyped [int16 ](c , key )
332324}
333325
334326// GetInt32 returns the value associated with the key as an integer 32.
335327func (c * Context ) GetInt32 (key string ) (i32 int32 ) {
336- if val , ok := c .Get (key ); ok && val != nil {
337- i32 , _ = val .(int32 )
338- }
339- return
328+ return getTyped [int32 ](c , key )
340329}
341330
342331// GetInt64 returns the value associated with the key as an integer 64.
343332func (c * Context ) GetInt64 (key string ) (i64 int64 ) {
344- if val , ok := c .Get (key ); ok && val != nil {
345- i64 , _ = val .(int64 )
346- }
347- return
333+ return getTyped [int64 ](c , key )
348334}
349335
350336// GetUint returns the value associated with the key as an unsigned integer.
351337func (c * Context ) GetUint (key string ) (ui uint ) {
352- if val , ok := c .Get (key ); ok && val != nil {
353- ui , _ = val .(uint )
354- }
355- return
338+ return getTyped [uint ](c , key )
356339}
357340
358341// GetUint8 returns the value associated with the key as an unsigned integer 8.
359342func (c * Context ) GetUint8 (key string ) (ui8 uint8 ) {
360- if val , ok := c .Get (key ); ok && val != nil {
361- ui8 , _ = val .(uint8 )
362- }
363- return
343+ return getTyped [uint8 ](c , key )
364344}
365345
366346// GetUint16 returns the value associated with the key as an unsigned integer 16.
367347func (c * Context ) GetUint16 (key string ) (ui16 uint16 ) {
368- if val , ok := c .Get (key ); ok && val != nil {
369- ui16 , _ = val .(uint16 )
370- }
371- return
348+ return getTyped [uint16 ](c , key )
372349}
373350
374351// GetUint32 returns the value associated with the key as an unsigned integer 32.
375352func (c * Context ) GetUint32 (key string ) (ui32 uint32 ) {
376- if val , ok := c .Get (key ); ok && val != nil {
377- ui32 , _ = val .(uint32 )
378- }
379- return
353+ return getTyped [uint32 ](c , key )
380354}
381355
382356// GetUint64 returns the value associated with the key as an unsigned integer 64.
383357func (c * Context ) GetUint64 (key string ) (ui64 uint64 ) {
384- if val , ok := c .Get (key ); ok && val != nil {
385- ui64 , _ = val .(uint64 )
386- }
387- return
358+ return getTyped [uint64 ](c , key )
388359}
389360
390361// GetFloat32 returns the value associated with the key as a float32.
391362func (c * Context ) GetFloat32 (key string ) (f32 float32 ) {
392- if val , ok := c .Get (key ); ok && val != nil {
393- f32 , _ = val .(float32 )
394- }
395- return
363+ return getTyped [float32 ](c , key )
396364}
397365
398366// GetFloat64 returns the value associated with the key as a float64.
399367func (c * Context ) GetFloat64 (key string ) (f64 float64 ) {
400- if val , ok := c .Get (key ); ok && val != nil {
401- f64 , _ = val .(float64 )
402- }
403- return
368+ return getTyped [float64 ](c , key )
404369}
405370
406371// GetTime returns the value associated with the key as time.
407372func (c * Context ) GetTime (key string ) (t time.Time ) {
408- if val , ok := c .Get (key ); ok && val != nil {
409- t , _ = val .(time.Time )
410- }
411- return
373+ return getTyped [time.Time ](c , key )
412374}
413375
414376// GetDuration returns the value associated with the key as a duration.
415377func (c * Context ) GetDuration (key string ) (d time.Duration ) {
416- if val , ok := c .Get (key ); ok && val != nil {
417- d , _ = val .(time.Duration )
418- }
419- return
378+ return getTyped [time.Duration ](c , key )
420379}
421380
381+ // GetIntSlice returns the value associated with the key as a slice of integers.
422382func (c * Context ) GetIntSlice (key string ) (is []int ) {
423- if val , ok := c .Get (key ); ok && val != nil {
424- is , _ = val .([]int )
425- }
426- return
383+ return getTyped [[]int ](c , key )
427384}
428385
386+ // GetInt8Slice returns the value associated with the key as a slice of int8 integers.
429387func (c * Context ) GetInt8Slice (key string ) (i8s []int8 ) {
430- if val , ok := c .Get (key ); ok && val != nil {
431- i8s , _ = val .([]int8 )
432- }
433- return
388+ return getTyped [[]int8 ](c , key )
434389}
435390
391+ // GetInt16Slice returns the value associated with the key as a slice of int16 integers.
436392func (c * Context ) GetInt16Slice (key string ) (i16s []int16 ) {
437- if val , ok := c .Get (key ); ok && val != nil {
438- i16s , _ = val .([]int16 )
439- }
440- return
393+ return getTyped [[]int16 ](c , key )
441394}
442395
396+ // GetInt32Slice returns the value associated with the key as a slice of int32 integers.
443397func (c * Context ) GetInt32Slice (key string ) (i32s []int32 ) {
444- if val , ok := c .Get (key ); ok && val != nil {
445- i32s , _ = val .([]int32 )
446- }
447- return
398+ return getTyped [[]int32 ](c , key )
448399}
449400
401+ // GetInt64Slice returns the value associated with the key as a slice of int64 integers.
450402func (c * Context ) GetInt64Slice (key string ) (i64s []int64 ) {
451- if val , ok := c .Get (key ); ok && val != nil {
452- i64s , _ = val .([]int64 )
453- }
454- return
403+ return getTyped [[]int64 ](c , key )
455404}
456405
406+ // GetUintSlice returns the value associated with the key as a slice of unsigned integers.
457407func (c * Context ) GetUintSlice (key string ) (uis []uint ) {
458- if val , ok := c .Get (key ); ok && val != nil {
459- uis , _ = val .([]uint )
460- }
461- return
408+ return getTyped [[]uint ](c , key )
462409}
463410
411+ // GetUint8Slice returns the value associated with the key as a slice of uint8 integers.
464412func (c * Context ) GetUint8Slice (key string ) (ui8s []uint8 ) {
465- if val , ok := c .Get (key ); ok && val != nil {
466- ui8s , _ = val .([]uint8 )
467- }
468- return
413+ return getTyped [[]uint8 ](c , key )
469414}
470415
416+ // GetUint16Slice returns the value associated with the key as a slice of uint16 integers.
471417func (c * Context ) GetUint16Slice (key string ) (ui16s []uint16 ) {
472- if val , ok := c .Get (key ); ok && val != nil {
473- ui16s , _ = val .([]uint16 )
474- }
475- return
418+ return getTyped [[]uint16 ](c , key )
476419}
477420
421+ // GetUint32Slice returns the value associated with the key as a slice of uint32 integers.
478422func (c * Context ) GetUint32Slice (key string ) (ui32s []uint32 ) {
479- if val , ok := c .Get (key ); ok && val != nil {
480- ui32s , _ = val .([]uint32 )
481- }
482- return
423+ return getTyped [[]uint32 ](c , key )
483424}
484425
426+ // GetUint64Slice returns the value associated with the key as a slice of uint64 integers.
485427func (c * Context ) GetUint64Slice (key string ) (ui64s []uint64 ) {
486- if val , ok := c .Get (key ); ok && val != nil {
487- ui64s , _ = val .([]uint64 )
488- }
489- return
428+ return getTyped [[]uint64 ](c , key )
490429}
491430
431+ // GetFloat32Slice returns the value associated with the key as a slice of float32 numbers.
492432func (c * Context ) GetFloat32Slice (key string ) (f32s []float32 ) {
493- if val , ok := c .Get (key ); ok && val != nil {
494- f32s , _ = val .([]float32 )
495- }
496- return
433+ return getTyped [[]float32 ](c , key )
497434}
498435
436+ // GetFloat64Slice returns the value associated with the key as a slice of float64 numbers.
499437func (c * Context ) GetFloat64Slice (key string ) (f64s []float64 ) {
500- if val , ok := c .Get (key ); ok && val != nil {
501- f64s , _ = val .([]float64 )
502- }
503- return
438+ return getTyped [[]float64 ](c , key )
504439}
505440
506441// GetStringSlice returns the value associated with the key as a slice of strings.
507442func (c * Context ) GetStringSlice (key string ) (ss []string ) {
508- if val , ok := c .Get (key ); ok && val != nil {
509- ss , _ = val .([]string )
510- }
511- return
443+ return getTyped [[]string ](c , key )
512444}
513445
514446// GetStringMap returns the value associated with the key as a map of interfaces.
515447func (c * Context ) GetStringMap (key string ) (sm map [string ]any ) {
516- if val , ok := c .Get (key ); ok && val != nil {
517- sm , _ = val .(map [string ]any )
518- }
519- return
448+ return getTyped [map [string ]any ](c , key )
520449}
521450
522451// GetStringMapString returns the value associated with the key as a map of strings.
523452func (c * Context ) GetStringMapString (key string ) (sms map [string ]string ) {
524- if val , ok := c .Get (key ); ok && val != nil {
525- sms , _ = val .(map [string ]string )
526- }
527- return
453+ return getTyped [map [string ]string ](c , key )
528454}
529455
530456// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
531457func (c * Context ) GetStringMapStringSlice (key string ) (smss map [string ][]string ) {
532- if val , ok := c .Get (key ); ok && val != nil {
533- smss , _ = val .(map [string ][]string )
534- }
535- return
458+ return getTyped [map [string ][]string ](c , key )
536459}
537460
538461/************************************/
0 commit comments