Skip to content

Commit

Permalink
Add -use-number flag that enables json.UseNumber(). Add deprecation w…
Browse files Browse the repository at this point in the history
…arnings.
  • Loading branch information
benmanns committed May 22, 2014
1 parent c3c123b commit ed77c6c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
12 changes: 10 additions & 2 deletions README.md
Expand Up @@ -94,18 +94,26 @@ goworker worker functions receive the queue they are serving and a slice of inte
```go
// Expecting (int, string, float64)
func myFunc(queue, args ...interface{}) error {
id, ok := args[0].(int)
idNum, ok := args[0].(json.Number)
if !ok {
return errorInvalidParam
}
id, err := idNum.Int64()
if err != nil {
return errorInvalidParam
}
name, ok := args[1].(string)
if !ok {
return errorInvalidParam
}
weight, ok := args[2].(float64)
weightNum, ok := args[2].(json.Number)
if !ok {
return errorInvalidParam
}
weight, err := weightNum.Float64()
if err != nil {
return errorInvalidParam
}
doSomething(id, name, weight)
return nil
}
Expand Down
12 changes: 10 additions & 2 deletions doc.go
Expand Up @@ -76,18 +76,26 @@
//
// // Expecting (int, string, float64)
// func myFunc(queue, args ...interface{}) error {
// id, ok := args[0].(int)
// idNum, ok := args[0].(json.Number)
// if !ok {
// return errorInvalidParam
// }
// id, err := idNum.Int64()
// if err != nil {
// return errorInvalidParam
// }
// name, ok := args[1].(string)
// if !ok {
// return errorInvalidParam
// }
// weight, ok := args[2].(float64)
// weightNum, ok := args[2].(json.Number)
// if !ok {
// return errorInvalidParam
// }
// weight, err := weightNum.Float64()
// if err != nil {
// return errorInvalidParam
// }
// doSomething(id, name, weight)
// return nil
// }
Expand Down
19 changes: 19 additions & 0 deletions flags.go
Expand Up @@ -69,6 +69,14 @@
// with the time command to benchmark different
// configurations.
//
// -use-number=false
// — Uses json.Number when decoding numbers in the
// job payloads. This will avoid issues that
// occur when goworker and the json package decode
// large numbers as floats, which then get
// encoded in scientific notation, losing
// pecision. This will default to true soon.
//
// You can also configure your own flags for use
// within your workers. Be sure to set them
// before calling goworker.Main(). It is okay to
Expand All @@ -94,6 +102,7 @@ var (
namespace string
exitOnComplete bool
isStrict bool
useNumber bool
)

// Namespace returns the namespace flag for goworker. You
Expand Down Expand Up @@ -127,6 +136,8 @@ func init() {
flag.StringVar(&namespace, "namespace", "resque:", "the Redis namespace")

flag.BoolVar(&exitOnComplete, "exit-on-complete", false, "exit when the queue is empty")

flag.BoolVar(&useNumber, "use-number", false, "use json.Number instead of float64 when decoding numbers in JSON. will default to true soon")
}

func flags() error {
Expand All @@ -140,5 +151,13 @@ func flags() error {
return err
}
isStrict = strings.IndexRune(queuesString, '=') == -1

if !useNumber {
logger.Warn("== DEPRECATION WARNING ==")
logger.Warn(" Currently, encoding/json decodes numbers as float64.")
logger.Warn(" This can cause numbers to lose precision as they are read from the Resque queue.")
logger.Warn(" Set the -use-numbers flag to use json.Number when decoding numbers and remove this warning.")
}

return nil
}
8 changes: 7 additions & 1 deletion poller.go
@@ -1,6 +1,7 @@
package goworker

import (
"bytes"
"encoding/json"
"fmt"
"time"
Expand Down Expand Up @@ -35,7 +36,12 @@ func (p *poller) getJob(conn *RedisConn) (*job, error) {

job := &job{Queue: queue}

if err := json.Unmarshal(reply.([]byte), &job.Payload); err != nil {
decoder := json.NewDecoder(bytes.NewReader(reply.([]byte)))
if useNumber {
decoder.UseNumber()
}

if err := decoder.Decode(&job.Payload); err != nil {
return nil, err
}
return job, nil
Expand Down

0 comments on commit ed77c6c

Please sign in to comment.