Skip to content

Commit 82485a6

Browse files
authored
Add json handlers (#93)
* Add json marshaler/unmarshaler helpers * Add json test cases
1 parent 45914be commit 82485a6

File tree

2 files changed

+103
-8
lines changed

2 files changed

+103
-8
lines changed

version.go

+24
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package version
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"fmt"
67
"reflect"
78
"regexp"
@@ -388,3 +389,26 @@ func (v *Version) String() string {
388389
func (v *Version) Original() string {
389390
return v.original
390391
}
392+
393+
// UnmarshalJSON implements JSON.Unmarshaler interface.
394+
func (v *Version) UnmarshalJSON(b []byte) error {
395+
var s string
396+
if err := json.Unmarshal(b, &s); err != nil {
397+
return err
398+
}
399+
temp, err := NewVersion(s)
400+
if err != nil {
401+
return err
402+
}
403+
v.metadata = temp.metadata
404+
v.pre = temp.pre
405+
v.segments = temp.segments
406+
v.si = temp.si
407+
v.original = temp.original
408+
return nil
409+
}
410+
411+
// MarshalJSON implements JSON.Marshaler interface.
412+
func (v Version) MarshalJSON() ([]byte, error) {
413+
return json.Marshal(v.String())
414+
}

version_test.go

+79-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package version
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"reflect"
57
"testing"
68
)
@@ -21,13 +23,13 @@ func TestNewVersion(t *testing.T) {
2123
{"1.2-beta.5", false},
2224
{"\n1.2", true},
2325
{"1.2.0-x.Y.0+metadata", false},
24-
{"1.2.0-x.Y.0+metadata-width-hypen", false},
25-
{"1.2.3-rc1-with-hypen", false},
26+
{"1.2.0-x.Y.0+metadata-width-hyphen", false},
27+
{"1.2.3-rc1-with-hyphen", false},
2628
{"1.2.3.4", false},
2729
{"1.2.0.4-x.Y.0+metadata", false},
28-
{"1.2.0.4-x.Y.0+metadata-width-hypen", false},
30+
{"1.2.0.4-x.Y.0+metadata-width-hyphen", false},
2931
{"1.2.0-X-1.2.0+metadata~dist", false},
30-
{"1.2.3.4-rc1-with-hypen", false},
32+
{"1.2.3.4-rc1-with-hyphen", false},
3133
{"1.2.3.4", false},
3234
{"v1.2.3", false},
3335
{"foo1.2.3", true},
@@ -62,13 +64,13 @@ func TestNewSemver(t *testing.T) {
6264
{"1.2-beta.5", false},
6365
{"\n1.2", true},
6466
{"1.2.0-x.Y.0+metadata", false},
65-
{"1.2.0-x.Y.0+metadata-width-hypen", false},
66-
{"1.2.3-rc1-with-hypen", false},
67+
{"1.2.0-x.Y.0+metadata-width-hyphen", false},
68+
{"1.2.3-rc1-with-hyphen", false},
6769
{"1.2.3.4", false},
6870
{"1.2.0.4-x.Y.0+metadata", false},
69-
{"1.2.0.4-x.Y.0+metadata-width-hypen", false},
71+
{"1.2.0.4-x.Y.0+metadata-width-hyphen", false},
7072
{"1.2.0-X-1.2.0+metadata~dist", false},
71-
{"1.2.3.4-rc1-with-hypen", false},
73+
{"1.2.3.4-rc1-with-hyphen", false},
7274
{"1.2.3.4", false},
7375
{"v1.2.3", false},
7476
{"foo1.2.3", true},
@@ -393,6 +395,75 @@ func TestVersionSegments64(t *testing.T) {
393395
}
394396
}
395397

398+
func TestJsonMarshal(t *testing.T) {
399+
cases := []struct {
400+
version string
401+
err bool
402+
}{
403+
{"1.2.3", false},
404+
{"1.2.0-x.Y.0+metadata", false},
405+
{"1.2.0-x.Y.0+metadata-width-hyphen", false},
406+
{"1.2.3-rc1-with-hyphen", false},
407+
{"1.2.3.4", false},
408+
{"1.2.0.4-x.Y.0+metadata", false},
409+
{"1.2.0.4-x.Y.0+metadata-width-hyphen", false},
410+
{"1.2.0-X-1.2.0+metadata~dist", false},
411+
{"1.2.3.4-rc1-with-hyphen", false},
412+
{"1.2.3.4", false},
413+
}
414+
415+
for _, tc := range cases {
416+
v, err1 := NewVersion(tc.version)
417+
if err1 != nil {
418+
t.Fatalf("error for version %q: %s", tc.version, err1)
419+
}
420+
421+
parsed, err2 := json.Marshal(v)
422+
if err2 != nil {
423+
t.Fatalf("error marshaling version %q: %s", tc.version, err2)
424+
}
425+
result := string(parsed)
426+
expected := fmt.Sprintf("%q", tc.version)
427+
if result != expected && !tc.err {
428+
t.Fatalf("Error marshaling unexpected marshaled content: result=%q expected=%q", result, expected)
429+
}
430+
}
431+
}
432+
433+
func TestJsonUnmarshal(t *testing.T) {
434+
cases := []struct {
435+
version string
436+
err bool
437+
}{
438+
{"1.2.3", false},
439+
{"1.2.0-x.Y.0+metadata", false},
440+
{"1.2.0-x.Y.0+metadata-width-hyphen", false},
441+
{"1.2.3-rc1-with-hyphen", false},
442+
{"1.2.3.4", false},
443+
{"1.2.0.4-x.Y.0+metadata", false},
444+
{"1.2.0.4-x.Y.0+metadata-width-hyphen", false},
445+
{"1.2.0-X-1.2.0+metadata~dist", false},
446+
{"1.2.3.4-rc1-with-hyphen", false},
447+
{"1.2.3.4", false},
448+
}
449+
450+
for _, tc := range cases {
451+
expected, err1 := NewVersion(tc.version)
452+
if err1 != nil {
453+
t.Fatalf("err: %s", err1)
454+
}
455+
456+
actual := &Version{}
457+
err2 := json.Unmarshal([]byte(fmt.Sprintf("%q", tc.version)), actual)
458+
if err2 != nil {
459+
t.Fatalf("error unmarshaling version: %s", err2)
460+
}
461+
if !reflect.DeepEqual(actual, expected) {
462+
t.Fatalf("error unmarshaling, unexpected object content: actual=%q expected=%q", actual, expected)
463+
}
464+
}
465+
}
466+
396467
func TestVersionString(t *testing.T) {
397468
cases := [][]string{
398469
{"1.2.3", "1.2.3"},

0 commit comments

Comments
 (0)