mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-13 18:07:51 +08:00
parent
5c39292700
commit
d1421bbe60
|
@ -1,6 +1,7 @@
|
|||
package vals
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
@ -105,16 +106,25 @@ func elvToFloat(arg interface{}) (float64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
var errMustBeInteger = errors.New("must be integer")
|
||||
|
||||
func elvToInt(arg interface{}) (int, error) {
|
||||
arg, ok := arg.(string)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("must be string")
|
||||
switch arg := arg.(type) {
|
||||
case float64:
|
||||
i := int(arg)
|
||||
if float64(i) != arg {
|
||||
return 0, errMustBeInteger
|
||||
}
|
||||
return i, nil
|
||||
case string:
|
||||
num, err := strconv.ParseInt(arg, 0, 0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(num), nil
|
||||
default:
|
||||
return 0, errMustBeInteger
|
||||
}
|
||||
num, err := strconv.ParseInt(arg.(string), 0, 0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(num), nil
|
||||
}
|
||||
|
||||
func elvToRune(arg interface{}) (rune, error) {
|
||||
|
|
|
@ -23,6 +23,7 @@ func scanToGo2(src interface{}, dstInit interface{}) (interface{}, error) {
|
|||
var scanToGoTests = tt.Table{
|
||||
Args("12", 0).Rets(12),
|
||||
Args("0x12", 0).Rets(0x12),
|
||||
Args(12.0, 0).Rets(12),
|
||||
Args("23", 0.0).Rets(23.0),
|
||||
Args("0x23", 0.0).Rets(float64(0x23)),
|
||||
Args("x", ' ').Rets('x'),
|
||||
|
@ -30,6 +31,7 @@ var scanToGoTests = tt.Table{
|
|||
Args(someType{"foo"}, someType{}).Rets(someType{"foo"}),
|
||||
Args(nil, nil).Rets(nil),
|
||||
|
||||
Args(0.5, 0).Rets(0, errMustBeInteger),
|
||||
Args("x", someType{}).Rets(any, anyError),
|
||||
Args(someType{}, 0).Rets(any, anyError),
|
||||
Args("x", 0).Rets(any, anyError),
|
||||
|
|
Loading…
Reference in New Issue
Block a user