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

RFC 3339 support for both Marshal and Unmarshal. #204

Merged
merged 7 commits into from
Apr 5, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge branch 'master' into aren55555/rfc3339/fix
# Conflicts:
#	constants.go
#	models_test.go
#	request_test.go
aren55555 committed Apr 5, 2021
commit 5964bfebd35f3af55d545915de0066dba1730f7a
1 change: 1 addition & 0 deletions constants.go
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ const (
annotationRelation = "relation"
annotationOmitEmpty = "omitempty"
annotationISO8601 = "iso8601"
annotationRFC3339 = "rfc3339"
annotationSeperator = ","

rfc3339TimeFormat = time.RFC3339
26 changes: 26 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
@@ -23,6 +23,9 @@ 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)
@@ -446,12 +449,15 @@ 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
}
}
}
@@ -476,6 +482,26 @@ 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
}
aren55555 marked this conversation as resolved.
Show resolved Hide resolved

var at int64

if v.Kind() == reflect.Float64 {
You are viewing a condensed version of this merge commit. You can view the full changes here.