-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from bugst/yamlsupport
Added YAML support
- Loading branch information
Showing
5 changed files
with
165 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |