From 2c14e3804ccb81910e3268647fd53b575013d063 Mon Sep 17 00:00:00 2001 From: Josh Glendenning Date: Mon, 16 Oct 2023 23:07:13 -0500 Subject: [PATCH] Simplify custom setter examples --- cli.go | 8 +++----- cli_test.go | 12 +++++------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/cli.go b/cli.go index 090199b..ee752fd 100644 --- a/cli.go +++ b/cli.go @@ -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 @@ -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 diff --git a/cli_test.go b/cli_test.go index c4e15b2..b0011e9 100644 --- a/cli_test.go +++ b/cli_test.go @@ -268,16 +268,14 @@ 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 } @@ -285,10 +283,10 @@ 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 }