Skip to content

Commit

Permalink
Implement the Scan and driver.Value SQL interfaces (#133)
Browse files Browse the repository at this point in the history
Co-authored-by: Radek Simko <[email protected]>
  • Loading branch information
svanharmelen and radeksimko authored May 22, 2024
1 parent e04a866 commit d55f214
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package version

import (
"bytes"
"database/sql/driver"
"fmt"
"regexp"
"strconv"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit d55f214

Please sign in to comment.