You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
656 B
30 lines
656 B
package scalars
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/99designs/gqlgen/graphql"
|
|
"io"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func MarshalDuration(d time.Duration) graphql.Marshaler {
|
|
return graphql.WriterFunc(func(w io.Writer) {
|
|
_, _ = w.Write([]byte(strconv.Itoa(int(d.Milliseconds()))))
|
|
})
|
|
}
|
|
|
|
func UnmarshalDuration(v interface{}) (time.Duration, error) {
|
|
switch v := v.(type) {
|
|
case string:
|
|
return time.ParseDuration(v)
|
|
case int:
|
|
return time.Millisecond * time.Duration(v), nil
|
|
case int64:
|
|
return time.Millisecond * time.Duration(v), nil
|
|
case float64:
|
|
return time.Millisecond * time.Duration(v), nil
|
|
default:
|
|
return 0, fmt.Errorf("%T is not a bool", v)
|
|
}
|
|
}
|