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.
|
// MarshalJSON implements the json.Marshaler interface for ClockTime.
|
||||||
func (t ClockTime) MarshalJSON() ([]byte, error) {
|
func (t ClockTime) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(t.String())
|
return json.Marshal("\"" + t.String() + "\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements the json.Unmarshaler interface for ClockTime.
|
// 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.
|
// MarshalGQL implements the graphql.Marshaler interface for ClockTime.
|
||||||
func (t ClockTime) MarshalGQL(w io.Writer) {
|
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.
|
// 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.
|
// Scan implements the database/sql.Scanner interface for ClockTime.
|
||||||
|
// Supports scanning from time.Time or []byte.
|
||||||
func (t *ClockTime) Scan(value any) error {
|
func (t *ClockTime) Scan(value any) error {
|
||||||
if value == nil {
|
if value == nil {
|
||||||
*t = ClockTime{}
|
*t = ClockTime{}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
rawTime, ok := value.(time.Time)
|
|
||||||
if !ok {
|
switch v := value.(type) {
|
||||||
return fmt.Errorf("ClockTime must be a time.Time, got %T", value)
|
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 = parsedTime
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("ClockTime.Scan: unsupported type %T", value)
|
||||||
}
|
}
|
||||||
*t = ClockTimeFromTime(rawTime)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user