Skip to content

Commit

Permalink
Merge pull request bitly#32 from jehiah/uint64_32
Browse files Browse the repository at this point in the history
possible uint64 support
  • Loading branch information
mreiferson committed May 23, 2014
2 parents 408ce0b + 7c0d2a9 commit 102f3f0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ language: go
go:
- 1.0.3
- 1.1.2
- 1.2
- tip
install:
- go get github.com/bmizerany/assert
script:
- pushd $TRAVIS_BUILD_DIR
- go test
- popd
notifications:
email: false
23 changes: 23 additions & 0 deletions simplejson.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,26 @@ func (j *Json) MustInt64(args ...int64) int64 {

return def
}

// MustUInt64 guarantees the return of an `uint64` (with optional default)
//
// useful when you explicitly want an `uint64` in a single value return context:
// myFunc(js.Get("param1").MustUint64(), js.Get("optional_param").MustUint64(5150))
func (j *Json) MustUint64(args ...uint64) uint64 {
var def uint64

switch len(args) {
case 0:
case 1:
def = args[0]
default:
log.Panicf("MustUint64() received too many arguments %d", len(args))
}

i, err := j.Uint64()
if err == nil {
return i
}

return def
}
10 changes: 9 additions & 1 deletion simplejson_go10.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ func (j *Json) Int() (int, error) {
return -1, errors.New("type assertion to float64 failed")
}

// Int type asserts to `float64` then converts to `int64`
// Int64 type asserts to `float64` then converts to `int64`
func (j *Json) Int64() (int64, error) {
if f, ok := (j.data).(float64); ok {
return int64(f), nil
}
return -1, errors.New("type assertion to float64 failed")
}

// Uint64 type asserts to `float64` then converts to `uint64`
func (j *Json) Uint64() (uint64, error) {
if f, ok := (j.data).(float64); ok {
return uint64(f), nil
}
return 0, errors.New("type assertion to float64 failed")
}
15 changes: 14 additions & 1 deletion simplejson_go11.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"encoding/json"
"errors"
"strconv"
)

// Implements the json.Unmarshaler interface.
Expand Down Expand Up @@ -38,7 +39,7 @@ func (j *Json) Int() (int, error) {
return -1, errors.New("type assertion to json.Number failed")
}

// Int type asserts to `json.Number` then converts to `int64`
// Int64 type asserts to `json.Number` then converts to `int64`
func (j *Json) Int64() (int64, error) {
if n, ok := (j.data).(json.Number); ok {
return n.Int64()
Expand All @@ -48,3 +49,15 @@ func (j *Json) Int64() (int64, error) {
}
return -1, errors.New("type assertion to json.Number failed")
}

// Uint64 type asserts to `json.Number` then converts to `uint64`
func (j *Json) Uint64() (uint64, error) {
if n, ok := (j.data).(json.Number); ok {
u, err := strconv.ParseUint(n.String(), 10, 64)
return u, err
}
if f, ok := (j.data).(float64); ok {
return uint64(f), nil
}
return 0, errors.New("type assertion to json.Number failed")
}
4 changes: 3 additions & 1 deletion simplejson_go11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func TestSimplejsonGo11(t *testing.T) {
{"subkeyone": 1},
{"subkeytwo": 2, "subkeythree": 3}
],
"bignum": 9223372036854775807
"bignum": 9223372036854775807,
"uint64": 18446744073709551615
}
}`))

Expand Down Expand Up @@ -46,4 +47,5 @@ func TestSimplejsonGo11(t *testing.T) {
assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")})

assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807))
assert.Equal(t, js.Get("test").Get("uint64").MustUint64(), uint64(18446744073709551615))
}

0 comments on commit 102f3f0

Please sign in to comment.