Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Revert "simplified flags package"
Browse files Browse the repository at this point in the history
This reverts commit a2c3ecb.

This broke the String() method of empty lists.

Also add a test to catch this in te future.
  • Loading branch information
jorinvo committed May 8, 2017
1 parent 02c725d commit 88ed0b2
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 39 deletions.
21 changes: 12 additions & 9 deletions flags/intlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
type intlist struct {
min int
max int
list *[]int
list []int
}

func (l *intlist) String() string {
s := make([]string, len(*l.list))
for i := range *l.list {
s[i] = strconv.Itoa((*l.list)[i])
s := make([]string, len(l.list))
for i := range l.list {
s[i] = strconv.Itoa(l.list[i])
}
return strings.Join(s, ",")
}
Expand All @@ -34,14 +34,17 @@ func (l *intlist) Set(s string) error {
if x > l.max {
return fmt.Errorf("%d is bigger than maximum %d", x, l.max)
}
*l.list = append(*l.list, x)
l.list = append(l.list, x)
}
return nil
}

// Intlist defines a flag for a comma-separated list of integers.
func Intlist(name, usage string, min, max int) *[]int {
l := &[]int{}
flag.Var(&intlist{list: l, min: min, max: max}, name, usage)
return l
// Call the returned function after flag.Parse to get the value.
func Intlist(name, usage string, min, max int) func() []int {
l := &intlist{min: min, max: max}
flag.Var(l, name, usage)
return func() []int {
return l.list
}
}
7 changes: 5 additions & 2 deletions flags/intlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestIntlist(t *testing.T) {
}

for i, tt := range tests {
l := intlist{min: 0, max: 10, list: &[]int{}}
l := intlist{min: 0, max: 10}
if err := l.Set(tt.text); err != nil {
if !tt.invalid {
t.Errorf("parsing %s failed unexpectedly: %v", tt.text, err)
Expand All @@ -31,7 +31,7 @@ func TestIntlist(t *testing.T) {
t.Errorf("parsing %s should have failed", tt.text)
continue
}
if !equal(*l.list, tt.parsed) {
if !equal(l.list, tt.parsed) {
t.Errorf(`
%d.
Input: %s
Expand All @@ -40,6 +40,9 @@ Got %v`, i, tt.text, tt.parsed, l.list)
}
}

if (&intlist{}).String() != "" {
t.Errorf("Non empty String() output: %s", (&intlist{}).String())
}
}

func equal(a []int, b []int) bool {
Expand Down
23 changes: 14 additions & 9 deletions flags/monthlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"time"
)

type monthlist []time.Month
type monthlist struct {
list []time.Month
}

func (l *monthlist) String() string {
s := make([]string, len(*l))
for i := range *l {
s[i] = strconv.Itoa(int((*l)[i]))
s := make([]string, len(l.list))
for i := range l.list {
s[i] = strconv.Itoa(int(l.list[i]))
}
return strings.Join(s, ",")
}
Expand All @@ -29,15 +31,18 @@ func (l *monthlist) Set(s string) error {
if x < 1 || x > 12 {
return fmt.Errorf("invalid month: %d", x)
}
*l = monthlist(append(*l, x))
l.list = append(l.list, x)
}
return nil
}

// Monthlist defines a flag for a comma-separated list of months.
// Valid values are between 1 and 12.
func Monthlist(name, usage string) *[]time.Month {
l := &[]time.Month{}
flag.Var((*monthlist)(l), name, usage)
return l
// Call the returned function after flag.Parse to get the value.
func Monthlist(name, usage string) func() []time.Month {
l := &monthlist{}
flag.Var(l, name, usage)
return func() []time.Month {
return l.list
}
}
7 changes: 5 additions & 2 deletions flags/monthlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ func TestMonthlist(t *testing.T) {
t.Errorf("parsing %s should have failed", tt.text)
continue
}
if !equalMonth(l, tt.parsed) {
if !equalMonth(l.list, tt.parsed) {
t.Errorf(`
%d.
Input: %s
Expected: %v
Got %v`, i, tt.text, tt.parsed, l)
Got %v`, i, tt.text, tt.parsed, l.list)
}
}

if (&monthlist{}).String() != "" {
t.Errorf("Non empty String() output: %s", (&monthlist{}).String())
}
}

func equalMonth(a []time.Month, b []time.Month) bool {
Expand Down
23 changes: 14 additions & 9 deletions flags/weekdaylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ var strDays = map[string]time.Weekday{
"su": time.Sunday,
}

type weekdaylist []time.Weekday
type weekdaylist struct {
list []time.Weekday
}

func (l *weekdaylist) String() string {
s := make([]string, len(*l))
for i := range *l {
s[i] = dayToString((*l)[i])
s := make([]string, len(l.list))
for i := range l.list {
s[i] = dayToString(l.list[i])
}
return strings.Join(s, ",")
}
Expand All @@ -43,15 +45,18 @@ func (l *weekdaylist) Set(s string) error {
if !ok {
return fmt.Errorf("invalid day at index %d: %s", i, d)
}
*l = weekdaylist(append(*l, x))
l.list = append(l.list, x)
}
return nil
}

// Weekdaylist defines a flag for a comma-separated list of week days.
// Valid values are mo, tu, we, th, fr, sa, su.
func Weekdaylist(name, usage string) *[]time.Weekday {
l := &[]time.Weekday{}
flag.Var((*weekdaylist)(l), name, usage)
return l
// Call the returned function after flag.Parse to get the value.
func Weekdaylist(name, usage string) func() []time.Weekday {
l := &weekdaylist{}
flag.Var(l, name, usage)
return func() []time.Weekday {
return l.list
}
}
7 changes: 5 additions & 2 deletions flags/weekdaylist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ func TestWeekdaylist(t *testing.T) {
t.Errorf("parsing %s should have failed", tt.text)
continue
}
if !equalWeekday(l, tt.parsed) {
if !equalWeekday(l.list, tt.parsed) {
t.Errorf(`
%d.
Input: %s
Expected: %v
Got %v`, i, tt.text, tt.parsed, l)
Got %v`, i, tt.text, tt.parsed, l.list)
}
}

if (&weekdaylist{}).String() != "" {
t.Errorf("Non empty String() output: %s", (&weekdaylist{}).String())
}
}

func equalWeekday(a []time.Weekday, b []time.Weekday) bool {
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func main() {
now := time.Now()

next := match.Next(now, match.Condition{
Month: *month,
Weekday: *weekday,
Day: *day,
Hour: *hour,
Minute: *minute,
Second: *second,
Month: month(),
Weekday: weekday(),
Day: day(),
Hour: hour(),
Minute: minute(),
Second: second(),
})

// No conditions specified
Expand Down

0 comments on commit 88ed0b2

Please sign in to comment.