Skip to content

Commit

Permalink
Implement json.Marshaler interface to Holiday
Browse files Browse the repository at this point in the history
  • Loading branch information
osamingo committed Mar 18, 2022
1 parent 16c8547 commit f2e8c2b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
28 changes: 22 additions & 6 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net"
"strings"
"time"
)

Expand All @@ -17,6 +18,13 @@ type (
String string
Valid bool // Valid is true if String is not NULL
}

holiday struct {
Title string `json:"title"`
Date string `json:"date"`
DayOfWeek int `json:"day_of_week"`
DayOfWeekText string `json:"day_of_week_text"`
}
)

type (
Expand Down Expand Up @@ -121,6 +129,8 @@ var (
_ json.Unmarshaler = (*RemoteAddress)(nil)
_ json.Unmarshaler = (*Holiday)(nil)

_ json.Marshaler = (*Holiday)(nil)

_ net.Addr = (*RemoteAddress)(nil)
)

Expand Down Expand Up @@ -195,12 +205,7 @@ func (ra *RemoteAddress) String() string {

// UnmarshalJSON implements json.Unmarshaler interface.
func (h *Holiday) UnmarshalJSON(data []byte) error {
type v struct {
Title string `json:"title"`
Date string `json:"date"`
}

var tmp v
var tmp holiday
if err := json.Unmarshal(data, &tmp); err != nil {
return fmt.Errorf("kenall: failed to parse Holiday: %w", err)
}
Expand All @@ -214,3 +219,14 @@ func (h *Holiday) UnmarshalJSON(data []byte) error {

return nil
}

// MarshalJSON implements json.Marshaler interface.
func (h Holiday) MarshalJSON() ([]byte, error) {
// nolint: wrapcheck
return json.Marshal(&holiday{
Title: h.Title,
Date: h.Format(RFC3339DateFormat),
DayOfWeek: int(h.Weekday()),
DayOfWeekText: strings.ToLower(h.Weekday().String()),
})
}
37 changes: 37 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kenall_test

import (
"bytes"
"testing"
"time"

Expand Down Expand Up @@ -160,3 +161,39 @@ func TestHoliday_UnmarshalJSON(t *testing.T) {
})
}
}

func TestHoliday_MarshalJSON(t *testing.T) {
t.Parallel()

cases := map[string]struct {
give *kenall.Holiday
want []byte
wantError bool
}{
"Normal case": {give: &kenall.Holiday{Title: "元日", Time: time.Date(2022, 1, 1, 0, 0, 0, 0, time.FixedZone("Asia/Tokyo", int(9*time.Hour)))}, want: []byte(`{"title":"元日","date":"2022-01-01","day_of_week":6,"day_of_week_text":"saturday"}`), wantError: false},
"Empty case": {give: &kenall.Holiday{}, want: []byte(`{"title":"","date":"0001-01-01","day_of_week":1,"day_of_week_text":"monday"}`), wantError: false},
}

for name, c := range cases {
c := c

t.Run(name, func(t *testing.T) {
t.Parallel()

b, err := c.give.MarshalJSON()
if c.wantError {
if err == nil {
t.Errorf("an error should not be nil")
}

return
}
if err != nil {
t.Fatalf("an error should be nil, err = %s", err)
}
if !bytes.Equal(b, c.want) {
t.Errorf("give: %s, want: %s", b, c.want)
}
})
}
}

0 comments on commit f2e8c2b

Please sign in to comment.