Created
January 7, 2020 10:50
-
-
Save gigo1980/e77549d19aa807b5e3bfc84762049cf9 to your computer and use it in GitHub Desktop.
gqlgen ISO8601
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Holds (un-)marshalling methods to handle graphql scalars in custom ways | |
package model | |
import ( | |
"fmt" | |
"github.com/99designs/gqlgen/graphql" | |
"github.com/google/uuid" | |
"io" | |
"strings" | |
"time" | |
) | |
type Time time.Time | |
type Date time.Time | |
type DateTime time.Time | |
type ID uuid.UUID | |
// Creates a marshaller which converts a time.Time (time) to a string | |
func MashalTime(time time.Time) graphql.Marshaler { | |
return graphql.WriterFunc(func(w io.Writer) { | |
_, e := io.WriteString(w, fmt.Sprintf("%s%s%s", "\"", time.Format("15:04:05-0700"), "\"")) | |
if e != nil { | |
panic(e) | |
} | |
}) | |
} | |
// Unmarshalls a string to a time.Time (time) | |
func UnmarshalTime(v interface{}) (time.Time, error) { | |
str, ok := v.(string) | |
if !ok { | |
return time.Time{}, fmt.Errorf("time must be strings") | |
} | |
withoutQuotes := strings.ReplaceAll(str, "\"", "") | |
i, err := time.Parse("15:04:05-0700", withoutQuotes) | |
if err != nil { | |
i, err = time.Parse("15:04:05", withoutQuotes) | |
} | |
return i, err | |
} | |
// Creates a marshaller which converts a time.Time (date) to a string | |
func MashalDate(date time.Time) graphql.Marshaler { | |
return graphql.WriterFunc(func(w io.Writer) { | |
_, e := io.WriteString(w, fmt.Sprintf("%s%s%s", "\"", date.Format("2006-01-02"), "\"")) | |
if e != nil { | |
panic(e) | |
} | |
}) | |
} | |
// Unmarshalls a string to a time.Time (date) | |
func UnmarshalDate(v interface{}) (time.Time, error) { | |
str, ok := v.(string) | |
if !ok { | |
return time.Time{}, fmt.Errorf("date must be strings") | |
} | |
withoutQuotes := strings.ReplaceAll(str, "\"", "") | |
i, err := time.Parse("2006-01-02", withoutQuotes) | |
if err != nil { | |
i, err = time.Parse("20060102", withoutQuotes) | |
} | |
return i, err | |
} | |
// Creates a marshaller which converts a time.Time (date time) to a string | |
func MashalDateTime(dateTime time.Time) graphql.Marshaler { | |
return graphql.WriterFunc(func(w io.Writer) { | |
_, e := io.WriteString(w, fmt.Sprintf("%s%s%s", "\"", dateTime.Format("2006-01-02T15:04:05-0700"), "\"")) | |
if e != nil { | |
panic(e) | |
} | |
}) | |
} | |
// Unmarshalls a string to a time.Time (date time) | |
func UnmarshalDateTime(v interface{}) (time.Time, error) { | |
str, ok := v.(string) | |
if !ok { | |
return time.Time{}, fmt.Errorf("datetime must be strings") | |
} | |
withoutQuotes := strings.ReplaceAll(str, "\"", "") | |
i, err := time.Parse("2006-01-02T15:04:05-0700", withoutQuotes) | |
if err != nil { | |
i, err = time.Parse("2006-01-02T15:04:05", withoutQuotes) | |
} | |
return i, err | |
} | |
// Creates a marshaller which converts a uuid to a string | |
func MarshalID(id uuid.UUID) graphql.Marshaler { | |
return graphql.WriterFunc(func(w io.Writer) { | |
_, e := io.WriteString(w, fmt.Sprintf("%s%s%s", "\"", id.String(), "\"")) | |
if e != nil { | |
panic(e) | |
} | |
}) | |
} | |
// Unmarshalls a string to a uuid | |
func UnmarshalID(v interface{}) (uuid.UUID, error) { | |
str, ok := v.(string) | |
if !ok { | |
return uuid.UUID{}, fmt.Errorf("ids must be strings") | |
} | |
withoutQuotes := strings.ReplaceAll(str, "\"", "") | |
i, err := uuid.Parse(withoutQuotes) | |
return i, err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment