types: Add text (un)marshaler on FIL

This commit is contained in:
Łukasz Magiera 2020-08-12 19:58:39 +02:00
parent 78eaac6534
commit 03c1776b43

View File

@ -1,6 +1,7 @@
package types package types
import ( import (
"encoding"
"fmt" "fmt"
"math/big" "math/big"
"strings" "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) { func ParseFIL(s string) (FIL, error) {
suffix := strings.TrimLeft(s, ".1234567890") suffix := strings.TrimLeft(s, ".1234567890")
s = s[:len(s)-len(suffix)] s = s[:len(s)-len(suffix)]
@ -61,3 +76,6 @@ func ParseFIL(s string) (FIL, error) {
return FIL{r.Num()}, nil return FIL{r.Num()}, nil
} }
var _ encoding.TextMarshaler = (*FIL)(nil)
var _ encoding.TextUnmarshaler = (*FIL)(nil)