From cdfeb479c68e51d2a5b50123ffb9daa14ce98e7a Mon Sep 17 00:00:00 2001 From: alessio Date: Sun, 12 Mar 2017 23:17:47 +0100 Subject: [PATCH] reload quest, cast error --- args.go | 65 +++++++++++++++++++++++++++--------------------------- context.go | 23 +++++++++++++------ quest.go | 5 +++++ 3 files changed, 53 insertions(+), 40 deletions(-) diff --git a/args.go b/args.go index dfd05be..7be43c4 100644 --- a/args.go +++ b/args.go @@ -8,11 +8,11 @@ import ( type ( Cast interface { - Int() int64 - Bool() bool - String() string - Float() float64 - Time() time.Duration + Int() (int64, error) + Bool() (bool, error) + String() (string, error) + Float() (float64, error) + Time() (time.Duration, error) Raw() interface{} } cast struct { @@ -37,30 +37,30 @@ type ( ) // Cast the answer as an int -func (c *cast) Int() (value int64) { +func (c *cast) Int() (value int64, err error) { if c.value != nil { switch c.value.(type) { case string: - value, _ = strconv.ParseInt(c.value.(string), 10, 64) + value, err = strconv.ParseInt(c.value.(string), 10, 64) case float64: value = int64(c.value.(float64)) case int: value = int64(c.value.(int)) default: - c.err = errors.New("conversion as int failed") + err = errors.New("conversion as int failed") } } else { - value, _ = strconv.ParseInt(c.answer, 10, 64) + value, err = strconv.ParseInt(c.answer, 10, 64) } - return value + return value, err } // Cast the answer as a float -func (c *cast) Float() (value float64) { +func (c *cast) Float() (value float64, err error) { if c.value != nil { switch c.value.(type) { case string: - value, _ = strconv.ParseFloat(c.value.(string), 64) + value, err = strconv.ParseFloat(c.value.(string), 64) case float64: value = c.value.(float64) case int: @@ -69,60 +69,59 @@ func (c *cast) Float() (value float64) { c.err = errors.New("conversion as uint failed") } } else { - value, _ = strconv.ParseFloat(c.answer, 64) + value, err = strconv.ParseFloat(c.answer, 64) } - return + return value, err } // Cast the answer as a time duration -func (c *cast) Time() time.Duration { +func (c *cast) Time() (t time.Duration, err error) { if c.value != nil { var cast int64 switch c.value.(type) { case string: - cast, _ = strconv.ParseInt(c.value.(string), 10, 64) + cast, err = strconv.ParseInt(c.value.(string), 10, 64) case float64: cast = int64(c.value.(float64)) case int: cast = int64(c.value.(int)) default: - c.err = errors.New("conversion as time duration failed") + err = errors.New("conversion as time duration failed") } - return time.Duration(int64(cast)) + return time.Duration(int64(cast)), err } if value, err := strconv.ParseUint(c.answer, 10, 64); err == nil { - return time.Duration(value) + return time.Duration(value), err } - return time.Duration(0) + return time.Duration(0), err } // Cast the answer as a bool -func (c *cast) Bool() (value bool) { +func (c *cast) Bool() (value bool, err error) { if c.value != nil { - //fmt.Println(c.value) switch c.value.(type) { case bool: value = c.value.(bool) default: - c.err = errors.New("conversion as bool failed") + err = errors.New("conversion as bool failed") } - return + return value, err } if c.answer == "y" || c.answer == "yes" { - return true + return true, nil } else if c.answer == "n" || c.answer == "no" { - return false + return false, nil } - value, _ = strconv.ParseBool(c.answer) - return + value, err = strconv.ParseBool(c.answer) + return value, err } // Cast the answer as a string -func (c *cast) String() (value string) { +func (c *cast) String() (value string, err error) { if c.value != nil { switch c.value.(type) { case string: - value, _ = c.value.(string) + value = c.value.(string) case int: value = strconv.Itoa(c.value.(int)) case float64: @@ -130,11 +129,11 @@ func (c *cast) String() (value string) { case bool: value = strconv.FormatBool(c.value.(bool)) default: - c.err = errors.New("conversion as string failed") + err = errors.New("conversion as string failed") } - return + return value, err } - return c.answer + return c.answer, err } // Raw return the answer as an interface diff --git a/context.go b/context.go index f759337..bfddfcb 100644 --- a/context.go +++ b/context.go @@ -1,16 +1,17 @@ package interact import ( - "io" "errors" + "io" ) type ( Context interface { Skip() + Reload() SetPrfx(io.Writer, interface{}) SetDef(interface{}, interface{}, bool) - SetErr(string) + SetErr(interface{}) Ans() Cast Def() Cast Err() error @@ -50,11 +51,19 @@ func (c *context) Skip() { c.i.skip = true } -func (c *context) Err(){ - if c.q != nil{ - return errors.New(c.q.err.(string)) +func (c *context) Reload() { + if c.q != nil { + c.q.reload = true } - return errors.New(c.i.Err.(string)) +} + +func (c *context) Err() error { + if c.q.Err != nil { + return errors.New(c.q.Err.(string)) + } else if c.i.Err != nil { + return errors.New(c.i.Err.(string)) + } + return nil } func (c *context) Def() Cast { @@ -117,7 +126,7 @@ func (c *context) SetDef(v interface{}, t interface{}, p bool) { return } -func (c *context) SetErr(e string) { +func (c *context) SetErr(e interface{}) { if c.q != nil { c.q.Err = e return diff --git a/quest.go b/quest.go index f683c54..5178fb7 100644 --- a/quest.go +++ b/quest.go @@ -30,6 +30,7 @@ type Default struct { type Question struct { Quest err error + reload bool choices bool response string value interface{} @@ -104,6 +105,10 @@ func (q *Question) ask() (err error) { return q.ask() } } + if q.reload { + q.reload = false + return q.ask() + } if err := context.method(q.After); err != nil { return err }