@@ -55,8 +55,10 @@ func init() {
5555 versionRegex = regexp .MustCompile ("^" + semVerRegex + "$" )
5656}
5757
58- const num string = "0123456789"
59- const allowed string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" + num
58+ const (
59+ num string = "0123456789"
60+ allowed string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" + num
61+ )
6062
6163// StrictNewVersion parses a given version and returns an instance of Version or
6264// an error if unable to parse the version. Only parses valid semantic versions.
@@ -284,7 +286,6 @@ func (v Version) Metadata() string {
284286
285287// originalVPrefix returns the original 'v' prefix if any.
286288func (v Version ) originalVPrefix () string {
287-
288289 // Note, only lowercase v is supported as a prefix by the parser.
289290 if v .original != "" && v .original [:1 ] == "v" {
290291 return v .original [:1 ]
@@ -453,6 +454,23 @@ func (v Version) MarshalJSON() ([]byte, error) {
453454 return json .Marshal (v .String ())
454455}
455456
457+ // UnmarshalText implements the encoding.TextUnmarshaler interface.
458+ func (v * Version ) UnmarshalText (text []byte ) error {
459+ temp , err := NewVersion (string (text ))
460+ if err != nil {
461+ return err
462+ }
463+
464+ * v = * temp
465+
466+ return nil
467+ }
468+
469+ // MarshalText implements the encoding.TextMarshaler interface.
470+ func (v Version ) MarshalText () ([]byte , error ) {
471+ return []byte (v .String ()), nil
472+ }
473+
456474// Scan implements the SQL.Scanner interface.
457475func (v * Version ) Scan (value interface {}) error {
458476 var s string
@@ -487,7 +505,6 @@ func compareSegment(v, o uint64) int {
487505}
488506
489507func comparePrerelease (v , o string ) int {
490-
491508 // split the prelease versions by their part. The separator, per the spec,
492509 // is a .
493510 sparts := strings .Split (v , "." )
@@ -579,7 +596,6 @@ func comparePrePart(s, o string) int {
579596 return 1
580597 }
581598 return - 1
582-
583599}
584600
585601// Like strings.ContainsAny but does an only instead of any.
0 commit comments