Skip to content

Commit 351efaa

Browse files
authored
Merge pull request #4 from gaze-network/feat/add-marshalers
Add text and json marshalers and unmarshalers
2 parents 3193f92 + a857213 commit 351efaa

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

uint128.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,38 @@ func FromString(s string) (u Uint128, err error) {
525525
_, err = fmt.Sscan(s, &u)
526526
return
527527
}
528+
529+
// MarshalText implements encoding.TextMarshaler.
530+
func (u Uint128) MarshalText() ([]byte, error) {
531+
return []byte(u.String()), nil
532+
}
533+
534+
// UnmarshalText implements encoding.TextUnmarshaler.
535+
func (u *Uint128) UnmarshalText(text []byte) error {
536+
v, err := FromString(string(text))
537+
if err != nil {
538+
return err
539+
}
540+
*u = v
541+
return nil
542+
}
543+
544+
// MarshalJSON implements json.Marshaler.
545+
func (u Uint128) MarshalJSON() ([]byte, error) {
546+
return []byte(`"` + u.String() + `"`), nil
547+
}
548+
549+
// UnmarshalJSON implements json.Unmarshaler.
550+
func (u *Uint128) UnmarshalJSON(data []byte) error {
551+
// data may be a JSON string or a JSON number
552+
// check if data is a JSON string first
553+
if len(data) > 0 && data[0] == '"' && data[len(data)-1] == '"' {
554+
data = data[1 : len(data)-1]
555+
}
556+
v, err := FromString(string(data))
557+
if err != nil {
558+
return err
559+
}
560+
*u = v
561+
return nil
562+
}

uint128_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,67 @@ func TestFromBytesBE(t *testing.T) {
524524
t.Fatalf("mismatch:\n%v !=\n%v", u1, u2)
525525
}
526526
}
527+
528+
func TestMarshal(t *testing.T) {
529+
number := "42540766411282592856903984951653826562"
530+
531+
u, err := FromString(number)
532+
if err != nil {
533+
t.Fatal(err)
534+
}
535+
536+
btext, err := u.MarshalText()
537+
if err != nil {
538+
t.Fatal(err)
539+
}
540+
bjson, err := u.MarshalJSON()
541+
if err != nil {
542+
t.Fatal(err)
543+
}
544+
545+
expectedText := number
546+
if string(btext) != expectedText {
547+
t.Fatalf("text mismatch:\n%v !=\n%v", string(btext), expectedText)
548+
}
549+
expectedJSON := `"` + number + `"`
550+
if string(bjson) != expectedJSON {
551+
t.Fatalf("json mismatch:\n%v !=\n%v", string(bjson), expectedJSON)
552+
}
553+
}
554+
555+
func TestUnmarshal(t *testing.T) {
556+
number := "42540766411282592856903984951653826562"
557+
558+
var u Uint128
559+
err := u.UnmarshalText([]byte(number))
560+
if err != nil {
561+
t.Fatal(err)
562+
}
563+
564+
expected, err := FromString(number)
565+
if err != nil {
566+
t.Fatal(err)
567+
}
568+
if u != expected {
569+
t.Fatalf("text mismatch:\n%v !=\n%v", u, expected)
570+
}
571+
572+
err = u.UnmarshalText([]byte("invalid"))
573+
if err == nil {
574+
t.Fatal("expected error")
575+
}
576+
577+
err = u.UnmarshalJSON([]byte(`"` + number + `"`))
578+
if err != nil {
579+
t.Fatal(err)
580+
}
581+
582+
if u != expected {
583+
t.Fatalf("json mismatch:\n%v !=\n%v", u, expected)
584+
}
585+
586+
err = u.UnmarshalJSON([]byte(`"invalid"`))
587+
if err == nil {
588+
t.Fatal("expected error")
589+
}
590+
}

0 commit comments

Comments
 (0)