Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add json handlers #93

Merged
merged 6 commits into from May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions version.go
Expand Up @@ -2,6 +2,7 @@ package version

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"regexp"
Expand Down Expand Up @@ -388,3 +389,26 @@ func (v *Version) String() string {
func (v *Version) Original() string {
return v.original
}

// UnmarshalJSON implements JSON.Unmarshaler interface.
func (v *Version) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
temp, err := NewVersion(s)
if err != nil {
return err
}
v.metadata = temp.metadata
v.pre = temp.pre
v.segments = temp.segments
v.si = temp.si
v.original = temp.original
return nil
}

// MarshalJSON implements JSON.Marshaler interface.
func (v Version) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String())
}
70 changes: 70 additions & 0 deletions version_test.go
@@ -1,6 +1,8 @@
package version

import (
"encoding/json"
"fmt"
"reflect"
"testing"
)
Expand Down Expand Up @@ -393,6 +395,74 @@ func TestVersionSegments64(t *testing.T) {
}
}

func TestJsonMarshal(t *testing.T) {
cases := []struct {
version string
err bool
}{
{"1.2.3", false},
{"1.2.0-x.Y.0+metadata", false},
{"1.2.0-x.Y.0+metadata-width-hypen", false},
{"1.2.3-rc1-with-hypen", false},
{"1.2.3.4", false},
{"1.2.0.4-x.Y.0+metadata", false},
{"1.2.0.4-x.Y.0+metadata-width-hypen", false},
{"1.2.0-X-1.2.0+metadata~dist", false},
{"1.2.3.4-rc1-with-hypen", false},
{"1.2.3.4", false},
}

for _, tc := range cases {
v, err := NewVersion(tc.version)
if err != nil {
t.Fatalf("err: %s", err)
}
parsed, err2 := json.Marshal(v)
if err2 != nil {
t.Fatalf("error marshaling version %q: %s", tc.version, err2)
}
actual := string(parsed)
expected := tc.version
if actual != expected && !tc.err {
t.Fatalf("Error marshaling unexpected marshaled content: actual=%q expected=%q", actual, expected)
}
}
}

func TestJsonUnmarshal(t *testing.T) {
cases := []struct {
version string
err bool
}{
{"1.2.3", false},
{"1.2.0-x.Y.0+metadata", false},
{"1.2.0-x.Y.0+metadata-width-hypen", false},
{"1.2.3-rc1-with-hypen", false},
{"1.2.3.4", false},
{"1.2.0.4-x.Y.0+metadata", false},
{"1.2.0.4-x.Y.0+metadata-width-hypen", false},
{"1.2.0-X-1.2.0+metadata~dist", false},
{"1.2.3.4-rc1-with-hypen", false},
{"1.2.3.4", false},
Copy link
Contributor Author

@jukie jukie May 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what values would make sense to force a failure here.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jukie,
Thanks for submitting this PR. I don't see much benefit in negative tests here because errors are only returned by code that's not specifically under test by this test function (the standard library json package and the parsing by NewVersion()). The code that really needs to be tested in your implementation is the transfer of data from the temporary struct to the receiver, and I think the tests you already have cover that logic.

}

for _, tc := range cases {
expected, err1 := NewVersion(tc.version)
if err1 != nil {
t.Fatalf("err: %s", err1)
}

actual := &Version{}
err := json.Unmarshal([]byte(fmt.Sprintf("%q", tc.version)), v)
if err != nil {
t.Errorf("error unmarshaling version: %s", err)
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("error unmarshaling, unexpected object content: actual=%q expected=%q", actual, expected)
}
}
}

func TestVersionString(t *testing.T) {
cases := [][]string{
{"1.2.3", "1.2.3"},
Expand Down