Skip to content

Commit

Permalink
Merge pull request #3 from stasundr/yaml_marshaler
Browse files Browse the repository at this point in the history
Implement Yaml Marshaler and Unmarshaler interface for Decimal
  • Loading branch information
yakud authored Dec 16, 2021
2 parents 2a4db15 + d2c7f46 commit 571a4cc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

### [0.1.3] 2021-12-16:

- Added: Implement `yaml.Marshaler` and `yaml.Unmarshaler` interface for Decimal

### [0.1.2] 2021-12-10:

- Changed: Removed redundant `go-ethereum` dependency
Expand Down
17 changes: 17 additions & 0 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ func (d *Decimal) UnmarshalJSON(dataJson []byte) error {
return nil
}

func (d *Decimal) MarshalYAML() (interface{}, error) {
return d.String(), nil
}

func (d *Decimal) UnmarshalYAML(unmarshal func(interface{}) error) error {
var data string
if err := unmarshal(&data); err != nil {
return nil
}

if !d.FromString(data) {
return fmt.Errorf("unmarshal decimal: %s", data)
}

return nil
}

// return d == y
func (d *Decimal) Eq(y *Decimal) bool {
xx := NewDecimal(d)
Expand Down
46 changes: 46 additions & 0 deletions decimal_marshal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package decimal

import (
"gopkg.in/yaml.v3"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDecimal_MarshalJSON(t *testing.T) {
x, ok := NewDecimalFromString("1")
assert.True(t, ok)
xJson, err := json.Marshal(x)
assert.NoError(t, err)
assert.Equal(t, []byte(`"1"`), xJson)
assert.NoError(t, json.Unmarshal(xJson, &x))
assert.Equal(t, x, NewDecimalFromStringOrDefault("1", NewDecimalZero()))

x, ok = NewDecimalFromString("1.5555555")
assert.True(t, ok)
xJson, err = json.Marshal(x)
assert.NoError(t, err)
assert.Equal(t, []byte(`"1.5555555"`), xJson)
assert.NoError(t, json.Unmarshal(xJson, &x))
assert.Equal(t, x, NewDecimalFromStringOrDefault("1.5555555", NewDecimalZero()))
}

func TestDecimal_MarshalYAML(t *testing.T) {
x, ok := NewDecimalFromString("1")
assert.True(t, ok)
xYaml, err := yaml.Marshal(x)
assert.NoError(t, err)
actualXYaml := append([]byte(`"1"`), 0xa)
assert.Equal(t, actualXYaml, xYaml)
assert.NoError(t, yaml.Unmarshal(actualXYaml, &x))
assert.Equal(t, x, NewDecimalFromStringOrDefault("1", NewDecimalZero()))

x, ok = NewDecimalFromString("1.5555555")
assert.True(t, ok)
xYaml, err = yaml.Marshal(x)
assert.NoError(t, err)
actualXYaml = append([]byte(`"1.5555555"`), 0xa)
assert.Equal(t, actualXYaml, xYaml)
assert.NoError(t, yaml.Unmarshal(actualXYaml, &x))
assert.Equal(t, x, NewDecimalFromStringOrDefault("1.5555555", NewDecimalZero()))
}
24 changes: 0 additions & 24 deletions decimal_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package decimal

import (
"fmt"
"testing"

"github.com/holiman/uint256"
Expand Down Expand Up @@ -642,26 +641,3 @@ func TestDecimal_Eq(t *testing.T) {
t.Fatalf("x is not equal y")
}
}

func TestExpScaleFast(t *testing.T) {
expScale := uint256.NewInt(0).Set(ten)
fmt.Println(expScale.Bytes(), expScale.Uint64())
for _, n := range expScale.Bytes() {
fmt.Printf("% 08b", n) // prints 00000000 11111101
}
fmt.Println("")

expScale2 := uint256.NewInt(0).Exp(expScale, uint256.NewInt(0).SetUint64(2))
fmt.Println(expScale2.Bytes(), expScale2.Uint64())
for _, n := range expScale2.Bytes() {
fmt.Printf("% 08b", n) // prints 00000000 11111101
}
fmt.Println("")

expScale3 := uint256.NewInt(0).Exp(expScale, uint256.NewInt(0).SetUint64(3))
fmt.Println(expScale3.Bytes(), expScale3.Uint64())
for _, n := range expScale3.Bytes() {
fmt.Printf("% 08b", n) // prints 00000000 11111101
}
fmt.Println("")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ require (
github.com/holiman/uint256 v1.2.0
github.com/json-iterator/go v1.1.6
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)

0 comments on commit 571a4cc

Please sign in to comment.