Skip to content

Commit 1ed5fe5

Browse files
committed
add Uint64 and MustUint64 support
1 parent 408ce0b commit 1ed5fe5

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

simplejson.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,26 @@ func (j *Json) MustInt64(args ...int64) int64 {
335335

336336
return def
337337
}
338+
339+
// MustUInt64 guarantees the return of an `uint64` (with optional default)
340+
//
341+
// useful when you explicitly want an `uint64` in a single value return context:
342+
// myFunc(js.Get("param1").MustUint64(), js.Get("optional_param").MustUint64(5150))
343+
func (j *Json) MustUint64(args ...uint64) uint64 {
344+
var def uint64
345+
346+
switch len(args) {
347+
case 0:
348+
case 1:
349+
def = args[0]
350+
default:
351+
log.Panicf("MustUint64() received too many arguments %d", len(args))
352+
}
353+
354+
i, err := j.Uint64()
355+
if err == nil {
356+
return i
357+
}
358+
359+
return def
360+
}

simplejson_go10.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,18 @@ func (j *Json) Int() (int, error) {
2828
return -1, errors.New("type assertion to float64 failed")
2929
}
3030

31-
// Int type asserts to `float64` then converts to `int64`
31+
// Int64 type asserts to `float64` then converts to `int64`
3232
func (j *Json) Int64() (int64, error) {
3333
if f, ok := (j.data).(float64); ok {
3434
return int64(f), nil
3535
}
3636
return -1, errors.New("type assertion to float64 failed")
3737
}
38+
39+
// Uint64 type asserts to `float64` then converts to `uint64`
40+
func (j *Json) Uint64() (uint64, error) {
41+
if f, ok := (j.data).(float64); ok {
42+
return uint64(f), nil
43+
}
44+
return 0, errors.New("type assertion to float64 failed")
45+
}

simplejson_go11.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"bytes"
77
"encoding/json"
88
"errors"
9+
"strconv"
910
)
1011

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

41-
// Int type asserts to `json.Number` then converts to `int64`
42+
// Int64 type asserts to `json.Number` then converts to `int64`
4243
func (j *Json) Int64() (int64, error) {
4344
if n, ok := (j.data).(json.Number); ok {
4445
return n.Int64()
@@ -48,3 +49,15 @@ func (j *Json) Int64() (int64, error) {
4849
}
4950
return -1, errors.New("type assertion to json.Number failed")
5051
}
52+
53+
// Uint64 type asserts to `json.Number` then converts to `uint64`
54+
func (j *Json) Uint64() (uint64, error) {
55+
if n, ok := (j.data).(json.Number); ok {
56+
u, err := strconv.ParseUint(n.String(), 10, 64)
57+
return u, err
58+
}
59+
if f, ok := (j.data).(float64); ok {
60+
return uint64(f), nil
61+
}
62+
return 0, errors.New("type assertion to json.Number failed")
63+
}

simplejson_go11_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ func TestSimplejsonGo11(t *testing.T) {
1717
{"subkeyone": 1},
1818
{"subkeytwo": 2, "subkeythree": 3}
1919
],
20-
"bignum": 9223372036854775807
20+
"bignum": 9223372036854775807,
21+
"uint64": 18446744073709551615
2122
}
2223
}`))
2324

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

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

0 commit comments

Comments
 (0)