Skip to content

Commit

Permalink
Add missing interface methods to Postgres store.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed May 17, 2024
1 parent ad5265b commit 1b70416
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 6 deletions.
98 changes: 98 additions & 0 deletions stores/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,104 @@ func (s *Store) Clear(id string) error {
return nil
}

// Int is a helper method to type assert as integer.
func (s *Store) Int(r interface{}, err error) (int, error) {
if err != nil {
return 0, err
}

v, ok := r.(float64)
if !ok {
return 0, ErrAssertType
}

return int(v), err
}

// Int64 is a helper method to type assert as Int64
func (s *Store) Int64(r interface{}, err error) (int64, error) {
if err != nil {
return 0, err
}

v, ok := r.(float64)
if !ok {
return 0, ErrAssertType
}

return int64(v), err
}

// UInt64 is a helper method to type assert as UInt64
func (s *Store) UInt64(r interface{}, err error) (uint64, error) {
if err != nil {
return 0, err
}

v, ok := r.(float64)
if !ok {
return 0, ErrAssertType
}

return uint64(v), err
}

// Float64 is a helper method to type assert as Float64
func (s *Store) Float64(r interface{}, err error) (float64, error) {
if err != nil {
return 0, err
}

v, ok := r.(float64)
if !ok {
return 0, ErrAssertType
}

return v, err
}

// String is a helper method to type assert as String
func (s *Store) String(r interface{}, err error) (string, error) {
if err != nil {
return "", err
}

v, ok := r.(string)
if !ok {
return "", ErrAssertType
}

return v, err
}

// Bytes is a helper method to type assert as Bytes
func (s *Store) Bytes(r interface{}, err error) ([]byte, error) {
if err != nil {
return nil, err
}

v, ok := r.(string)
if !ok {
return nil, ErrAssertType
}

return []byte(v), err
}

// Bool is a helper method to type assert as Bool
func (s *Store) Bool(r interface{}, err error) (bool, error) {
if err != nil {
return false, err
}

v, ok := r.(bool)
if !ok {
return false, ErrAssertType
}

return v, nil
}

// Prune deletes rows that have exceeded the TTL. This should be run externally periodically (ideally as a separate goroutine)
// at desired intervals, hourly/daily etc. based on the expected volume of sessions.
func (s *Store) Prune() error {
Expand Down
59 changes: 53 additions & 6 deletions stores/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestSet(t *testing.T) {
assert.NotEmpty(t, id)

assert.NoError(t, st.Set(id, "num", 123))
assert.NoError(t, st.Set(id, "float", 12.3))
assert.NoError(t, st.Set(id, "str", "hello 123"))
assert.NoError(t, st.Set(id, "bool", true))

Expand All @@ -81,13 +82,59 @@ func TestSet(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, v, float64(123))

v, err = st.Get(id, "str")
assert.NoError(t, err)
assert.Equal(t, v, "hello 123")
{
v, err := st.Int(st.Get(id, "num"))
assert.NoError(t, err)
assert.Equal(t, v, int(123))
}

v, err = st.Get(id, "bool")
assert.NoError(t, err)
assert.Equal(t, v, true)
{
v, err := st.Int64(st.Get(id, "num"))
assert.NoError(t, err)
assert.Equal(t, v, int64(123))
}

{
v, err := st.UInt64(st.Get(id, "num"))
assert.NoError(t, err)
assert.Equal(t, v, uint64(123))
}

{
v, err := st.Float64(st.Get(id, "float"))
assert.NoError(t, err)
assert.Equal(t, v, float64(12.3))
}

{
v, err := st.String(st.Get(id, "str"))
assert.NoError(t, err)
assert.Equal(t, v, "hello 123")
}

{
v, err := st.Bytes(st.Get(id, "str"))
assert.NoError(t, err)
assert.Equal(t, v, []byte("hello 123"))
}

{
v, err := st.Bool(st.Get(id, "bool"))
assert.NoError(t, err)
assert.Equal(t, v, true)
}

{
v, err := st.Get(id, "str")
assert.NoError(t, err)
assert.Equal(t, v, "hello 123")
}

{
v, err := st.Get(id, "bool")
assert.NoError(t, err)
assert.Equal(t, v, true)
}

// Non-existent field.
_, err = st.Get(id, "xx")
Expand Down

0 comments on commit 1b70416

Please sign in to comment.