Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions duration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duration

import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -309,3 +310,27 @@ func (duration *Duration) UnmarshalJSON(source []byte) error {
*duration = *parsed
return nil
}

func (d *Duration) Scan(x interface{}) error {
switch v := x.(type) {
case []uint8:
parsed, err := Parse(string(v))
if err != nil {
return fmt.Errorf("failed to parse duration: %w", err)
}
d = parsed
case string:
parsed, err := Parse(v)
if err != nil {
return fmt.Errorf("failed to parse duration: %w", err)
}
d = parsed
default:
return fmt.Errorf("value must be a string")
}
return nil
}

func (d Duration) Value() (driver.Value, error) {
return d.String(), nil
}