Skip to content
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

Add missing RFC3339 to response, add tests #5

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 8 additions & 10 deletions models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ type WithPointer struct {
FloatVal *float32 `jsonapi:"attr,float-val"`
}

type Timestamp struct {
ID int `jsonapi:"primary,timestamps"`
Time time.Time `jsonapi:"attr,timestamp,iso8601"`
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 TimestampModel struct {
ID int `jsonapi:"primary,timestamps"`
DefaultV time.Time `jsonapi:"attr,defaultv"`
DefaultP *time.Time `jsonapi:"attr,defaultp"`
ISO8601V time.Time `jsonapi:"attr,iso8601v,iso8601"`
ISO8601P *time.Time `jsonapi:"attr,iso8601p,iso8601"`
RFC3339V time.Time `jsonapi:"attr,rfc3339v,rfc3339"`
RFC3339P *time.Time `jsonapi:"attr,rfc3339p,rfc3339"`
}

type Car struct {
Expand Down
21 changes: 7 additions & 14 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,29 +547,25 @@ 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
var isISO8601, isRFC3339 bool
v := reflect.ValueOf(attribute)

if len(args) > 2 {
for _, arg := range args[2:] {
if arg == annotationISO8601 {
isIso8601 = true
isISO8601 = true
} else if arg == annotationRFC3339 {
isRFC3339 = true
}
}
}

if isIso8601 {
var tm string
if v.Kind() == reflect.String {
tm = v.Interface().(string)
} else {
if isISO8601 {
if v.Kind() != reflect.String {
return reflect.ValueOf(time.Now()), ErrInvalidISO8601
}

t, err := time.Parse(iso8601TimeFormat, tm)
t, err := time.Parse(iso8601TimeFormat, v.Interface().(string))
if err != nil {
return reflect.ValueOf(time.Now()), ErrInvalidISO8601
}
Expand All @@ -582,14 +578,11 @@ func handleTime(attribute interface{}, args []string, fieldValue reflect.Value)
}

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

t, err := time.Parse(time.RFC3339, tm)
t, err := time.Parse(time.RFC3339, v.Interface().(string))
if err != nil {
return reflect.ValueOf(time.Now()), ErrInvalidRFC3339
}
Expand Down
Loading