Skip to content

Commit 1cfaa73

Browse files
authored
refactor: merge big.Rat wrapper types (#1134)
Fixes #1133 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 0120828 commit 1cfaa73

File tree

5 files changed

+52
-103
lines changed

5 files changed

+52
-103
lines changed

cbor/tags.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package cbor
1616

1717
import (
18+
"encoding/json"
1819
"errors"
1920
"fmt"
2021
"math/big"
@@ -150,6 +151,24 @@ func (r *Rat) MarshalCBOR() ([]byte, error) {
150151
return Encode(&tmpData)
151152
}
152153

154+
func (r *Rat) UnmarshalJSON(data []byte) error {
155+
// Try as ratio
156+
var tmpData struct {
157+
Numerator int64 `json:"numerator"`
158+
Denominator int64 `json:"denominator"`
159+
}
160+
if err := json.Unmarshal(data, &tmpData); err == nil {
161+
r.Rat = big.NewRat(tmpData.Numerator, tmpData.Denominator)
162+
return nil
163+
}
164+
// Try as decimal value
165+
r.Rat = new(big.Rat)
166+
if _, ok := r.SetString(string(data)); !ok {
167+
return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", data)
168+
}
169+
return nil
170+
}
171+
153172
func (r *Rat) ToBigRat() *big.Rat {
154173
return r.Rat
155174
}

cbor/tags_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ package cbor_test
1616

1717
import (
1818
"encoding/hex"
19+
"encoding/json"
1920
"math/big"
2021
"reflect"
2122
"testing"
2223

2324
"github.com/blinklabs-io/gouroboros/cbor"
25+
"github.com/blinklabs-io/gouroboros/ledger/common"
2426
)
2527

2628
var tagsTestDefs = []struct {
@@ -109,3 +111,31 @@ func TestTagsEncode(t *testing.T) {
109111
}
110112
}
111113
}
114+
115+
func TestRatJsonUnmarshalNumDenom(t *testing.T) {
116+
jsonData := `{"testRat": { "numerator": 721, "denominator": 10000 }}`
117+
expectedRat := big.NewRat(721, 10000)
118+
var testData struct {
119+
TestRat common.GenesisRat `json:"testRat"`
120+
}
121+
if err := json.Unmarshal([]byte(jsonData), &testData); err != nil {
122+
t.Fatalf("unexpected error: %s", err)
123+
}
124+
if testData.TestRat.Cmp(expectedRat) != 0 {
125+
t.Errorf("did not get expected value: got %s, wanted %s", testData.TestRat.String(), expectedRat.String())
126+
}
127+
}
128+
129+
func TestRatJsonUnmarshalFloat(t *testing.T) {
130+
jsonData := `{"testRat": 0.0721}`
131+
expectedRat := big.NewRat(721, 10000)
132+
var testData struct {
133+
TestRat common.GenesisRat `json:"testRat"`
134+
}
135+
if err := json.Unmarshal([]byte(jsonData), &testData); err != nil {
136+
t.Fatalf("unexpected error: %s", err)
137+
}
138+
if testData.TestRat.Cmp(expectedRat) != 0 {
139+
t.Errorf("did not get expected value: got %s, wanted %s", testData.TestRat.String(), expectedRat.String())
140+
}
141+
}

ledger/common/common.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,6 @@ type ExUnits struct {
439439
Memory uint64
440440
Steps uint64
441441
}
442+
443+
// GenesisRat is a convenience type for cbor.Rat
444+
type GenesisRat = cbor.Rat

ledger/common/genesis.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

ledger/common/genesis_test.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)