Skip to content

Commit

Permalink
Merge pull request bitly#19 from mreiferson/must_int64_19
Browse files Browse the repository at this point in the history
add MustInt64()
  • Loading branch information
jehiah committed Oct 8, 2013
2 parents 3f62156 + ae67657 commit 4855898
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion simplejson.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// returns the current implementation version
func Version() string {
return "0.4.3"
return "0.5.0-alpha"
}

type Json struct {
Expand Down Expand Up @@ -312,3 +312,26 @@ func (j *Json) MustBool(args ...bool) bool {

return def
}

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

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

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

return def
}

0 comments on commit 4855898

Please sign in to comment.