140 lines
2.1 KiB
Go
140 lines
2.1 KiB
Go
package objects
|
|
|
|
import (
|
|
"strconv"
|
|
)
|
|
|
|
// ToString will try to convert object o to string value.
|
|
func ToString(o Object) (v string, ok bool) {
|
|
if _, isUndefined := o.(*Undefined); isUndefined {
|
|
//ok = false
|
|
return
|
|
}
|
|
|
|
ok = true
|
|
|
|
if str, isStr := o.(*String); isStr {
|
|
v = str.Value
|
|
} else {
|
|
v = o.String()
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// ToInt will try to convert object o to int value.
|
|
func ToInt(o Object) (v int, ok bool) {
|
|
switch o := o.(type) {
|
|
case *Int:
|
|
v = int(o.Value)
|
|
ok = true
|
|
case *Float:
|
|
v = int(o.Value)
|
|
ok = true
|
|
case *Char:
|
|
v = int(o.Value)
|
|
ok = true
|
|
case *Bool:
|
|
if o.Value {
|
|
v = 1
|
|
}
|
|
ok = true
|
|
case *String:
|
|
c, err := strconv.ParseInt(o.Value, 10, 64)
|
|
if err == nil {
|
|
v = int(c)
|
|
ok = true
|
|
}
|
|
}
|
|
|
|
//ok = false
|
|
return
|
|
}
|
|
|
|
// ToInt64 will try to convert object o to int64 value.
|
|
func ToInt64(o Object) (v int64, ok bool) {
|
|
switch o := o.(type) {
|
|
case *Int:
|
|
v = o.Value
|
|
ok = true
|
|
case *Float:
|
|
v = int64(o.Value)
|
|
ok = true
|
|
case *Char:
|
|
v = int64(o.Value)
|
|
ok = true
|
|
case *Bool:
|
|
if o.Value {
|
|
v = 1
|
|
}
|
|
ok = true
|
|
case *String:
|
|
c, err := strconv.ParseInt(o.Value, 10, 64)
|
|
if err == nil {
|
|
v = c
|
|
ok = true
|
|
}
|
|
}
|
|
|
|
//ok = false
|
|
return
|
|
}
|
|
|
|
// ToFloat64 will try to convert object o to float64 value.
|
|
func ToFloat64(o Object) (v float64, ok bool) {
|
|
switch o := o.(type) {
|
|
case *Int:
|
|
v = float64(o.Value)
|
|
ok = true
|
|
case *Float:
|
|
v = o.Value
|
|
ok = true
|
|
case *String:
|
|
c, err := strconv.ParseFloat(o.Value, 64)
|
|
if err == nil {
|
|
v = c
|
|
ok = true
|
|
}
|
|
}
|
|
|
|
//ok = false
|
|
return
|
|
}
|
|
|
|
// ToBool will try to convert object o to bool value.
|
|
func ToBool(o Object) (v bool, ok bool) {
|
|
ok = true
|
|
v = !o.IsFalsy()
|
|
|
|
return
|
|
}
|
|
|
|
// ToRune will try to convert object o to rune value.
|
|
func ToRune(o Object) (v rune, ok bool) {
|
|
switch o := o.(type) {
|
|
case *Int:
|
|
v = rune(o.Value)
|
|
ok = true
|
|
case *Char:
|
|
v = rune(o.Value)
|
|
ok = true
|
|
}
|
|
|
|
//ok = false
|
|
return
|
|
}
|
|
|
|
// ToByteSlice will try to convert object o to []byte value.
|
|
func ToByteSlice(o Object) (v []byte, ok bool) {
|
|
switch o := o.(type) {
|
|
case *Bytes:
|
|
v = o.Value
|
|
ok = true
|
|
case *String:
|
|
v = []byte(o.Value)
|
|
ok = true
|
|
}
|
|
|
|
//ok = false
|
|
return
|
|
}
|