-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
146 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
package patreon | ||
|
||
import "time" | ||
|
||
// Goal represents a Patreon's goal. | ||
type Goal struct { | ||
Type string `json:"type"` | ||
Id string `json:"id"` | ||
Attributes struct { | ||
Amount int `json:"amount"` | ||
AmountCents int `json:"amount_cents"` | ||
CompletedPercentage int `json:"completed_percentage"` | ||
CreatedAt time.Time `json:"created_at"` | ||
ReachedAt time.Time `json:"reached_at"` | ||
Title string `json:"title"` | ||
Description string `json:"description"` | ||
Amount int `json:"amount"` | ||
AmountCents int `json:"amount_cents"` | ||
CompletedPercentage int `json:"completed_percentage"` | ||
CreatedAt NullTime `json:"created_at"` | ||
ReachedAt NullTime `json:"reached_at"` | ||
Title string `json:"title"` | ||
Description string `json:"description"` | ||
} `json:"attributes"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package patreon | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// NullTime represents a time.Time that may be JSON "null". | ||
// golang prior 1.8 doesn't support this scenario (fails with error: parsing time "null" as ""2006-01-02T15:04:05Z07:00"": cannot parse "null" as """) | ||
type NullTime struct { | ||
time.Time | ||
Valid bool | ||
} | ||
|
||
// MarshalJSON implements json.Marshaler, it will encode null if this time is null. | ||
func (t *NullTime) MarshalJSON() ([]byte, error) { | ||
if !t.Valid { | ||
return []byte("null"), nil | ||
} | ||
return t.Time.MarshalJSON() | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaler with JSON "null" support | ||
func (t *NullTime) UnmarshalJSON(data []byte) error { | ||
s := string(data) | ||
if strings.EqualFold(s, "null") { | ||
t.Valid = false | ||
return nil | ||
} | ||
|
||
err := json.Unmarshal(data, &t.Time) | ||
t.Valid = err == nil | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package patreon | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNullTime_parseValidTime(t *testing.T) { | ||
s := &struct { | ||
Time NullTime `json:"time"` | ||
}{} | ||
|
||
err := json.Unmarshal([]byte(`{ "time": "2017-06-20T23:21:34.514822+00:00" }`), s) | ||
require.NoError(t, err) | ||
require.True(t, s.Time.Valid) | ||
require.Equal(t, time.Date(2017, 6, 20, 23, 21, 34, 514822000, time.UTC).Unix(), s.Time.Time.Unix()) | ||
} | ||
|
||
func TestNullTime_parseInvalidTime(t *testing.T) { | ||
s := &struct { | ||
Time NullTime `json:"time"` | ||
}{} | ||
|
||
err := json.Unmarshal([]byte(`{ "time": null }`), s) | ||
require.NoError(t, err) | ||
require.False(t, s.Time.Valid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
package patreon | ||
|
||
import "time" | ||
|
||
// Reward represents a Patreon's reward. | ||
type Reward struct { | ||
Type string `json:"type"` | ||
Id string `json:"id"` | ||
Attributes struct { | ||
Amount int `json:"amount"` | ||
AmountCents int `json:"amount_cents"` | ||
CreatedAt time.Time `json:"created_at"` | ||
DeletedAt time.Time `json:"deleted_at"` | ||
EditedAt time.Time `json:"edited_at"` | ||
Description string `json:"description"` | ||
ImageURL string `json:"image_url"` | ||
PatronCount int `json:"patron_count"` | ||
PostCount int `json:"post_count"` | ||
Published bool `json:"published"` | ||
PublishedAt time.Time `json:"published_at"` | ||
RequiresShipping bool `json:"requires_shipping"` | ||
Title string `json:"title"` | ||
UnpublishedAt time.Time `json:"unpublished_at"` | ||
URL string `json:"url"` | ||
Amount int `json:"amount"` | ||
AmountCents int `json:"amount_cents"` | ||
CreatedAt NullTime `json:"created_at"` | ||
DeletedAt NullTime `json:"deleted_at"` | ||
EditedAt NullTime `json:"edited_at"` | ||
Description string `json:"description"` | ||
ImageURL string `json:"image_url"` | ||
PatronCount int `json:"patron_count"` | ||
PostCount int `json:"post_count"` | ||
Published bool `json:"published"` | ||
PublishedAt NullTime `json:"published_at"` | ||
RequiresShipping bool `json:"requires_shipping"` | ||
Title string `json:"title"` | ||
UnpublishedAt NullTime `json:"unpublished_at"` | ||
URL string `json:"url"` | ||
} `json:"attributes"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters