Skip to content

Commit

Permalink
Merge pull request #15 from winebarrel/fix_NestIF
Browse files Browse the repository at this point in the history
Fix Next() retval
  • Loading branch information
winebarrel authored Jun 17, 2023
2 parents 254dccf + 54b1e01 commit 96060b6
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func main() {
yes, _ := client.IsHoliday(date)
fmt.Println(yes) //=> true

h, _ = client.Next(date)
fmt.Println(h) //=> 2023-07-17 海の日

holidays, _ := client.NextN(date, 3)
fmt.Println(holidays) //=> [2023-07-17 海の日 2023-08-11 山の日 2023-09-18 敬老の日]

Expand Down
20 changes: 13 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ const (
JaJapaneseHolidayCalendar = "ja.japanese#holiday@group.v.calendar.google.com"
)

var (
DefaultMaxResults = 10
)

type Client struct {
APIKey string
CalendarID string
Expand Down Expand Up @@ -127,15 +123,25 @@ func (client *Client) NextN(ctx context.Context, t time.Time, n int) ([]*Holiday
return holidays, nil
}

func (client *Client) Next(ctx context.Context, t time.Time) ([]*Holiday, error) {
return client.NextN(ctx, t, DefaultMaxResults)
func (client *Client) Next(ctx context.Context, t time.Time) (*Holiday, error) {
holidays, err := client.NextN(ctx, t, 1)

if err != nil {
return nil, err
}

if len(holidays) == 0 {
return nil, nil
}

return holidays[0], nil
}

func (c *ClientWithoutContext) NextN(t time.Time, n int) ([]*Holiday, error) {
return c.client.NextN(context.Background(), t, n)
}

func (c *ClientWithoutContext) Next(t time.Time) ([]*Holiday, error) {
func (c *ClientWithoutContext) Next(t time.Time) (*Holiday, error) {
return c.client.Next(context.Background(), t)
}

Expand Down
34 changes: 34 additions & 0 deletions client_next_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,37 @@ func TestClientNextN_UTC(_t *testing.T) {
assert.Equal(expected, holidays)
}
}

func TestClientNext(_t *testing.T) {
assert := assert.New(_t)
client := jhol.NewClient(TestGCalAPIKey)

tests := []struct {
date string
expectedDate string
expectedName string
}{
{"2022-01-01 00:00:00", "2022-01-01", "元日"},
{"2022-01-01 23:59:59", "2022-01-01", "元日"},
{"2022-01-02 00:00:00", "2022-01-10", "成人の日"},
{"2022-05-03 00:00:00", "2022-05-03", "憲法記念日"},
{"2022-05-03 23:59:59", "2022-05-03", "憲法記念日"},
{"2022-05-04 00:00:00", "2022-05-04", "みどりの日"},
{"2022-11-23 00:00:00", "2022-11-23", "勤労感謝の日"},
{"2022-11-23 23:59:59", "2022-11-23", "勤労感謝の日"},
{"2022-11-24 00:00:00", "2023-01-01", "元日"},
}

for _, t := range tests {
d, _ := time.ParseInLocation("2006-01-02 15:04:05", t.date, JST)
h, err := client.Next(context.Background(), d)

if !assert.NoErrorf(err, "%+v", t) {
continue
}

expectedDate, _ := time.ParseInLocation("2006-01-02", t.expectedDate, JST)
expected := &jhol.Holiday{Date: expectedDate, Name: t.expectedName}
assert.Equal(expected, h)
}
}
35 changes: 35 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"os"
"time"

"github.com/winebarrel/jhol"
)

func parseDate(s string) time.Time {
d, _ := time.ParseInLocation("2006-01-02", s, time.Local)
return d
}

func main() {
apiKey := os.Getenv("GOOGLE_API_KEY")
client := jhol.NewClient(apiKey).WithoutContext()

date := parseDate("2023-07-17")
h, _ := client.Get(date)
fmt.Println(h) //=> 2023-07-17 海の日

yes, _ := client.IsHoliday(date)
fmt.Println(yes) //=> true

h, _ = client.Next(date)
fmt.Println(h) //=> 2023-07-17 海の日

holidays, _ := client.NextN(date, 3)
fmt.Println(holidays) //=> [2023-07-17 海の日 2023-08-11 山の日 2023-09-18 敬老の日]

holidays, _ = client.Between(date, parseDate("2023-08-11"))
fmt.Println(holidays) //=> [2023-07-17 海の日 2023-08-11 山の日]
}

0 comments on commit 96060b6

Please sign in to comment.