Skip to content

Commit

Permalink
Revert "Add RFC3339 timestamp" (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
omarismail authored Apr 13, 2021
1 parent 95d5725 commit c4c8211
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 106 deletions.
2 changes: 0 additions & 2 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ const (
annotationLinks = "links"
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 @@ -548,15 +545,12 @@ func handleLinks(attribute interface{}, args []string, fieldValue reflect.Value)

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 @@ -581,26 +575,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

0 comments on commit c4c8211

Please sign in to comment.