Skip to content

Commit

Permalink
scalars: Add Datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Renard authored and chris-ramon committed Jul 23, 2017
1 parent 3442aeb commit ef32a0f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
55 changes: 55 additions & 0 deletions scalars.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math"
"strconv"
"time"

"github.com/graphql-go/graphql/language/ast"
)
Expand Down Expand Up @@ -276,3 +277,57 @@ var ID = NewScalar(ScalarConfig{
return nil
},
})

func serializeDateTime(value interface{}) interface{} {
switch value := value.(type) {
case time.Time:
buff, err := value.MarshalText()
if err != nil {
return nil
}

return string(buff)
case *time.Time:
return serializeDateTime(*value)
default:
return nil
}

return nil
}

func unserializeDateTime(value interface{}) interface{} {
switch value := value.(type) {
case []byte:
t := time.Time{}
err := t.UnmarshalText(value)
if err != nil {
return nil
}

return t
case string:
return unserializeDateTime([]byte(value))
case *string:
return unserializeDateTime([]byte(*value))
default:
return nil
}

return nil
}

var DateTime = NewScalar(ScalarConfig{
Name: "DateTime",
Description: "The `DateTime` scalar type represents a DateTime." +
" The DateTime is serialized as an RFC 3339 quoted string",
Serialize: serializeDateTime,
ParseValue: unserializeDateTime,
ParseLiteral: func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.StringValue:
return valueAST.Value
}
return nil
},
})
34 changes: 34 additions & 0 deletions scalars_serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math"
"reflect"
"testing"
"time"

"github.com/graphql-go/graphql"
)
Expand All @@ -12,6 +13,7 @@ type intSerializationTest struct {
Value interface{}
Expected interface{}
}

type float64SerializationTest struct {
Value interface{}
Expected interface{}
Expand All @@ -22,6 +24,11 @@ type stringSerializationTest struct {
Expected string
}

type dateTimeSerializationTest struct {
Value interface{}
Expected interface{}
}

type boolSerializationTest struct {
Value interface{}
Expected bool
Expand Down Expand Up @@ -163,3 +170,30 @@ func TestTypeSystem_Scalar_SerializesOutputBoolean(t *testing.T) {
}
}
}

func TestTypeSystem_Scalar_SerializeOutputDateTime(t *testing.T) {
now := time.Now()
nowString, err := now.MarshalText()
if err != nil {
t.Fatal(err)
}

tests := []dateTimeSerializationTest{
{"string", nil},
{int(1), nil},
{float32(-1.1), nil},
{float64(-1.1), nil},
{true, nil},
{false, nil},
{now, string(nowString)},
{&now, string(nowString)},
}

for _, test := range tests {
val := graphql.DateTime.Serialize(test.Value)
if val != test.Expected {
reflectedValue := reflect.ValueOf(test.Value)
t.Fatalf("Failed DateTime.Serialize(%v(%v)), expected: %v, got %v", reflectedValue.Type(), test.Value, test.Expected, val)
}
}
}

0 comments on commit ef32a0f

Please sign in to comment.