Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove simplesessions dependency on 'conv' package. #30

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,30 @@ package conv

import (
"strconv"
)

"github.com/vividvilla/simplesessions"
var (
// Error codes for store errors. This should match the codes
// defined in the /simplesessions package exactly.
ErrInvalidSession = &Err{code: 1, msg: "invalid session"}
ErrFieldNotFound = &Err{code: 2, msg: "field not found"}
ErrAssertType = &Err{code: 3, msg: "assertion failed"}
ErrNil = &Err{code: 4, msg: "nil returned"}
)

type Err struct {
code int
msg string
}

func (e *Err) Error() string {
return e.msg
}

func (e *Err) Code() int {
return e.code
}

// Int converts interface to integer.
func Int(r interface{}, err error) (int, error) {
if err != nil {
Expand All @@ -29,10 +49,10 @@ func Int(r interface{}, err error) (int, error) {
n, err := strconv.ParseInt(r, 10, 0)
return int(n), err
case nil:
return 0, simplesessions.ErrNil
return 0, ErrNil
}

return 0, simplesessions.ErrAssertType
return 0, ErrAssertType
}

// Int64 converts interface to Int64.
Expand All @@ -53,10 +73,10 @@ func Int64(r interface{}, err error) (int64, error) {
n, err := strconv.ParseInt(r, 10, 64)
return n, err
case nil:
return 0, simplesessions.ErrNil
return 0, ErrNil
}

return 0, simplesessions.ErrAssertType
return 0, ErrAssertType
}

// UInt64 converts interface to UInt64.
Expand All @@ -70,12 +90,12 @@ func UInt64(r interface{}, err error) (uint64, error) {
return r, err
case int:
if r < 0 {
return 0, simplesessions.ErrAssertType
return 0, ErrAssertType
}
return uint64(r), nil
case int64:
if r < 0 {
return 0, simplesessions.ErrAssertType
return 0, ErrAssertType
}
return uint64(r), nil
case []byte:
Expand All @@ -85,10 +105,10 @@ func UInt64(r interface{}, err error) (uint64, error) {
n, err := strconv.ParseUint(r, 10, 64)
return n, err
case nil:
return 0, simplesessions.ErrNil
return 0, ErrNil
}

return 0, simplesessions.ErrAssertType
return 0, ErrAssertType
}

// Float64 converts interface to Float64.
Expand All @@ -106,9 +126,9 @@ func Float64(r interface{}, err error) (float64, error) {
n, err := strconv.ParseFloat(r, 64)
return n, err
case nil:
return 0, simplesessions.ErrNil
return 0, ErrNil
}
return 0, simplesessions.ErrAssertType
return 0, ErrAssertType
}

// String converts interface to String.
Expand All @@ -122,9 +142,9 @@ func String(r interface{}, err error) (string, error) {
case string:
return r, nil
case nil:
return "", simplesessions.ErrNil
return "", ErrNil
}
return "", simplesessions.ErrAssertType
return "", ErrAssertType
}

// Bytes converts interface to Bytes.
Expand All @@ -138,9 +158,9 @@ func Bytes(r interface{}, err error) ([]byte, error) {
case string:
return []byte(r), nil
case nil:
return nil, simplesessions.ErrNil
return nil, ErrNil
}
return nil, simplesessions.ErrAssertType
return nil, ErrAssertType
}

// Bool converts interface to Bool.
Expand All @@ -161,7 +181,7 @@ func Bool(r interface{}, err error) (bool, error) {
case string:
return strconv.ParseBool(r)
case nil:
return false, simplesessions.ErrNil
return false, ErrNil
}
return false, simplesessions.ErrAssertType
return false, ErrAssertType
}
29 changes: 14 additions & 15 deletions conv/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vividvilla/simplesessions"
)

