Skip to content

Commit

Permalink
Merge pull request #1404 from xanzy/feature/iso-time
Browse files Browse the repository at this point in the history
Tweak the ISOTime marshaller
  • Loading branch information
svanharmelen committed Mar 9, 2022
2 parents e63371f + 8daa0de commit b3f55a4
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"net/url"
"reflect"
"strconv"
"time"
)
Expand Down Expand Up @@ -119,7 +120,7 @@ func (a *ApproverIDsValue) EncodeValues(key string, v *url.Values) error {
}

// MarshalJSON implements the json.Marshaler interface
func (a *ApproverIDsValue) MarshalJSON() ([]byte, error) {
func (a ApproverIDsValue) MarshalJSON() ([]byte, error) {
return json.Marshal(a.value)
}

Expand Down Expand Up @@ -155,7 +156,7 @@ func (a *AssigneeIDValue) EncodeValues(key string, v *url.Values) error {
}

// MarshalJSON implements the json.Marshaler interface
func (a *AssigneeIDValue) MarshalJSON() ([]byte, error) {
func (a AssigneeIDValue) MarshalJSON() ([]byte, error) {
return json.Marshal(a.value)
}

Expand Down Expand Up @@ -191,7 +192,7 @@ func (a *ReviewerIDValue) EncodeValues(key string, v *url.Values) error {
}

// MarshalJSON implements the json.Marshaler interface
func (a *ReviewerIDValue) MarshalJSON() ([]byte, error) {
func (a ReviewerIDValue) MarshalJSON() ([]byte, error) {
return json.Marshal(a.value)
}

Expand Down Expand Up @@ -362,6 +363,10 @@ const iso8601 = "2006-01-02"

// MarshalJSON implements the json.Marshaler interface
func (t ISOTime) MarshalJSON() ([]byte, error) {
if reflect.ValueOf(t).IsZero() {
return []byte(`null`), nil
}

if y := time.Time(t).Year(); y < 0 || y >= 10000 {
// ISO 8901 uses 4 digits for the years
return nil, errors.New("json: ISOTime year outside of range [0,9999]")
Expand Down

0 comments on commit b3f55a4

Please sign in to comment.