Skip to content

Commit 9d7c0e9

Browse files
authored
feat(context): GetXxx added support for more go native types (#3633)
1 parent f2c861a commit 9d7c0e9

File tree

2 files changed

+294
-2
lines changed

2 files changed

+294
-2
lines changed

context.go

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,31 @@ func (c *Context) GetInt(key string) (i int) {
315315
return
316316
}
317317

318-
// GetInt64 returns the value associated with the key as an integer.
318+
// GetInt8 returns the value associated with the key as an integer 8.
319+
func (c *Context) GetInt8(key string) (i8 int8) {
320+
if val, ok := c.Get(key); ok && val != nil {
321+
i8, _ = val.(int8)
322+
}
323+
return
324+
}
325+
326+
// GetInt16 returns the value associated with the key as an integer 16.
327+
func (c *Context) GetInt16(key string) (i16 int16) {
328+
if val, ok := c.Get(key); ok && val != nil {
329+
i16, _ = val.(int16)
330+
}
331+
return
332+
}
333+
334+
// GetInt32 returns the value associated with the key as an integer 32.
335+
func (c *Context) GetInt32(key string) (i32 int32) {
336+
if val, ok := c.Get(key); ok && val != nil {
337+
i32, _ = val.(int32)
338+
}
339+
return
340+
}
341+
342+
// GetInt64 returns the value associated with the key as an integer 64.
319343
func (c *Context) GetInt64(key string) (i64 int64) {
320344
if val, ok := c.Get(key); ok && val != nil {
321345
i64, _ = val.(int64)
@@ -331,14 +355,46 @@ func (c *Context) GetUint(key string) (ui uint) {
331355
return
332356
}
333357

334-
// GetUint64 returns the value associated with the key as an unsigned integer.
358+
// GetUint8 returns the value associated with the key as an unsigned integer 8.
359+
func (c *Context) GetUint8(key string) (ui8 uint8) {
360+
if val, ok := c.Get(key); ok && val != nil {
361+
ui8, _ = val.(uint8)
362+
}
363+
return
364+
}
365+
366+
// GetUint16 returns the value associated with the key as an unsigned integer 16.
367+
func (c *Context) GetUint16(key string) (ui16 uint16) {
368+
if val, ok := c.Get(key); ok && val != nil {
369+
ui16, _ = val.(uint16)
370+
}
371+
return
372+
}
373+
374+
// GetUint32 returns the value associated with the key as an unsigned integer 32.
375+
func (c *Context) GetUint32(key string) (ui32 uint32) {
376+
if val, ok := c.Get(key); ok && val != nil {
377+
ui32, _ = val.(uint32)
378+
}
379+
return
380+
}
381+
382+
// GetUint64 returns the value associated with the key as an unsigned integer 64.
335383
func (c *Context) GetUint64(key string) (ui64 uint64) {
336384
if val, ok := c.Get(key); ok && val != nil {
337385
ui64, _ = val.(uint64)
338386
}
339387
return
340388
}
341389

390+
// GetFloat32 returns the value associated with the key as a float32.
391+
func (c *Context) GetFloat32(key string) (f32 float32) {
392+
if val, ok := c.Get(key); ok && val != nil {
393+
f32, _ = val.(float32)
394+
}
395+
return
396+
}
397+
342398
// GetFloat64 returns the value associated with the key as a float64.
343399
func (c *Context) GetFloat64(key string) (f64 float64) {
344400
if val, ok := c.Get(key); ok && val != nil {
@@ -363,6 +419,90 @@ func (c *Context) GetDuration(key string) (d time.Duration) {
363419
return
364420
}
365421

422+
func (c *Context) GetIntSlice(key string) (is []int) {
423+
if val, ok := c.Get(key); ok && val != nil {
424+
is, _ = val.([]int)
425+
}
426+
return
427+
}
428+
429+
func (c *Context) GetInt8Slice(key string) (i8s []int8) {
430+
if val, ok := c.Get(key); ok && val != nil {
431+
i8s, _ = val.([]int8)
432+
}
433+
return
434+
}
435+
436+
func (c *Context) GetInt16Slice(key string) (i16s []int16) {
437+
if val, ok := c.Get(key); ok && val != nil {
438+
i16s, _ = val.([]int16)
439+
}
440+
return
441+
}
442+
443+
func (c *Context) GetInt32Slice(key string) (i32s []int32) {
444+
if val, ok := c.Get(key); ok && val != nil {
445+
i32s, _ = val.([]int32)
446+
}
447+
return
448+
}
449+
450+
func (c *Context) GetInt64Slice(key string) (i64s []int64) {
451+
if val, ok := c.Get(key); ok && val != nil {
452+
i64s, _ = val.([]int64)
453+
}
454+
return
455+
}
456+
457+
func (c *Context) GetUintSlice(key string) (uis []uint) {
458+
if val, ok := c.Get(key); ok && val != nil {
459+
uis, _ = val.([]uint)
460+
}
461+
return
462+
}
463+
464+
func (c *Context) GetUint8Slice(key string) (ui8s []uint8) {
465+
if val, ok := c.Get(key); ok && val != nil {
466+
ui8s, _ = val.([]uint8)
467+
}
468+
return
469+
}
470+
471+
func (c *Context) GetUint16Slice(key string) (ui16s []uint16) {
472+
if val, ok := c.Get(key); ok && val != nil {
473+
ui16s, _ = val.([]uint16)
474+
}
475+
return
476+
}
477+
478+
func (c *Context) GetUint32Slice(key string) (ui32s []uint32) {
479+
if val, ok := c.Get(key); ok && val != nil {
480+
ui32s, _ = val.([]uint32)
481+
}
482+
return
483+
}
484+
485+
func (c *Context) GetUint64Slice(key string) (ui64s []uint64) {
486+
if val, ok := c.Get(key); ok && val != nil {
487+
ui64s, _ = val.([]uint64)
488+
}
489+
return
490+
}
491+
492+
func (c *Context) GetFloat32Slice(key string) (f32s []float32) {
493+
if val, ok := c.Get(key); ok && val != nil {
494+
f32s, _ = val.([]float32)
495+
}
496+
return
497+
}
498+
499+
func (c *Context) GetFloat64Slice(key string) (f64s []float64) {
500+
if val, ok := c.Get(key); ok && val != nil {
501+
f64s, _ = val.([]float64)
502+
}
503+
return
504+
}
505+
366506
// GetStringSlice returns the value associated with the key as a slice of strings.
367507
func (c *Context) GetStringSlice(key string) (ss []string) {
368508
if val, ok := c.Get(key); ok && val != nil {

context_test.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,30 @@ func TestContextGetInt(t *testing.T) {
252252
assert.Equal(t, 1, c.GetInt("int"))
253253
}
254254

255+
func TestContextGetInt8(t *testing.T) {
256+
c, _ := CreateTestContext(httptest.NewRecorder())
257+
key := "int8"
258+
value := int8(0x7F)
259+
c.Set(key, value)
260+
assert.Equal(t, value, c.GetInt8(key))
261+
}
262+
263+
func TestContextGetInt16(t *testing.T) {
264+
c, _ := CreateTestContext(httptest.NewRecorder())
265+
key := "int16"
266+
value := int16(0x7FFF)
267+
c.Set(key, value)
268+
assert.Equal(t, value, c.GetInt16(key))
269+
}
270+
271+
func TestContextGetInt32(t *testing.T) {
272+
c, _ := CreateTestContext(httptest.NewRecorder())
273+
key := "int32"
274+
value := int32(0x7FFFFFFF)
275+
c.Set(key, value)
276+
assert.Equal(t, value, c.GetInt32(key))
277+
}
278+
255279
func TestContextGetInt64(t *testing.T) {
256280
c, _ := CreateTestContext(httptest.NewRecorder())
257281
c.Set("int64", int64(42424242424242))
@@ -264,12 +288,44 @@ func TestContextGetUint(t *testing.T) {
264288
assert.Equal(t, uint(1), c.GetUint("uint"))
265289
}
266290

291+
func TestContextGetUint8(t *testing.T) {
292+
c, _ := CreateTestContext(httptest.NewRecorder())
293+
key := "uint8"
294+
value := uint8(0xFF)
295+
c.Set(key, value)
296+
assert.Equal(t, value, c.GetUint8(key))
297+
}
298+
299+
func TestContextGetUint16(t *testing.T) {
300+
c, _ := CreateTestContext(httptest.NewRecorder())
301+
key := "uint16"
302+
value := uint16(0xFFFF)
303+
c.Set(key, value)
304+
assert.Equal(t, value, c.GetUint16(key))
305+
}
306+
307+
func TestContextGetUint32(t *testing.T) {
308+
c, _ := CreateTestContext(httptest.NewRecorder())
309+
key := "uint32"
310+
value := uint32(0xFFFFFFFF)
311+
c.Set(key, value)
312+
assert.Equal(t, value, c.GetUint32(key))
313+
}
314+
267315
func TestContextGetUint64(t *testing.T) {
268316
c, _ := CreateTestContext(httptest.NewRecorder())
269317
c.Set("uint64", uint64(18446744073709551615))
270318
assert.Equal(t, uint64(18446744073709551615), c.GetUint64("uint64"))
271319
}
272320

321+
func TestContextGetFloat32(t *testing.T) {
322+
c, _ := CreateTestContext(httptest.NewRecorder())
323+
key := "float32"
324+
value := float32(3.14)
325+
c.Set(key, value)
326+
assert.Equal(t, value, c.GetFloat32(key))
327+
}
328+
273329
func TestContextGetFloat64(t *testing.T) {
274330
c, _ := CreateTestContext(httptest.NewRecorder())
275331
c.Set("float64", 4.2)
@@ -289,6 +345,102 @@ func TestContextGetDuration(t *testing.T) {
289345
assert.Equal(t, time.Second, c.GetDuration("duration"))
290346
}
291347

348+
func TestContextGetIntSlice(t *testing.T) {
349+
c, _ := CreateTestContext(httptest.NewRecorder())
350+
key := "int-slice"
351+
value := []int{1, 2}
352+
c.Set(key, value)
353+
assert.Equal(t, value, c.GetIntSlice(key))
354+
}
355+
356+
func TestContextGetInt8Slice(t *testing.T) {
357+
c, _ := CreateTestContext(httptest.NewRecorder())
358+
key := "int8-slice"
359+
value := []int8{1, 2}
360+
c.Set(key, value)
361+
assert.Equal(t, value, c.GetInt8Slice(key))
362+
}
363+
364+
func TestContextGetInt16Slice(t *testing.T) {
365+
c, _ := CreateTestContext(httptest.NewRecorder())
366+
key := "int16-slice"
367+
value := []int16{1, 2}
368+
c.Set(key, value)
369+
assert.Equal(t, value, c.GetInt16Slice(key))
370+
}
371+
372+
func TestContextGetInt32Slice(t *testing.T) {
373+
c, _ := CreateTestContext(httptest.NewRecorder())
374+
key := "int32-slice"
375+
value := []int32{1, 2}
376+
c.Set(key, value)
377+
assert.Equal(t, value, c.GetInt32Slice(key))
378+
}
379+
380+
func TestContextGetInt64Slice(t *testing.T) {
381+
c, _ := CreateTestContext(httptest.NewRecorder())
382+
key := "int64-slice"
383+
value := []int64{1, 2}
384+
c.Set(key, value)
385+
assert.Equal(t, value, c.GetInt64Slice(key))
386+
}
387+
388+
func TestContextGetUintSlice(t *testing.T) {
389+
c, _ := CreateTestContext(httptest.NewRecorder())
390+
key := "uint-slice"
391+
value := []uint{1, 2}
392+
c.Set(key, value)
393+
assert.Equal(t, value, c.GetUintSlice(key))
394+
}
395+
396+
func TestContextGetUint8Slice(t *testing.T) {
397+
c, _ := CreateTestContext(httptest.NewRecorder())
398+
key := "uint8-slice"
399+
value := []uint8{1, 2}
400+
c.Set(key, value)
401+
assert.Equal(t, value, c.GetUint8Slice(key))
402+
}
403+
404+
func TestContextGetUint16Slice(t *testing.T) {
405+
c, _ := CreateTestContext(httptest.NewRecorder())
406+
key := "uint16-slice"
407+
value := []uint16{1, 2}
408+
c.Set(key, value)
409+
assert.Equal(t, value, c.GetUint16Slice(key))
410+
}
411+
412+
func TestContextGetUint32Slice(t *testing.T) {
413+
c, _ := CreateTestContext(httptest.NewRecorder())
414+
key := "uint32-slice"
415+
value := []uint32{1, 2}
416+
c.Set(key, value)
417+
assert.Equal(t, value, c.GetUint32Slice(key))
418+
}
419+
420+
func TestContextGetUint64Slice(t *testing.T) {
421+
c, _ := CreateTestContext(httptest.NewRecorder())
422+
key := "uint64-slice"
423+
value := []uint64{1, 2}
424+
c.Set(key, value)
425+
assert.Equal(t, value, c.GetUint64Slice(key))
426+
}
427+
428+
func TestContextGetFloat32Slice(t *testing.T) {
429+
c, _ := CreateTestContext(httptest.NewRecorder())
430+
key := "float32-slice"
431+
value := []float32{1, 2}
432+
c.Set(key, value)
433+
assert.Equal(t, value, c.GetFloat32Slice(key))
434+
}
435+
436+
func TestContextGetFloat64Slice(t *testing.T) {
437+
c, _ := CreateTestContext(httptest.NewRecorder())
438+
key := "float64-slice"
439+
value := []float64{1, 2}
440+
c.Set(key, value)
441+
assert.Equal(t, value, c.GetFloat64Slice(key))
442+
}
443+
292444
func TestContextGetStringSlice(t *testing.T) {
293445
c, _ := CreateTestContext(httptest.NewRecorder())
294446
c.Set("slice", []string{"foo"})

0 commit comments

Comments
 (0)