var (
Expand Down Expand Up @@ -39,7 +38,7 @@ func TestInt(t *testing.T) {

// Test if ErrNil is returned if value is nil.
v, err = Int(nil, nil)
assert.Error(err, simplesessions.ErrNil)
assert.Error(err, ErrNil)
assert.Equal(0, v)

// Test if custom error sent is returned.
Expand All @@ -49,7 +48,7 @@ func TestInt(t *testing.T) {

// Test invalid assert error.
v, err = Int(10.1112, nil)
assert.Error(err, simplesessions.ErrAssertType)
assert.Error(err, ErrAssertType)
assert.Equal(0, v)
}

Expand All @@ -75,7 +74,7 @@ func TestInt64(t *testing.T) {

// Test if ErrNil is returned if value is nil.
v, err = Int64(nil, nil)
assert.Error(err, simplesessions.ErrNil)
assert.Error(err, ErrNil)
assert.Equal(int64(0), v)

// Test if custom error sent is returned.
Expand All @@ -85,7 +84,7 @@ func TestInt64(t *testing.T) {

// Test invalid assert error.
v, err = Int64(10.1112, nil)
assert.Error(err, simplesessions.ErrAssertType)
assert.Error(err, ErrAssertType)
assert.Equal(int64(0), v)
}

Expand All @@ -111,7 +110,7 @@ func TestUInt64(t *testing.T) {

// Test if ErrNil is returned if value is nil.
v, err = UInt64(nil, nil)
assert.Error(err, simplesessions.ErrNil)
assert.Error(err, ErrNil)
assert.Equal(uint64(0), v)

// Test if custom error sent is returned.
Expand All @@ -121,7 +120,7 @@ func TestUInt64(t *testing.T) {

// Test invalid assert error.
v, err = UInt64(10.1112, nil)
assert.Error(err, simplesessions.ErrAssertType)
assert.Error(err, ErrAssertType)
assert.Equal(uint64(0), v)
}

Expand All @@ -147,7 +146,7 @@ func TestFloat64(t *testing.T) {

// Test if ErrNil is returned if value is nil.
v, err = Float64(nil, nil)
assert.Error(err, simplesessions.ErrNil)
assert.Error(err, ErrNil)
assert.Equal(float64(0), v)

// Test if custom error sent is returned.
Expand All @@ -157,7 +156,7 @@ func TestFloat64(t *testing.T) {

// Test invalid assert error.
v, err = Float64("abc", nil)
assert.Error(err, simplesessions.ErrAssertType)
assert.Error(err, ErrAssertType)
assert.Equal(float64(0), v)
}

Expand All @@ -179,7 +178,7 @@ func TestString(t *testing.T) {

// Test if ErrNil is returned if value is nil.
v, err = String(nil, nil)
assert.Error(err, simplesessions.ErrNil)
assert.Error(err, ErrNil)
assert.Equal("", v)

// Test if custom error sent is returned.
Expand All @@ -189,7 +188,7 @@ func TestString(t *testing.T) {

// Test invalid assert error.
v, err = String(10.1112, nil)
assert.Error(err, simplesessions.ErrAssertType)
assert.Error(err, ErrAssertType)
assert.Equal("", v)
}

Expand All @@ -211,7 +210,7 @@ func TestBytes(t *testing.T) {

// Test if ErrNil is returned if value is nil.
v, err = Bytes(nil, nil)
assert.Error(err, simplesessions.ErrNil)
assert.Error(err, ErrNil)
assert.Equal([]byte(nil), v)

// Test if custom error sent is returned.
Expand All @@ -221,7 +220,7 @@ func TestBytes(t *testing.T) {

// Test invalid assert error.
v, err = Bytes(10.1112, nil)
assert.Error(err, simplesessions.ErrAssertType)
assert.Error(err, ErrAssertType)
assert.Equal([]byte(nil), v)
}

Expand Down Expand Up @@ -270,7 +269,7 @@ func TestBool(t *testing.T) {

// Test if ErrNil is returned if value is nil.
v, err = Bool(nil, nil)
assert.Error(err, simplesessions.ErrNil)
assert.Error(err, ErrNil)
assert.Equal(false, v)

// Test if custom error sent is returned.
Expand All @@ -280,6 +279,6 @@ func TestBool(t *testing.T) {

// Test invalid assert error.
v, err = Bool(10.1112, nil)
assert.Error(err, simplesessions.ErrAssertType)
assert.Error(err, ErrAssertType)
assert.Equal(false, v)
}
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ go 1.18

use (
.
./conv
./stores/goredis
./stores/redis
./stores/securecookie
Expand Down
Loading