From 03c1776b43c31b1d56607bd85f10c57ba59d7f09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 12 Aug 2020 19:58:39 +0200 Subject: [PATCH] types: Add text (un)marshaler on FIL --- chain/types/fil.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/chain/types/fil.go b/chain/types/fil.go index 1d912d9c0..99a896e38 100644 --- a/chain/types/fil.go +++ b/chain/types/fil.go @@ -1,6 +1,7 @@ package types import ( + "encoding" "fmt" "math/big" "strings" @@ -27,6 +28,20 @@ func (f FIL) Format(s fmt.State, ch rune) { } } +func (f FIL) MarshalText() (text []byte, err error) { + return []byte(f.String()), nil +} + +func (f FIL) UnmarshalText(text []byte) error { + p, err := ParseFIL(string(text)) + if err != nil { + return err + } + + f.Int.Set(p.Int) + return nil +} + func ParseFIL(s string) (FIL, error) { suffix := strings.TrimLeft(s, ".1234567890") s = s[:len(s)-len(suffix)] @@ -61,3 +76,6 @@ func ParseFIL(s string) (FIL, error) { return FIL{r.Num()}, nil } + +var _ encoding.TextMarshaler = (*FIL)(nil) +var _ encoding.TextUnmarshaler = (*FIL)(nil)