Skip to content

Commit d55f214

Browse files
Implement the Scan and driver.Value SQL interfaces (#133)
Co-authored-by: Radek Simko <[email protected]>
1 parent e04a866 commit d55f214

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

version.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package version
55

66
import (
77
"bytes"
8+
"database/sql/driver"
89
"fmt"
910
"regexp"
1011
"strconv"
@@ -161,7 +162,7 @@ func (v *Version) Compare(other *Version) int {
161162
// this means Other had the lower specificity
162163
// Check to see if the remaining segments in Self are all zeros -
163164
if !allZero(segmentsSelf[i:]) {
164-
//if not, it means that Self has to be greater than Other
165+
// if not, it means that Self has to be greater than Other
165166
return 1
166167
}
167168
break
@@ -421,3 +422,20 @@ func (v *Version) UnmarshalText(b []byte) error {
421422
func (v *Version) MarshalText() ([]byte, error) {
422423
return []byte(v.String()), nil
423424
}
425+
426+
// Scan implements the sql.Scanner interface.
427+
func (v *Version) Scan(src interface{}) error {
428+
switch src := src.(type) {
429+
case string:
430+
return v.UnmarshalText([]byte(src))
431+
case nil:
432+
return nil
433+
default:
434+
return fmt.Errorf("cannot scan %T as Version", src)
435+
}
436+
}
437+
438+
// Value implements the driver.Valuer interface.
439+
func (v *Version) Value() (driver.Value, error) {
440+
return v.String(), nil
441+
}

0 commit comments

Comments
 (0)