diff --git a/session/session.go b/session/session.go index f40f73e..eb6ce25 100644 --- a/session/session.go +++ b/session/session.go @@ -116,180 +116,180 @@ func (s *Session) Set(key string, value interface{}) { s.data[key] = value } -func (s *Session) Int(key string) (int, error) { +func (s *Session) HasKey(key string) bool { + _, has := s.data[key] + return has +} + +func (s *Session) Int(key string) int { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(int) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) Int8(key string) (int8, error) { +func (s *Session) Int8(key string) int8 { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(int8) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) Int16(key string) (int16, error) { +func (s *Session) Int16(key string) int16 { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(int16) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) Int32(key string) (int32, error) { +func (s *Session) Int32(key string) int32 { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(int32) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) Int64(key string) (int64, error) { +func (s *Session) Int64(key string) int64 { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(int64) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) Uint(key string) (uint, error) { +func (s *Session) Uint(key string) uint { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(uint) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) Uint8(key string) (uint8, error) { +func (s *Session) Uint8(key string) uint8 { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(uint8) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) Uint16(key string) (uint16, error) { +func (s *Session) Uint16(key string) uint16 { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(uint16) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) Uint32(key string) (uint32, error) { +func (s *Session) Uint32(key string) uint32 { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(uint32) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) Uint64(key string) (uint64, error) { +func (s *Session) Uint64(key string) uint64 { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(uint64) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) Float32(key string) (float32, error) { +func (s *Session) Float32(key string) float32 { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(float32) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) Float64(key string) (float64, error) { +func (s *Session) Float64(key string) float64 { v, ok := s.data[key] if !ok { - return 0, ErrKeyNotFound + return 0 } value, ok := v.(float64) if !ok { - return 0, ErrWrongValueType + return 0 } - return value, nil + return value } -func (s *Session) String(key string) (string, error) { +func (s *Session) String(key string) string { v, ok := s.data[key] if !ok { - return "", ErrKeyNotFound + return "" } value, ok := v.(string) if !ok { - return "", ErrWrongValueType + return "" } - return value, nil + return value } -func (s *Session) Value(key string) (interface{}, error) { - v, ok := s.data[key] - if !ok { - return nil, ErrKeyNotFound - } - - return v, nil +func (s *Session) Value(key string) interface{} { + return s.data[key] } diff --git a/session/session_test.go b/session/session_test.go index 6c9f3f5..76638b5 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -1,2 +1,151 @@ package session +import "testing" + +func TestNewSession(t *testing.T) { + s := NewSession(nil) + if s.ID < 1 { + t.Fail() + } +} + +func TestSession_Bind(t *testing.T) { + s := NewSession(nil) + uids := []int64{100,1000,10000000} + for i, uid := range uids { + s.Bind(uid) + if s.Uid != uids[i] { + t.Fail() + } + } +} + +func TestSession_HasKey(t *testing.T) { + s := NewSession(nil) + key := "hello" + value := "world" + s.Set(key,value) + if !s.HasKey(key) { + t.Fail() + } +} + +func TestSession_Float32(t *testing.T) { + s := NewSession(nil) + key := "hello" + value := float32(1.2000) + s.Set(key,value) + if value != s.Float32(key) { + t.Fail() + } +} + +func TestSession_Float64(t *testing.T) { + s := NewSession(nil) + key := "hello" + value := 1.2000 + s.Set(key,value) + if value != s.Float64(key) { + t.Fail() + } +} + +func TestSession_Int(t *testing.T) { +s := NewSession(nil) + key := "testkey" + value := 234 + s.Set(key,value) + if value != s.Int(key) { + t.Fail() + } +} + +func TestSession_Int8(t *testing.T) { +s := NewSession(nil) + key := "testkey" + value := int8(123) + s.Set(key,value) + if value != s.Int8(key) { + t.Fail() + } +} + +func TestSession_Int16(t *testing.T) { +s := NewSession(nil) + key := "testkey" + value := int16(3245) + s.Set(key,value) + if value != s.Int16(key) { + t.Fail() + } +} + +func TestSession_Int32(t *testing.T) { +s := NewSession(nil) + key := "testkey" + value := int32(5454) + s.Set(key,value) + if value != s.Int32(key) { + t.Fail() + } +} + +func TestSession_Int64(t *testing.T) { +s := NewSession(nil) + key := "testkey" + value := int64(444454) + s.Set(key,value) + if value != s.Int64(key) { + t.Fail() + } +} + +func TestSession_Uint(t *testing.T) { +s := NewSession(nil) + key := "testkey" + value := uint(24254) + s.Set(key,value) + if value != s.Uint(key) { + t.Fail() + } +} + +func TestSession_Uint8(t *testing.T) { +s := NewSession(nil) + key := "testkey" + value := uint8(34) + s.Set(key,value) + if value != s.Uint8(key) { + t.Fail() + } +} + +func TestSession_Uint16(t *testing.T) { +s := NewSession(nil) + key := "testkey" + value := uint16(4645) + s.Set(key,value) + if value != s.Uint16(key) { + t.Fail() + } +} + +func TestSession_Uint32(t *testing.T) { +s := NewSession(nil) + key := "testkey" + value := uint32(12365) + s.Set(key,value) + if value != s.Uint32(key) { + t.Fail() + } +} + +func TestSession_Uint64(t *testing.T) { +s := NewSession(nil) + key := "testkey" + value := uint64(1000) + s.Set(key,value) + if value != s.Uint64(key) { + t.Fail() + } +} \ No newline at end of file