From d55f21493f82b092e4869cfeb94b4602252d811e Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Wed, 22 May 2024 16:57:36 +0200 Subject: [PATCH] Implement the Scan and driver.Value SQL interfaces (#133) Co-authored-by: Radek Simko --- version.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/version.go b/version.go index 36f5729..7c683c2 100644 --- a/version.go +++ b/version.go @@ -5,6 +5,7 @@ package version import ( "bytes" + "database/sql/driver" "fmt" "regexp" "strconv" @@ -161,7 +162,7 @@ func (v *Version) Compare(other *Version) int { // this means Other had the lower specificity // Check to see if the remaining segments in Self are all zeros - if !allZero(segmentsSelf[i:]) { - //if not, it means that Self has to be greater than Other + // if not, it means that Self has to be greater than Other return 1 } break @@ -421,3 +422,20 @@ func (v *Version) UnmarshalText(b []byte) error { func (v *Version) MarshalText() ([]byte, error) { return []byte(v.String()), nil } + +// Scan implements the sql.Scanner interface. +func (v *Version) Scan(src interface{}) error { + switch src := src.(type) { + case string: + return v.UnmarshalText([]byte(src)) + case nil: + return nil + default: + return fmt.Errorf("cannot scan %T as Version", src) + } +} + +// Value implements the driver.Valuer interface. +func (v *Version) Value() (driver.Value, error) { + return v.String(), nil +}