Skip to content

Commit

Permalink
adding json support for constraints and version
Browse files Browse the repository at this point in the history
this change adds support to marshal and unmarshal version constraints
and versions to JSON.
  • Loading branch information
xinau committed Aug 14, 2020
1 parent 59da58c commit 2523cfd
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
22 changes: 22 additions & 0 deletions constraint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package version

import (
"encoding/json"
"fmt"
"reflect"
"regexp"
Expand Down Expand Up @@ -202,3 +203,24 @@ func constraintPessimistic(v, c *Version) bool {
// If nothing has rejected the version by now, it's valid
return true
}

// MarshalJSON implements the json.Marshaler interface
func (c *Constraints) MarshalJSON() ([]byte, error) {
return json.Marshal(c.String())
}

// UnmarshalJSON implements the json.Unmarshaler interface
func (c *Constraints) UnmarshalJSON(data []byte) error {
var csStr string
if err := json.Unmarshal(data, &csStr); err != nil {
return err
}

nc, err := NewConstraint(csStr)
if err != nil {
return err
}
*c = nc

return nil
}
56 changes: 56 additions & 0 deletions constraint_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package version

import (
"encoding/json"
"testing"
)

Expand Down Expand Up @@ -124,3 +125,58 @@ func TestConstraintsString(t *testing.T) {
}
}
}

func TestConstraintsMarshalJSON(t *testing.T) {
cases := []struct {
constraint string
result string
}{
{">= 1.0, < 1.2", `"\u003e= 1.0, \u003c 1.2"`},
{"~> 1.0.7", `"~\u003e 1.0.7"`},
}

for _, tc := range cases {
c, err := NewConstraint(tc.constraint)
if err != nil {
t.Fatalf("err: %s", err)
}

actual, err := json.Marshal(&c)
if err != nil {
t.Fatalf("err: %s", err)
}

expected := tc.result
if string(actual) != expected {
t.Fatalf("Constraint: %s\nExpected: %s\nActual: %s",
tc.constraint, expected, actual)
}
}
}

func TestConstraintsUnmarshalJSON(t *testing.T) {
cases := []struct {
constraint string
result string
}{
{`">= 1.0, < 1.2"`, ">= 1.0, < 1.2"},
{`"~> 1.0.7"`, "~> 1.0.7"},
}

for _, tc := range cases {
var actual Constraints
if err := json.Unmarshal([]byte(tc.constraint), &actual); err != nil {
t.Fatalf("err: %s", err)
}

expected, err := NewConstraint(tc.result)
if err != nil {
t.Fatalf("err: %s", err)
}

if actual.String() != expected.String() {
t.Fatalf("Constraint: %s\nExpected: %s\nActual: %s",
tc.result, expected, actual)
}
}
}
22 changes: 22 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package version

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

// MarshalJSON implements the json.Marshaler interface
func (v *Version) MarshalJSON() ([]byte, error) {
return json.Marshal(v.original)
}

// UnmarshalJSON implements the json.Unmarshaler interface
func (v *Version) UnmarshalJSON(data []byte) error {
var vStr string
if err := json.Unmarshal(data, &vStr); err != nil {
return err
}

nv, err := NewVersion(vStr)
if err != nil {
return err
}
*v = *nv

return nil
}
56 changes: 56 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package version

import (
"encoding/json"
"reflect"
"testing"
)
Expand Down Expand Up @@ -622,3 +623,58 @@ func TestLessThanOrEqual(t *testing.T) {
}
}
}

func TestVersionMarshalJSON(t *testing.T) {
cases := []struct {
version string
result string
}{
{"1.7rc2", `"1.7rc2"`},
{"1.2.0", `"1.2.0"`},
}

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

actual, err := json.Marshal(&v)
if err != nil {
t.Fatalf("err: %s", err)
}

expected := tc.result
if string(actual) != expected {
t.Fatalf("Version: %s\nExpected: %s\nActual: %s",
tc.version, expected, actual)
}
}
}

func TestVersionUnmarshalJSON(t *testing.T) {
cases := []struct {
version string
result string
}{
{`"1.7rc2"`, "1.7rc2"},
{`"1.2.0"`, "1.2.0"},
}

for _, tc := range cases {
var actual Version
if err := json.Unmarshal([]byte(tc.version), &actual); err != nil {
t.Fatalf("err: %s", err)
}

expected, err := NewVersion(tc.result)
if err != nil {
t.Fatalf("err: %s", err)
}

if !reflect.DeepEqual(&actual, expected) {
t.Fatalf("Constraint: %s\nExpected: %#v\nActual: %#v",
tc.result, expected, actual)
}
}
}

0 comments on commit 2523cfd

Please sign in to comment.