Skip to content

Commit

Permalink
stores: change bCurrency type to BigEndian (#991)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl authored Feb 22, 2024
1 parent 9cdcd60 commit 414090b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
16 changes: 11 additions & 5 deletions stores/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4326,13 +4326,13 @@ func TestTypeCurrency(t *testing.T) {
cmp: "=",
},
{
a: types.NewCurrency(0, math.MaxUint64),
b: types.NewCurrency(math.MaxUint64, 0),
a: types.NewCurrency(math.MaxUint64, 0),
b: types.NewCurrency(0, math.MaxUint64),
cmp: "<",
},
{
a: types.NewCurrency(math.MaxUint64, 0),
b: types.NewCurrency(0, math.MaxUint64),
a: types.NewCurrency(0, math.MaxUint64),
b: types.NewCurrency(math.MaxUint64, 0),
cmp: ">",
},
}
Expand All @@ -4342,7 +4342,13 @@ func TestTypeCurrency(t *testing.T) {
if err != nil {
t.Fatal(err)
} else if !result {
t.Fatal("unexpected result", result)
t.Fatalf("unexpected result %v for %v %v %v", result, test.a, test.cmp, test.b)
} else if test.cmp == "<" && test.a.Cmp(test.b) >= 0 {
t.Fatal("invalid result")
} else if test.cmp == ">" && test.a.Cmp(test.b) <= 0 {
t.Fatal("invalid result")
} else if test.cmp == "=" && test.a.Cmp(test.b) != 0 {
t.Fatal("invalid result")
}
}

Expand Down
8 changes: 4 additions & 4 deletions stores/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,15 @@ func (sc *bCurrency) Scan(src any) error {
return fmt.Errorf("cannot scan %d bytes to Currency", len(buf))
}

sc.Lo = binary.LittleEndian.Uint64(buf[:8])
sc.Hi = binary.LittleEndian.Uint64(buf[8:])
sc.Hi = binary.BigEndian.Uint64(buf[:8])
sc.Lo = binary.BigEndian.Uint64(buf[8:])
return nil
}

// Value implements the driver.Valuer interface.
func (sc bCurrency) Value() (driver.Value, error) {
buf := make([]byte, 16)
binary.LittleEndian.PutUint64(buf[:8], sc.Lo)
binary.LittleEndian.PutUint64(buf[8:], sc.Hi)
binary.BigEndian.PutUint64(buf[:8], sc.Hi)
binary.BigEndian.PutUint64(buf[8:], sc.Lo)
return buf, nil
}

0 comments on commit 414090b

Please sign in to comment.