Skip to content

Commit

Permalink
Port precision to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
josephburnett committed Jul 25, 2024
1 parent 3846799 commit 6132595
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/go-openapi/jsonpointer v0.19.5
github.com/josephburnett/jd/v2 v2.0.0-00010101000000-000000000000
github.com/josephburnett/jd/v2 v2.0.0-20240724160553-38467990808b
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ func parseMetadata() ([]jd.Metadata, error) {
}

func parseMetadataV2() ([]v2.Option, error) {
if *precision != 0.0 {
errorAndExit("--precision cannot be used with --v2 yet")
}
if *precision != 0.0 && (*set || *mset) {
return nil, fmt.Errorf("-precision cannot be used with -set or -mset because they use hashcodes")
}
Expand All @@ -202,6 +199,7 @@ func parseMetadataV2() ([]v2.Option, error) {
if *format == "merge" {
options = append(options, v2.MERGE)
}
options = append(options, v2.Precision(*precision))
return options, nil
}

Expand Down
12 changes: 8 additions & 4 deletions v2/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jd
import (
"bytes"
"encoding/binary"
"math"
)

type jsonNumber float64
Expand All @@ -22,14 +23,17 @@ func (n jsonNumber) raw() interface{} {
}

func (n1 jsonNumber) Equals(node JsonNode, options ...Option) bool {

precision := 0.0
if p, ok := getOption[precisionOption](options); ok {
precision = p.precision
}

n2, ok := node.(jsonNumber)
if !ok {
return false
}
if n1 != n2 {
return false
}
return true
return math.Abs(float64(n1)-float64(n2)) <= precision
}

func (n jsonNumber) hashCode(options []Option) [8]byte {
Expand Down
6 changes: 6 additions & 0 deletions v2/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ func TestNumberEqual(t *testing.T) {
checkEqual(ctx, `0`, `0.0`)
checkEqual(ctx, `0.0001`, `0.0001`)
checkEqual(ctx, `123`, `123`)
checkEqual(
newTestContext(t).withOptions(Precision(0.1)),
`1.0`, `1.09`)
}

func TestNumberNotEqual(t *testing.T) {
ctx := newTestContext(t)
checkNotEqual(ctx, `0`, `1`)
checkNotEqual(ctx, `0`, `0.0001`)
checkNotEqual(ctx, `1234`, `1235`)
checkNotEqual(
newTestContext(t).withOptions(Precision(0.1)),
`1`, `1.2`)
}

func TestNumberHash(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions v2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ type setOption struct{}
type setKeysOption []string
type multisetOption struct{}
type renderColorOption struct{}
type precisionOption struct {
precision float64
}

func (o mergeOption) isOption() {}
func (o mergeOption) string() string { return "MERGE" }
func (o setOption) isOption() {}
func (o setKeysOption) isOption() {}
func (o multisetOption) isOption() {}
func (o renderColorOption) isOption() {}
func (o precisionOption) isOption() {}

var (
SET = setOption{}
Expand All @@ -35,6 +39,10 @@ func SetKeys(keys ...string) Option {
return setKeysOption(keys)
}

func Precision(precision float64) Option {
return precisionOption{precision}
}

type patchStrategy string

const (
Expand Down
12 changes: 8 additions & 4 deletions vendor/github.com/josephburnett/jd/v2/number.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/josephburnett/jd/v2/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ github.com/go-openapi/jsonpointer
# github.com/go-openapi/swag v0.21.1
## explicit; go 1.11
github.com/go-openapi/swag
# github.com/josephburnett/jd/v2 v2.0.0-00010101000000-000000000000 => ./v2
# github.com/josephburnett/jd/v2 v2.0.0-20240724160553-38467990808b => ./v2
## explicit; go 1.18
github.com/josephburnett/jd/v2
# github.com/josharian/intern v1.0.0
Expand Down

0 comments on commit 6132595

Please sign in to comment.