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

adding json support for constraints and version #75

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -390,3 +391,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 @@ -654,3 +655,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)
}
}
}