mirror of https://github.com/gofiber/fiber.git
Add missing helpers for golang built-in types
parent
99d8db951a
commit
72992999ed
252
state.go
252
state.go
|
@ -36,54 +36,6 @@ func (s *State) MustGet(key string) any {
|
|||
panic("state: dependency not found!")
|
||||
}
|
||||
|
||||
// GetString retrieves a string value from the State.
|
||||
// It returns the string and a boolean indicating successful type assertion.
|
||||
func (s *State) GetString(key string) (string, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
depString, okCast := dep.(string)
|
||||
return depString, okCast
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
// GetInt retrieves an integer value from the State.
|
||||
// It returns the int and a boolean indicating successful type assertion.
|
||||
func (s *State) GetInt(key string) (int, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
depInt, okCast := dep.(int)
|
||||
return depInt, okCast
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetBool retrieves a boolean value from the State.
|
||||
// It returns the bool and a boolean indicating successful type assertion.
|
||||
func (s *State) GetBool(key string) (value, ok bool) { //nolint:nonamedreturns // Better idea to use named returns here
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
depBool, okCast := dep.(bool)
|
||||
return depBool, okCast
|
||||
}
|
||||
|
||||
return false, false
|
||||
}
|
||||
|
||||
// GetFloat64 retrieves a float64 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetFloat64(key string) (float64, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
depFloat64, okCast := dep.(float64)
|
||||
return depFloat64, okCast
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// Has checks if a key is present in the State.
|
||||
// It returns a boolean indicating if the key is present.
|
||||
func (s *State) Has(key string) bool {
|
||||
|
@ -164,3 +116,207 @@ func GetStateWithDefault[T any](s *State, key string, defaultVal T) T {
|
|||
|
||||
return dep
|
||||
}
|
||||
|
||||
// GetString retrieves a string value from the State.
|
||||
// It returns the string and a boolean indicating successful type assertion.
|
||||
func (s *State) GetString(key string) (string, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
depString, okCast := dep.(string)
|
||||
return depString, okCast
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
|
||||
// GetInt retrieves an integer value from the State.
|
||||
// It returns the int and a boolean indicating successful type assertion.
|
||||
func (s *State) GetInt(key string) (int, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
depInt, okCast := dep.(int)
|
||||
return depInt, okCast
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetBool retrieves a boolean value from the State.
|
||||
// It returns the bool and a boolean indicating successful type assertion.
|
||||
func (s *State) GetBool(key string) (value, ok bool) { //nolint:nonamedreturns // Better idea to use named returns here
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
depBool, okCast := dep.(bool)
|
||||
return depBool, okCast
|
||||
}
|
||||
|
||||
return false, false
|
||||
}
|
||||
|
||||
// GetFloat64 retrieves a float64 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetFloat64(key string) (float64, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
depFloat64, okCast := dep.(float64)
|
||||
return depFloat64, okCast
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetUint retrieves a uint value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetUint(key string) (uint, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depUint, okCast := dep.(uint); okCast {
|
||||
return depUint, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetInt8 retrieves an int8 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetInt8(key string) (int8, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depInt8, okCast := dep.(int8); okCast {
|
||||
return depInt8, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetInt16 retrieves an int16 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetInt16(key string) (int16, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depInt16, okCast := dep.(int16); okCast {
|
||||
return depInt16, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetInt32 retrieves an int32 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetInt32(key string) (int32, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depInt32, okCast := dep.(int32); okCast {
|
||||
return depInt32, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetInt64 retrieves an int64 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetInt64(key string) (int64, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depInt64, okCast := dep.(int64); okCast {
|
||||
return depInt64, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetUint8 retrieves a uint8 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetUint8(key string) (uint8, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depUint8, okCast := dep.(uint8); okCast {
|
||||
return depUint8, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetUint16 retrieves a uint16 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetUint16(key string) (uint16, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depUint16, okCast := dep.(uint16); okCast {
|
||||
return depUint16, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetUint32 retrieves a uint32 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetUint32(key string) (uint32, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depUint32, okCast := dep.(uint32); okCast {
|
||||
return depUint32, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetUint64 retrieves a uint64 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetUint64(key string) (uint64, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depUint64, okCast := dep.(uint64); okCast {
|
||||
return depUint64, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetUintptr retrieves a uintptr value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetUintptr(key string) (uintptr, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depUintptr, okCast := dep.(uintptr); okCast {
|
||||
return depUintptr, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetFloat32 retrieves a float32 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetFloat32(key string) (float32, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depFloat32, okCast := dep.(float32); okCast {
|
||||
return depFloat32, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetComplex64 retrieves a complex64 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetComplex64(key string) (complex64, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depComplex64, okCast := dep.(complex64); okCast {
|
||||
return depComplex64, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// GetComplex128 retrieves a complex128 value from the State.
|
||||
// It returns the float64 and a boolean indicating successful type assertion.
|
||||
func (s *State) GetComplex128(key string) (complex128, bool) {
|
||||
dep, ok := s.Get(key)
|
||||
if ok {
|
||||
if depComplex128, okCast := dep.(complex128); okCast {
|
||||
return depComplex128, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
250
state_test.go
250
state_test.go
|
@ -106,6 +106,256 @@ func TestState_GetFloat64(t *testing.T) {
|
|||
require.InDelta(t, 0.0, f, 0.0001)
|
||||
}
|
||||
|
||||
func TestState_GetUint(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
st.Set("uint", uint(100))
|
||||
u, ok := st.GetUint("uint")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, uint(100), u)
|
||||
|
||||
st.Set("wrong", "not uint")
|
||||
u, ok = st.GetUint("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uint(0), u)
|
||||
|
||||
u, ok = st.GetUint("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uint(0), u)
|
||||
}
|
||||
|
||||
func TestState_GetInt8(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
st.Set("int8", int8(10))
|
||||
i, ok := st.GetInt8("int8")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, int8(10), i)
|
||||
|
||||
st.Set("wrong", "not int8")
|
||||
i, ok = st.GetInt8("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, int8(0), i)
|
||||
|
||||
i, ok = st.GetInt8("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, int8(0), i)
|
||||
}
|
||||
|
||||
func TestState_GetInt16(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
st.Set("int16", int16(200))
|
||||
i, ok := st.GetInt16("int16")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, int16(200), i)
|
||||
|
||||
st.Set("wrong", "not int16")
|
||||
i, ok = st.GetInt16("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, int16(0), i)
|
||||
|
||||
i, ok = st.GetInt16("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, int16(0), i)
|
||||
}
|
||||
|
||||
func TestState_GetInt32(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
st.Set("int32", int32(3000))
|
||||
i, ok := st.GetInt32("int32")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, int32(3000), i)
|
||||
|
||||
st.Set("wrong", "not int32")
|
||||
i, ok = st.GetInt32("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, int32(0), i)
|
||||
|
||||
i, ok = st.GetInt32("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, int32(0), i)
|
||||
}
|
||||
|
||||
func TestState_GetInt64(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
st.Set("int64", int64(4000))
|
||||
i, ok := st.GetInt64("int64")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, int64(4000), i)
|
||||
|
||||
st.Set("wrong", "not int64")
|
||||
i, ok = st.GetInt64("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, int64(0), i)
|
||||
|
||||
i, ok = st.GetInt64("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, int64(0), i)
|
||||
}
|
||||
|
||||
func TestState_GetUint8(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
st.Set("uint8", uint8(20))
|
||||
u, ok := st.GetUint8("uint8")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, uint8(20), u)
|
||||
|
||||
st.Set("wrong", "not uint8")
|
||||
u, ok = st.GetUint8("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uint8(0), u)
|
||||
|
||||
u, ok = st.GetUint8("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uint8(0), u)
|
||||
}
|
||||
|
||||
func TestState_GetUint16(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
st.Set("uint16", uint16(300))
|
||||
u, ok := st.GetUint16("uint16")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, uint16(300), u)
|
||||
|
||||
st.Set("wrong", "not uint16")
|
||||
u, ok = st.GetUint16("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uint16(0), u)
|
||||
|
||||
u, ok = st.GetUint16("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uint16(0), u)
|
||||
}
|
||||
|
||||
func TestState_GetUint32(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
st.Set("uint32", uint32(400000))
|
||||
u, ok := st.GetUint32("uint32")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, uint32(400000), u)
|
||||
|
||||
st.Set("wrong", "not uint32")
|
||||
u, ok = st.GetUint32("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uint32(0), u)
|
||||
|
||||
u, ok = st.GetUint32("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uint32(0), u)
|
||||
}
|
||||
|
||||
func TestState_GetUint64(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
st.Set("uint64", uint64(5000000))
|
||||
u, ok := st.GetUint64("uint64")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, uint64(5000000), u)
|
||||
|
||||
st.Set("wrong", "not uint64")
|
||||
u, ok = st.GetUint64("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uint64(0), u)
|
||||
|
||||
u, ok = st.GetUint64("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uint64(0), u)
|
||||
}
|
||||
|
||||
func TestState_GetUintptr(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
var ptr uintptr = 12345
|
||||
st.Set("uintptr", ptr)
|
||||
u, ok := st.GetUintptr("uintptr")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, ptr, u)
|
||||
|
||||
st.Set("wrong", "not uintptr")
|
||||
u, ok = st.GetUintptr("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uintptr(0), u)
|
||||
|
||||
u, ok = st.GetUintptr("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, uintptr(0), u)
|
||||
}
|
||||
|
||||
func TestState_GetFloat32(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
st.Set("float32", float32(3.14))
|
||||
f, ok := st.GetFloat32("float32")
|
||||
require.True(t, ok)
|
||||
require.InDelta(t, float32(3.14), f, 0.0001)
|
||||
|
||||
st.Set("wrong", "not float32")
|
||||
f, ok = st.GetFloat32("wrong")
|
||||
require.False(t, ok)
|
||||
require.InDelta(t, float32(0), f, 0.0001)
|
||||
|
||||
f, ok = st.GetFloat32("missing")
|
||||
require.False(t, ok)
|
||||
require.InDelta(t, float32(0), f, 0.0001)
|
||||
}
|
||||
|
||||
func TestState_GetComplex64(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
var c complex64 = complex(2, 3)
|
||||
st.Set("complex64", c)
|
||||
cRes, ok := st.GetComplex64("complex64")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, c, cRes)
|
||||
|
||||
st.Set("wrong", "not complex64")
|
||||
cRes, ok = st.GetComplex64("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, complex64(0), cRes)
|
||||
|
||||
cRes, ok = st.GetComplex64("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, complex64(0), cRes)
|
||||
}
|
||||
|
||||
func TestState_GetComplex128(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
||||
var c complex128 = complex(4, 5)
|
||||
st.Set("complex128", c)
|
||||
cRes, ok := st.GetComplex128("complex128")
|
||||
require.True(t, ok)
|
||||
require.Equal(t, c, cRes)
|
||||
|
||||
st.Set("wrong", "not complex128")
|
||||
cRes, ok = st.GetComplex128("wrong")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, complex128(0), cRes)
|
||||
|
||||
cRes, ok = st.GetComplex128("missing")
|
||||
require.False(t, ok)
|
||||
require.Equal(t, complex128(0), cRes)
|
||||
}
|
||||
|
||||
func TestState_MustGet(t *testing.T) {
|
||||
t.Parallel()
|
||||
st := newState()
|
||||
|
|
Loading…
Reference in New Issue