Closed dilaragorum closed 1 year ago
Hi, thank you for the contribution, but Survey can already convert the entered value into an integer if it's storing the value into a variable or struct field that is of int
type. https://github.com/AlecAivazis/survey/issues/388#issuecomment-1280760228
If you have multiple questions and want to store the answers into a single answers
object, use a struct instead of map[string]interface{}
. This will also be easier to work with in the rest of your program:
var answers struct {
Age int
}
The downside of the Transformer approach that you have suggested in your PR is that it doesn't throw errors on invalid integer values and just returns 0 instead. The approach outlined above will correctly handle invalid inputs by erroring out.
When users want to use
survey.Ask
method which can return asmap[string]interface{}
, they may need an integer transformer that converts string results to an integer.For example, if the
survey.Question
is defined as shown belowand ask it like
Age field comes as
interface{} | string
typeIn order to use this field as Integer, it is needed to use
strconv.Atoi
method so I wrote down a ready-to-use Transformer.They use this transformer as
Age field comes as
interface {} | int
type