Skip to content

Commit

Permalink
reload quest, cast error
Browse files Browse the repository at this point in the history
  • Loading branch information
asoseil committed Mar 12, 2017
1 parent ee93089 commit cdfeb47
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
65 changes: 32 additions & 33 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand All @@ -69,72 +69,71 @@ 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:
value = strconv.FormatFloat(c.value.(float64), 'f', 2, 64)
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
Expand Down
23 changes: 16 additions & 7 deletions context.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions quest.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Default struct {
type Question struct {
Quest
err error
reload bool
choices bool
response string
value interface{}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit cdfeb47

Please sign in to comment.