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

Implement encoding.TextMarshaler and encoding.TextUnmarshaler #173

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 24 additions & 5 deletions version.go
Expand Up @@ -55,8 +55,10 @@ func init() {
versionRegex = regexp.MustCompile("^" + semVerRegex + "$")
}

const num string = "0123456789"
const allowed string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" + num
const (
num string = "0123456789"
allowed string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" + num
)
MarkRosemaker marked this conversation as resolved.
Show resolved Hide resolved

// StrictNewVersion parses a given version and returns an instance of Version or
// an error if unable to parse the version. Only parses valid semantic versions.
Expand Down Expand Up @@ -267,7 +269,6 @@ func (v Version) Metadata() string {

// originalVPrefix returns the original 'v' prefix if any.
func (v Version) originalVPrefix() string {

// Note, only lowercase v is supported as a prefix by the parser.
if v.original != "" && v.original[:1] == "v" {
return v.original[:1]
Expand Down Expand Up @@ -436,6 +437,26 @@ func (v Version) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String())
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (v *Version) UnmarshalText(text []byte) error {
temp, err := NewVersion(string(text))
if err != nil {
return err
}
v.major = temp.major
v.minor = temp.minor
v.patch = temp.patch
v.pre = temp.pre
v.metadata = temp.metadata
v.original = temp.original
return nil
}

// MarshalText implements the encoding.TextMarshaler interface.
func (v Version) MarshalText() ([]byte, error) {
return []byte(v.String()), nil
}

// Scan implements the SQL.Scanner interface.
func (v *Version) Scan(value interface{}) error {
var s string
Expand Down Expand Up @@ -470,7 +491,6 @@ func compareSegment(v, o uint64) int {
}

func comparePrerelease(v, o string) int {

// split the prelease versions by their part. The separator, per the spec,
// is a .
sparts := strings.Split(v, ".")
Expand Down Expand Up @@ -562,7 +582,6 @@ func comparePrePart(s, o string) int {
return 1
}
return -1

}

// Like strings.ContainsAny but does an only instead of any.
Expand Down
67 changes: 67 additions & 0 deletions version_test.go
Expand Up @@ -5,6 +5,10 @@ import (
"encoding/json"
"fmt"
"testing"

yamlGoccy "github.com/goccy/go-yaml"
MarkRosemaker marked this conversation as resolved.
Show resolved Hide resolved
yamlV2 "gopkg.in/yaml.v2"
yamlV3 "gopkg.in/yaml.v3"
)

func TestStrictNewVersion(t *testing.T) {
Expand Down Expand Up @@ -542,6 +546,69 @@ func TestJsonUnmarshal(t *testing.T) {
}
}

type testStruct struct {
MyVersion *Version `yaml:"myVersion"`
}

func TestYAMLMarshal(t *testing.T) {
t.Parallel()

sVer := "1.1.1"
x, err := StrictNewVersion(sVer)
if err != nil {
t.Errorf("creating version: %s", err)
}

for i, marshal := range []func(interface{}) ([]byte, error){
yamlGoccy.Marshal,
yamlV2.Marshal,
yamlV3.Marshal,
} {
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
t.Parallel()

out, err2 := marshal(testStruct{MyVersion: x})
if err2 != nil {
t.Errorf("Error marshaling version: %s", err2)
}

got := string(out)
want := fmt.Sprintf("myVersion: %s\n", sVer)
if got != want {
t.Errorf("Error marshaling unexpected marshaled content: got=%q want=%q", got, want)
}
})
}
}

func TestYAMLUnmarshal(t *testing.T) {
t.Parallel()

sVer := "1.1.1"

for i, unmarshal := range []func([]byte, interface{}) error{
yamlGoccy.Unmarshal,
yamlV2.Unmarshal,
yamlV3.Unmarshal,
} {
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
t.Parallel()

ts := &testStruct{}
err := unmarshal([]byte(fmt.Sprintf("myVersion: %s\n", sVer)), ts)
if err != nil {
t.Errorf("Error unmarshaling version: %s", err)
}

got := ts.MyVersion.String()
want := sVer
if got != want {
t.Errorf("Error unmarshaling unexpected object content: got=%q want=%q", got, want)
}
})
}
}

func TestSQLScanner(t *testing.T) {
sVer := "1.1.1"
x, err := StrictNewVersion(sVer)
Expand Down