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

Added YAML support #16

Merged
merged 3 commits into from
Jul 3, 2023
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ The `Version` and `RelaxedVersion` have the JSON un/marshaler implemented so the
## Binary/GOB encoding support

The `Version` and `RelaxedVersion` provides optimized `MarshalBinary`/`UnmarshalBinary` methods for binary encoding.

## Yaml parsable with `gopkg.in/yaml.v3`

The `Version` and `RelaxedVersion` have the YAML un/marshaler implemented so they can be YAML decoded/encoded with the excellent `gopkg.in/yaml.v3` library.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ module go.bug.st/relaxed-semver

go 1.17

require github.com/stretchr/testify v1.7.1
require (
github.com/stretchr/testify v1.7.1
gopkg.in/yaml.v3 v3.0.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
17 changes: 7 additions & 10 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ func TestJSONParseVersion(t *testing.T) {
fmt.Println(string(data))
require.NoError(t, err)

dump := fmt.Sprintf("%s,%s,%s,%s,%v,%s",
v.major, v.minor, v.patch,
v.prerelases, v.numericPrereleases,
v.builds)
require.Equal(t, "1,2,3,[aaa 4 5 6],[false true true true],[bbb 7 8 9]", dump)

var u Version
err = json.Unmarshal(data, &u)
require.NoError(t, err)

require.Equal(t, testVersion, v.String())
dump := fmt.Sprintf("%s,%s,%s,%s,%v,%s",
u.major, u.minor, u.patch,
u.prerelases, u.numericPrereleases,
u.builds)
require.Equal(t, "1,2,3,[aaa 4 5 6],[false true true true],[bbb 7 8 9]", dump)
require.Equal(t, testVersion, u.String())

err = json.Unmarshal([]byte(`"invalid"`), &u)
require.Error(t, err)
Expand All @@ -53,8 +51,7 @@ func TestJSONParseRelaxedVersion(t *testing.T) {
var u RelaxedVersion
err = json.Unmarshal(data, &u)
require.NoError(t, err)

require.Equal(t, testVersion, v.String())
require.Equal(t, testVersion, u.String())

err = json.Unmarshal([]byte(`"invalid"`), &u)
require.NoError(t, err)
Expand Down
54 changes: 54 additions & 0 deletions yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Copyright 2018-2022 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//

package semver

import (
"gopkg.in/yaml.v3"
)

// MarshalYAML implements yaml.Marshaler
func (v *Version) MarshalYAML() (interface{}, error) {
return v.String(), nil
}

// UnmarshalYAML implements yaml.Unmarshaler
func (v *Version) UnmarshalYAML(node *yaml.Node) error {
var versionString string
if err := node.Decode(&versionString); err != nil {
return err
}
parsed, err := Parse(versionString)
if err != nil {
return err
}

v.major = parsed.major
v.minor = parsed.minor
v.patch = parsed.patch
v.prerelases = parsed.prerelases
v.numericPrereleases = parsed.numericPrereleases
v.builds = parsed.builds
return nil
}

// MarshalYAML implements yaml.Marshaler
func (v *RelaxedVersion) MarshalYAML() (interface{}, error) {
return v.String(), nil
}

// UnmarshalYAML implements yaml.Unmarshaler
func (v *RelaxedVersion) UnmarshalYAML(node *yaml.Node) error {
var versionString string
if err := node.Decode(&versionString); err != nil {
return err
}
parsed := ParseRelaxed(versionString)

v.customversion = parsed.customversion
v.version = parsed.version
return nil
}
96 changes: 96 additions & 0 deletions yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Copyright 2018-2022 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//

package semver

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)

func TestYAMLParseVersion(t *testing.T) {
var versionIsYamlUnmarshaler yaml.Unmarshaler = MustParse("1.0.0")
var versionIsYamlMarshaler yaml.Marshaler = MustParse("1.0.0")
_ = versionIsYamlUnmarshaler
_ = versionIsYamlMarshaler

testVersion := "1.2.3-aaa.4.5.6+bbb.7.8.9"
v, err := Parse(testVersion)
require.NoError(t, err)

data, err := yaml.Marshal(v)
require.Equal(t, "1.2.3-aaa.4.5.6+bbb.7.8.9\n", string(data))
require.NoError(t, err)

var u Version
err = yaml.Unmarshal(data, &u)
require.NoError(t, err)

dump := fmt.Sprintf("%s,%s,%s,%s,%v,%s",
u.major, u.minor, u.patch,
u.prerelases, u.numericPrereleases,
u.builds)
require.Equal(t, "1,2,3,[aaa 4 5 6],[false true true true],[bbb 7 8 9]", dump)

require.Equal(t, testVersion, u.String())

err = yaml.Unmarshal([]byte(`"invalid"`), &u)
require.Error(t, err)

err = yaml.Unmarshal([]byte(`invalid:`), &u)
require.Error(t, err)
}

func TestYAMLParseRelaxedVersion(t *testing.T) {
var relaxedVersionIsYamlUnmarshaler yaml.Unmarshaler = ParseRelaxed("1.0.0")
var relaxedVersionIsYamlMarshaler yaml.Marshaler = ParseRelaxed("1.0.0")
_ = relaxedVersionIsYamlUnmarshaler
_ = relaxedVersionIsYamlMarshaler

testVersion := "1.2.3-aaa.4.5.6+bbb.7.8.9"

v := ParseRelaxed(testVersion)

data, err := yaml.Marshal(v)
require.NoError(t, err)
require.Equal(t, "1.2.3-aaa.4.5.6+bbb.7.8.9\n", string(data))

var u RelaxedVersion
err = yaml.Unmarshal(data, &u)
require.NoError(t, err)

require.Equal(t, testVersion, u.String())

err = yaml.Unmarshal([]byte(`"invalid"`), &u)
require.NoError(t, err)
require.Equal(t, "invalid", u.String())

err = yaml.Unmarshal([]byte(`invalid:`), &u)
require.Error(t, err)
}

func BenchmarkYAMLDecoding(b *testing.B) {
testVersion := "1.2.3-aaa.4.5.6+bbb.7.8.9"
v, _ := Parse(testVersion)
data, _ := yaml.Marshal(v)
var u Version
for i := 0; i < b.N; i++ {
yaml.Unmarshal(data, &u)
}
}

func BenchmarkYAMLDecodingRelaxed(b *testing.B) {
testVersion := "1.2.3-aaa.4.5.6+bbb.7.8.9"
v := ParseRelaxed(testVersion)
data, _ := yaml.Marshal(v)
var u RelaxedVersion
for i := 0; i < b.N; i++ {
yaml.Unmarshal(data, &u)
}
}