Skip to content

Commit

Permalink
Updated types.Version.MarshalJSON for newly implemented Stringer inte…
Browse files Browse the repository at this point in the history
…rface
  • Loading branch information
twystd committed Jan 5, 2021
1 parent 74c46a9 commit 44f0222
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion types/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (v *Version) UnmarshalUT0311L0x(bytes []byte) (interface{}, error) {
}

func (v Version) MarshalJSON() ([]byte, error) {
return json.Marshal(fmt.Sprintf("%04x", v))
return json.Marshal(fmt.Sprintf("%04x", uint16(v)))
}

func (v *Version) UnmarshalJSON(bytes []byte) error {
Expand Down
24 changes: 24 additions & 0 deletions types/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,27 @@ func TestVersionStringer(t *testing.T) {
t.Errorf("Invalid string value - expected:%s, got:%s", "6.62", s)
}
}

func TestMarshalJSON(t *testing.T) {
v := Version(0x0662)
b, err := v.MarshalJSON()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if string(b) != `"0662"` {
t.Errorf("Invalid JSON value - expected:%s, got:%s", `"0662"`, string(b))
}
}

func TestUnmarshalJSON(t *testing.T) {
v := Version(0)
err := v.UnmarshalJSON([]byte(`"0662"`))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if uint16(v) != 0x0662 {
t.Errorf("Invalid version - expected:%v, got:%v", "0662", v)
}
}

0 comments on commit 44f0222

Please sign in to comment.