Skip to content

Commit

Permalink
remove json.Number usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mperham committed May 1, 2024
1 parent 428506c commit 07dbb5d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
25 changes: 1 addition & 24 deletions util/json.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
package util

import (
"bytes"
"encoding/json"
)

func Must[T any](obj T, err error) T {
if err != nil {
panic(err)
}
return obj
}

var (
// If true, activates encoding/json's Decoder and its UseNumber()
// option to preserve number precision.
// Defaults to false in Faktory 1.x.
// Will default to true in Faktory 2.x
JsonUseNumber bool = Faktory2Preview
)

func JsonUnmarshal(data []byte, target any) error {
if !JsonUseNumber {
return json.Unmarshal(data, target)
}

buf := bytes.NewBuffer(data)
dec := json.NewDecoder(buf)
dec.UseNumber()
return dec.Decode(target)
return json.Unmarshal(data, target)
}
7 changes: 7 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ const (
maxInt63 = int64(^uint64(0) >> 1)
)

func Must[T any](obj T, err error) T {
if err != nil {
panic(err)
}
return obj
}

var (
// Set FAKTORY2_PREVIEW=true to enable breaking changes coming in Faktory 2.0.
Faktory2Preview bool = Must(strconv.ParseBool(cmp.Or(os.Getenv("FAKTORY2_PREVIEW"), "false")))
Expand Down

0 comments on commit 07dbb5d

Please sign in to comment.