Skip to content

Commit

Permalink
Simplify custom setter examples
Browse files Browse the repository at this point in the history
  • Loading branch information
isobit committed Oct 17, 2023
1 parent f6dbe90 commit 2c14e38
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
8 changes: 3 additions & 5 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ type CLI struct {
// Here is an example which uses a custom layout for parsing any time.Time
// fields:
//
// type CustomTimeSetter struct {
// value *time.Time
// }
// func (ts *CustomTimeSetter) Set(s string) error {
// type customTime time.Time
// func (t *customTime) Set(s string) error {
// parsed, err := time.Parse("2006-01-02 15:04", s)
// if err != nil {
// return err
Expand All @@ -42,7 +40,7 @@ type CLI struct {
// cli.Setter = func(i interface{}) cli.Setter {
// switch v := i.(type) {
// case *time.Time:
// return &CustomTimeSetter{v}
// return (*customTime)(v)
// default:
// // return nil to fall back on default behavior
// return nil
Expand Down
12 changes: 5 additions & 7 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,27 +268,25 @@ func TestCLIEnvLookupError(t *testing.T) {
assert.Error(t, r.Err)
}

type testTimeSetter struct {
t *time.Time
}
type customTime time.Time

func (ts *testTimeSetter) Set(s string) error {
func (t *customTime) Set(s string) error {
v, err := time.Parse(time.Kitchen, s)
if err != nil {
return err
}
*ts.t = v
*t = (customTime)(v)
return nil
}

func TestCLISetter(t *testing.T) {
b := &strings.Builder{}
cli := CLI{
ErrWriter: b,
Setter: func(i interface{}) setter {
Setter: func(i interface{}) Setter {
switch v := i.(type) {
case *time.Time:
return &testTimeSetter{v}
return (*customTime)(v)
default:
return nil
}
Expand Down

0 comments on commit 2c14e38

Please sign in to comment.