xgo/script/variable.go

150 lines
3.4 KiB
Go
Raw Normal View History

2019-01-09 23:39:40 +03:00
package script
import (
2019-01-18 13:33:23 +03:00
"errors"
2019-01-09 23:39:40 +03:00
"github.com/d5/tengo/objects"
)
// Variable is a user-defined variable for the script.
type Variable struct {
name string
value objects.Object
2019-01-09 23:39:40 +03:00
}
2019-01-15 21:15:48 +03:00
// NewVariable creates a Variable.
func NewVariable(name string, value interface{}) (*Variable, error) {
obj, err := objects.FromInterface(value)
if err != nil {
return nil, err
}
return &Variable{
name: name,
value: obj,
}, nil
}
2019-01-09 23:39:40 +03:00
// Name returns the name of the variable.
func (v *Variable) Name() string {
return v.name
}
// Value returns an empty interface of the variable value.
func (v *Variable) Value() interface{} {
return objects.ToInterface(v.value)
2019-01-09 23:39:40 +03:00
}
// ValueType returns the name of the value type.
func (v *Variable) ValueType() string {
return v.value.TypeName()
2019-01-09 23:39:40 +03:00
}
// Int returns int value of the variable value.
// It returns 0 if the value is not convertible to int.
func (v *Variable) Int() int {
c, _ := objects.ToInt(v.value)
2019-01-18 12:43:46 +03:00
return c
2019-01-09 23:39:40 +03:00
}
// Int64 returns int64 value of the variable value.
// It returns 0 if the value is not convertible to int64.
func (v *Variable) Int64() int64 {
c, _ := objects.ToInt64(v.value)
2019-01-09 23:39:40 +03:00
2019-01-18 12:43:46 +03:00
return c
2019-01-09 23:39:40 +03:00
}
// Float returns float64 value of the variable value.
// It returns 0.0 if the value is not convertible to float64.
func (v *Variable) Float() float64 {
c, _ := objects.ToFloat64(v.value)
2019-01-09 23:39:40 +03:00
2019-01-18 12:43:46 +03:00
return c
2019-01-09 23:39:40 +03:00
}
// Char returns rune value of the variable value.
// It returns 0 if the value is not convertible to rune.
func (v *Variable) Char() rune {
c, _ := objects.ToRune(v.value)
2019-01-09 23:39:40 +03:00
2019-01-18 12:43:46 +03:00
return c
2019-01-09 23:39:40 +03:00
}
// Bool returns bool value of the variable value.
// It returns 0 if the value is not convertible to bool.
func (v *Variable) Bool() bool {
c, _ := objects.ToBool(v.value)
2019-01-09 23:39:40 +03:00
2019-01-18 12:43:46 +03:00
return c
2019-01-09 23:39:40 +03:00
}
// Array returns []interface value of the variable value.
// It returns 0 if the value is not convertible to []interface.
func (v *Variable) Array() []interface{} {
switch val := v.value.(type) {
2019-01-09 23:39:40 +03:00
case *objects.Array:
var arr []interface{}
for _, e := range val.Value {
arr = append(arr, objects.ToInterface(e))
2019-01-09 23:39:40 +03:00
}
return arr
}
return nil
}
// Map returns map[string]interface{} value of the variable value.
// It returns 0 if the value is not convertible to map[string]interface{}.
func (v *Variable) Map() map[string]interface{} {
switch val := v.value.(type) {
2019-01-09 23:39:40 +03:00
case *objects.Map:
kv := make(map[string]interface{})
for mk, mv := range val.Value {
kv[mk] = objects.ToInterface(mv)
2019-01-09 23:39:40 +03:00
}
return kv
}
return nil
}
// String returns string value of the variable value.
// It returns 0 if the value is not convertible to string.
func (v *Variable) String() string {
c, _ := objects.ToString(v.value)
2019-01-18 12:43:46 +03:00
return c
}
// Bytes returns a byte slice of the variable value.
// It returns nil if the value is not convertible to byte slice.
func (v *Variable) Bytes() []byte {
c, _ := objects.ToByteSlice(v.value)
2019-01-18 12:43:46 +03:00
return c
2019-01-09 23:39:40 +03:00
}
2019-01-18 13:33:23 +03:00
// Error returns an error if the underlying value is error object.
// If not, this returns nil.
func (v *Variable) Error() error {
err, ok := v.value.(*objects.Error)
2019-01-18 13:33:23 +03:00
if ok {
return errors.New(err.String())
}
return nil
}
2019-01-09 23:39:40 +03:00
// Object returns an underlying Object of the variable value.
// Note that returned Object is a copy of an actual Object used in the script.
func (v *Variable) Object() objects.Object {
return v.value
2019-01-09 23:39:40 +03:00
}
2019-01-16 04:17:08 +03:00
// IsUndefined returns true if the underlying value is undefined.
func (v *Variable) IsUndefined() bool {
return v.value == objects.UndefinedValue
2019-01-16 04:17:08 +03:00
}