Skip to content

Revert "Add RFC3339 timestamp" #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ const (
annotationRelation = "relation"
annotationOmitEmpty = "omitempty"
annotationISO8601 = "iso8601"
annotationRFC3339 = "rfc3339"
annotationSeperator = ","

rfc3339TimeFormat = "2006-01-02T15:04:05Z07:00"
iso8601TimeFormat = "2006-01-02T15:04:05Z"

// MediaType is the identifier for the JSON API media type
Expand Down
6 changes: 0 additions & 6 deletions models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ type Timestamp struct {
Next *time.Time `jsonapi:"attr,next,iso8601"`
}

type TimestampRFC3339 struct {
ID int `jsonapi:"primary,timestamps"`
Time time.Time `jsonapi:"attr,timestamp,rfc3339"`
Next *time.Time `jsonapi:"attr,next,rfc3339"`
}

type Car struct {
ID *string `jsonapi:"primary,cars"`
Make *string `jsonapi:"attr,make,omitempty"`
Expand Down
26 changes: 0 additions & 26 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ var (
// ErrInvalidISO8601 is returned when a struct has a time.Time type field and includes
// "iso8601" in the tag spec, but the JSON value was not an ISO8601 timestamp string.
ErrInvalidISO8601 = errors.New("Only strings can be parsed as dates, ISO8601 timestamps")
// ErrInvalidRFC3339 is returned when a struct has a time.Time type field and includes
// "rfc3339" in the tag spec, but the JSON value was not an RFC3339 timestamp string.
ErrInvalidRFC3339 = errors.New("Only strings can be parsed as dates, RFC3339 timestamps")
// ErrUnknownFieldNumberType is returned when the JSON value was a float
// (numeric) but the Struct field was a non numeric type (i.e. not int, uint,
// float, etc)
Expand Down Expand Up @@ -449,15 +446,12 @@ func handleStringSlice(attribute interface{}) (reflect.Value, error) {

func handleTime(attribute interface{}, args []string, fieldValue reflect.Value) (reflect.Value, error) {
var isIso8601 bool
var isRFC3339 bool
v := reflect.ValueOf(attribute)

if len(args) > 2 {
for _, arg := range args[2:] {
if arg == annotationISO8601 {
isIso8601 = true
} else if arg == annotationRFC3339 {
isRFC3339 = true
}
}
}
Expand All @@ -482,26 +476,6 @@ func handleTime(attribute interface{}, args []string, fieldValue reflect.Value)
return reflect.ValueOf(t), nil
}

if isRFC3339 {
var tm string
if v.Kind() == reflect.String {
tm = v.Interface().(string)
} else {
return reflect.ValueOf(time.Now()), ErrInvalidRFC3339
}

t, err := time.Parse(time.RFC3339, tm)
if err != nil {
return reflect.ValueOf(time.Now()), ErrInvalidRFC3339
}

if fieldValue.Kind() == reflect.Ptr {
return reflect.ValueOf(&t), nil
}

return reflect.ValueOf(t), nil
}

var at int64

if v.Kind() == reflect.Float64 {
Expand Down
72 changes: 0 additions & 72 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,78 +413,6 @@ func TestUnmarshalInvalidISO8601(t *testing.T) {
}
}

func TestUnmarshalParsesRFC3339(t *testing.T) {
payload := &OnePayload{
Data: &Node{
Type: "timestamps",
Attributes: map[string]interface{}{
"timestamp": "2020-03-16T23:09:59+00:00",
},
},
}

in := bytes.NewBuffer(nil)
json.NewEncoder(in).Encode(payload)

out := new(TimestampRFC3339)

if err := UnmarshalPayload(in, out); err != nil {
t.Fatal(err)
}

expected := time.Date(2020, 3, 16, 23, 9, 59, 0, time.UTC)

if !out.Time.Equal(expected) {
t.Fatal("Parsing the RFC3339 timestamp failed")
}
}

func TestUnmarshalParsesRFC3339TimePointer(t *testing.T) {
payload := &OnePayload{
Data: &Node{
Type: "timestamps",
Attributes: map[string]interface{}{
"next": "2020-03-16T23:09:59+00:00",
},
},
}

in := bytes.NewBuffer(nil)
json.NewEncoder(in).Encode(payload)

out := new(TimestampRFC3339)

if err := UnmarshalPayload(in, out); err != nil {
t.Fatal(err)
}

expected := time.Date(2020, 3, 16, 23, 9, 59, 0, time.UTC)

if !out.Next.Equal(expected) {
t.Fatal("Parsing the RFC3339 timestamp failed")
}
}

func TestUnmarshalInvalidRFC3339(t *testing.T) {
payload := &OnePayload{
Data: &Node{
Type: "timestamps",
Attributes: map[string]interface{}{
"timestamp": "17 Aug 16 08:027 MST",
},
},
}

in := bytes.NewBuffer(nil)
json.NewEncoder(in).Encode(payload)

out := new(TimestampRFC3339)

if err := UnmarshalPayload(in, out); err != ErrInvalidRFC3339 {
t.Fatalf("Expected ErrInvalidRFC3339, got %v", err)
}
}

func TestUnmarshalRelationshipsWithoutIncluded(t *testing.T) {
data, err := json.Marshal(samplePayloadWithoutIncluded())
if err != nil {
Expand Down