From f803df294d2e4ab2c4e7252573d3e716f8b7472f Mon Sep 17 00:00:00 2001 From: simlecode <69969590+simlecode@users.noreply.github.com> Date: Wed, 29 Mar 2023 15:00:45 +0800 Subject: [PATCH] fix: use FIL pointer to unmarshaltext --- venus-shared/actors/types/bigint_fil.go | 2 +- venus-shared/types/bigint_fil_test.go | 35 +++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/venus-shared/actors/types/bigint_fil.go b/venus-shared/actors/types/bigint_fil.go index 95e1ba038e..233f19f62d 100644 --- a/venus-shared/actors/types/bigint_fil.go +++ b/venus-shared/actors/types/bigint_fil.go @@ -80,7 +80,7 @@ func (f FIL) MarshalText() (text []byte, err error) { return []byte(f.String()), nil } -func (f FIL) UnmarshalText(text []byte) error { +func (f *FIL) UnmarshalText(text []byte) error { p, err := ParseFIL(string(text)) if err != nil { return err diff --git a/venus-shared/types/bigint_fil_test.go b/venus-shared/types/bigint_fil_test.go index 068a30e24b..82e053f4ca 100644 --- a/venus-shared/types/bigint_fil_test.go +++ b/venus-shared/types/bigint_fil_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "github.com/BurntSushi/toml" tf "github.com/filecoin-project/venus/pkg/testhelpers/testflags" "github.com/filecoin-project/venus/venus-shared/testutil" "github.com/filecoin-project/venus/venus-shared/types/params" @@ -192,7 +193,7 @@ func TestFilShort(t *testing.T) { } } -func TestMarshal(t *testing.T) { +func TestJSONMarshal(t *testing.T) { tf.UnitTest(t) type A struct { Fil FIL @@ -208,7 +209,7 @@ func TestMarshal(t *testing.T) { fmt.Println(string(aBytes)) } -func TestUnMarshal(t *testing.T) { +func TestJSONUnMarshal(t *testing.T) { tf.UnitTest(t) type A struct { Fil FIL @@ -237,3 +238,33 @@ func TestUnMarshal(t *testing.T) { require.Equal(t, a.Fil.String(), s.expect.String()) } } + +func TestTextUnMarshal(t *testing.T) { + tf.UnitTest(t) + type A struct { + Fil FIL + } + bigFIl, _ := big.NewInt(0).SetString("100000000000000000000", 10) + for _, s := range []struct { + fil string + expect FIL + }{ + { + fil: `Fil = "0.000000000001 FIL"`, + expect: FIL{Int: big.NewInt(1000000)}, + }, + { + fil: `Fil = "1 FIL"`, + expect: FIL{Int: big.NewInt(1000000000000000000)}, + }, + { + fil: `Fil = "100 FIL"`, + expect: FIL{Int: bigFIl}, + }, + } { + a := A{} + err := toml.Unmarshal([]byte(s.fil), &a) + require.NoError(t, err) + require.Equal(t, a.Fil.String(), s.expect.String()) + } +}