From 89efb154c1c71c7ed83f910d0fa0a74e5055adc6 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 21 Nov 2023 23:59:57 +0100 Subject: [PATCH] Fixed JSON/YAML/Binary parsing --- binary.go | 1 + binary_test.go | 12 ++++++++++++ json.go | 1 + json_test.go | 4 ++++ yaml.go | 1 + yaml_test.go | 4 ++++ 6 files changed, 23 insertions(+) diff --git a/binary.go b/binary.go index 74ecae8..141fb2d 100644 --- a/binary.go +++ b/binary.go @@ -53,6 +53,7 @@ func (v *Version) UnmarshalBinary(data []byte) error { buff, data = decodeArray(data) v.raw = string(buff) + v.bytes = []byte(v.raw) v.major, data = decodeInt(data) v.minor, data = decodeInt(data) v.patch, data = decodeInt(data) diff --git a/binary_test.go b/binary_test.go index 4d66d1a..666f964 100644 --- a/binary_test.go +++ b/binary_test.go @@ -35,6 +35,18 @@ func TestGOBEncoderVersion(t *testing.T) { require.Equal(t, dumpV, dumpU) require.Equal(t, testVersion, u.String()) + + { + dataV := new(bytes.Buffer) + dataU := new(bytes.Buffer) + require.NoError(t, gob.NewEncoder(dataV).Encode(MustParse("1.6.2"))) + require.NoError(t, gob.NewEncoder(dataU).Encode(MustParse("1.6.3"))) + + var v, u *Version + require.NoError(t, gob.NewDecoder(dataV).Decode(&v)) + require.NoError(t, gob.NewDecoder(dataU).Decode(&u)) + require.True(t, u.GreaterThan(v)) + } } func TestGOBEncoderRelaxedVersion(t *testing.T) { diff --git a/json.go b/json.go index 7c1e766..c72e4b1 100644 --- a/json.go +++ b/json.go @@ -27,6 +27,7 @@ func (v *Version) UnmarshalJSON(data []byte) error { } v.raw = parsed.raw + v.bytes = []byte(v.raw) v.major = parsed.major v.minor = parsed.minor v.patch = parsed.patch diff --git a/json_test.go b/json_test.go index 321c5ef..0c4a4fb 100644 --- a/json_test.go +++ b/json_test.go @@ -36,6 +36,10 @@ func TestJSONParseVersion(t *testing.T) { err = json.Unmarshal([]byte(`123`), &u) require.Error(t, err) + + require.NoError(t, json.Unmarshal([]byte(`"1.6.2"`), &v)) + require.NoError(t, json.Unmarshal([]byte(`"1.6.3"`), &u)) + require.True(t, u.GreaterThan(v)) } func TestJSONParseRelaxedVersion(t *testing.T) { diff --git a/yaml.go b/yaml.go index c642866..fc8e942 100644 --- a/yaml.go +++ b/yaml.go @@ -27,6 +27,7 @@ func (v *Version) UnmarshalYAML(node *yaml.Node) error { } v.raw = parsed.raw + v.bytes = []byte(v.raw) v.major = parsed.major v.minor = parsed.minor v.patch = parsed.patch diff --git a/yaml_test.go b/yaml_test.go index 8d196ab..255c617 100644 --- a/yaml_test.go +++ b/yaml_test.go @@ -42,6 +42,10 @@ func TestYAMLParseVersion(t *testing.T) { err = yaml.Unmarshal([]byte(`invalid:`), &u) require.Error(t, err) + + require.NoError(t, yaml.Unmarshal([]byte(`"1.6.2"`), &v)) + require.NoError(t, yaml.Unmarshal([]byte(`"1.6.3"`), &u)) + require.True(t, u.GreaterThan(v)) } func TestYAMLParseRelaxedVersion(t *testing.T) {