support unmarshaling from []byte
This commit is contained in:
+16
-6
@@ -88,7 +88,7 @@ func (t ClockTime) After(other ClockTime) bool {
|
||||
|
||||
// MarshalJSON implements the json.Marshaler interface for ClockTime.
|
||||
func (t ClockTime) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(t.String())
|
||||
return json.Marshal("\"" + t.String() + "\"")
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements the json.Unmarshaler interface for ClockTime.
|
||||
@@ -107,7 +107,7 @@ func (t *ClockTime) UnmarshalJSON(data []byte) error {
|
||||
|
||||
// MarshalGQL implements the graphql.Marshaler interface for ClockTime.
|
||||
func (t ClockTime) MarshalGQL(w io.Writer) {
|
||||
fmt.Fprint(w, t.String())
|
||||
fmt.Fprint(w, "\""+t.String()+"\"")
|
||||
}
|
||||
|
||||
// UnmarshalGQL implements the graphql.Unmarshaler interface for ClockTime.
|
||||
@@ -135,15 +135,25 @@ func (t ClockTime) Value() (driver.Value, error) {
|
||||
}
|
||||
|
||||
// Scan implements the database/sql.Scanner interface for ClockTime.
|
||||
// Supports scanning from time.Time or []byte.
|
||||
func (t *ClockTime) Scan(value any) error {
|
||||
if value == nil {
|
||||
*t = ClockTime{}
|
||||
return nil
|
||||
}
|
||||
rawTime, ok := value.(time.Time)
|
||||
if !ok {
|
||||
return fmt.Errorf("ClockTime must be a time.Time, got %T", value)
|
||||
|
||||
switch v := value.(type) {
|
||||
case time.Time:
|
||||
*t = ClockTimeFromTime(v)
|
||||
case []byte:
|
||||
parsedTime, err := ClockTimeFromString(string(v))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse ClockTime from string: %w", err)
|
||||
}
|
||||
*t = ClockTimeFromTime(rawTime)
|
||||
*t = parsedTime
|
||||
default:
|
||||
return fmt.Errorf("ClockTime.Scan: unsupported type %T", value)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user