Skip to content

implements cast methods for (u)int64 and (u)int32 #153

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

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 52 additions & 0 deletions msgp/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,58 @@ func (n *Number) Float() (float64, bool) {
}
}

// CastInt64 returns the number as an int64 and
// returns whether the cast is valid.
func (n *Number) CastInt64() (int64, bool) {
i, ok := n.Int()
if !ok {
return 0, false
}

return i, true
}

// CastInt32 returns the number as an int32 and
// returns whether the cast is valid.
func (n *Number) CastInt32() (int32, bool) {
i, ok := n.CastInt64()
if !ok {
return 0, false
}

if i >= math.MinInt32 && i <= math.MaxInt32 {
return int32(i), true
}

return 0, false
}

// CastUint64 returns the number as an uint64 and
// returns whether the cast is valid.
func (n *Number) CastUint64() (uint64, bool) {
i, ok := n.Uint()
if i-1 == math.MaxInt64 && !ok {
return 0, false
}

return i, true
}

// CastUint32 returns the number as an uint32 and
// returns whether the cast is valid.
func (n *Number) CastUint32() (uint32, bool) {
i, ok := n.CastUint64()
if !ok {
return 0, false
}

if i <= math.MaxInt32 {
return uint32(i), true
}

return 0, false
}

// Type will return one of:
// Float64Type, Float32Type, UintType, or IntType.
func (n *Number) Type() Type {
Expand Down
66 changes: 66 additions & 0 deletions msgp/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package msgp

import (
"bytes"
"math"
"testing"
)

Expand Down Expand Up @@ -92,3 +93,68 @@ func TestNumber(t *testing.T) {
}

}

func TestNumber_CastInt64(t *testing.T) {
var n Number
n.AsUint(math.MaxUint64)

_, ok := n.CastInt64()
if ok {
t.Error("CastInt64() failed: MaxUint64 > MaxInt64")
}
}

func TestNumber_CastInt32(t *testing.T) {
cases := []struct {
in int64
want bool
}{
{math.MinInt32, true},
{math.MinInt32 - 1, false},
{math.MaxInt32, true},
{math.MaxInt32 + 1, false},
{0, true},
}

var n Number
for _, c := range cases {
n.AsInt(c.in)

_, ok := n.CastInt32()
if ok != c.want {
t.Errorf("cast %v to int32 invalid: %v, got %v", c.in, c.want, ok)
}
}
}

func TestNumber_CastUint64(t *testing.T) {
var n Number
n.AsInt(math.MinInt64)

_, ok := n.CastUint64()
if ok {
t.Error("CastUint64() failed: MinInt64 < 0")
}
}

func TestNumber_CastUint32(t *testing.T) {
cases := []struct {
in int64
want bool
}{
{math.MinInt64, false},
{math.MaxInt32, true},
{math.MaxInt32 + 1, false},
{0, true},
}

var n Number
for _, c := range cases {
n.AsInt(c.in)

_, ok := n.CastUint32()
if ok != c.want {
t.Errorf("cast %v to uint32 invalid: %v, got %v", c.in, c.want, ok)
}
}
}