Skip to content

Commit

Permalink
Implement the Scan and driver.Value SQL interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
svanharmelen committed May 16, 2024
1 parent 73ddc63 commit 29469fb
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package version

import (
"bytes"
"database/sql/driver"
"fmt"
"reflect"
"regexp"
Expand Down Expand Up @@ -160,7 +161,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 @@ -405,3 +406,25 @@ 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 *Version:
*v = *src
return nil
case []byte:
return v.UnmarshalText(src)
case string:
return v.UnmarshalText([]byte(src))
case nil:
return nil
default:
return fmt.Errorf("Cannot convert %T to Version", src)
}
}

// Value implements the driver.Valuer interface.
func (v *Version) Value() (driver.Value, error) {
return v.String(), nil
}

0 comments on commit 29469fb

Please sign in to comment.