Skip to content

Commit 00300c4

Browse files
authored
Merge pull request #167 from SimonTheLeg/master
add marshalling for constraints
2 parents 2084c82 + 1a07443 commit 00300c4

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

constraints.go

+28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package semver
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"regexp"
@@ -134,6 +135,33 @@ func (cs Constraints) String() string {
134135
return strings.Join(buf, " || ")
135136
}
136137

138+
// UnmarshalJSON implements JSON.Unmarshaler interface.
139+
func (cs *Constraints) UnmarshalJSON(b []byte) error {
140+
var s string
141+
if err := json.Unmarshal(b, &s); err != nil {
142+
return err
143+
}
144+
temp, err := NewConstraint(s)
145+
if err != nil {
146+
return err
147+
}
148+
*cs = *temp
149+
return nil
150+
}
151+
152+
// MarshalJSON implements JSON.Marshaler interface.
153+
func (cs Constraints) MarshalJSON() ([]byte, error) {
154+
// we need our own encoder so we don't escape '<' and '>' which json.Marshal does
155+
buf := new(bytes.Buffer)
156+
enc := json.NewEncoder(buf)
157+
enc.SetEscapeHTML(false)
158+
159+
if err := enc.Encode(cs.String()); err != nil {
160+
return nil, err
161+
}
162+
return bytes.TrimRight(buf.Bytes(), "\n"), nil
163+
}
164+
137165
var constraintOps map[string]cfunc
138166
var constraintRegex *regexp.Regexp
139167
var constraintRangeRegex *regexp.Regexp

constraints_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package semver
22

33
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
47
"reflect"
58
"testing"
69
)
@@ -676,3 +679,56 @@ func TestConstraintString(t *testing.T) {
676679
}
677680
}
678681
}
682+
683+
func TestJsonMarshalConstraints(t *testing.T) {
684+
tests := []struct {
685+
sCs string
686+
want string
687+
}{
688+
{"1.1.1", "1.1.1"},
689+
{">=1.1.1", ">=1.1.1"},
690+
{"<=1.1.1", "<=1.1.1"},
691+
}
692+
693+
for _, tc := range tests {
694+
cs, err := NewConstraint(tc.sCs)
695+
if err != nil {
696+
t.Errorf("Error creating constraints: %s", err)
697+
}
698+
buf := new(bytes.Buffer)
699+
enc := json.NewEncoder(buf)
700+
enc.SetEscapeHTML(false)
701+
err = enc.Encode(cs)
702+
if err != nil {
703+
t.Errorf("Error unmarshaling version: %s", err)
704+
}
705+
got := buf.String()
706+
want := fmt.Sprintf("%q\n", tc.want)
707+
if got != want {
708+
t.Errorf("Error marshaling unexpected marshaled content: got=%q want=%q", got, want)
709+
}
710+
}
711+
}
712+
713+
func TestJsonUnmarshalConstraints(t *testing.T) {
714+
tests := []struct {
715+
sCs string
716+
want string
717+
}{
718+
{"1.1.1", "1.1.1"},
719+
{">=1.2.3", ">=1.2.3"},
720+
{"<=1.2.3", "<=1.2.3"},
721+
}
722+
723+
for _, tc := range tests {
724+
cs := Constraints{}
725+
err := json.Unmarshal([]byte(fmt.Sprintf("%q", tc.sCs)), &cs)
726+
if err != nil {
727+
t.Errorf("Error unmarshaling constraints: %s", err)
728+
}
729+
got := cs.String()
730+
if got != tc.want {
731+
t.Errorf("Error unmarshaling unexpected object content: got=%q want=%q", got, tc.want)
732+
}
733+
}
734+
}

0 commit comments

Comments
 (0)