2020-01-09 20:52:19 +00:00
|
|
|
package tengo
|
2019-02-23 15:39:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Variable is a user-defined variable for the script.
|
|
|
|
type Variable struct {
|
|
|
|
name string
|
2020-01-09 20:52:19 +00:00
|
|
|
value Object
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewVariable creates a Variable.
|
|
|
|
func NewVariable(name string, value interface{}) (*Variable, error) {
|
2020-01-09 20:52:19 +00:00
|
|
|
obj, err := FromInterface(value)
|
2019-02-23 15:39:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Variable{
|
|
|
|
name: name,
|
2019-04-06 20:18:25 +00:00
|
|
|
value: obj,
|
2019-02-23 15:39:44 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{} {
|
2020-01-09 20:52:19 +00:00
|
|
|
return ToInterface(v.value)
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValueType returns the name of the value type.
|
|
|
|
func (v *Variable) ValueType() string {
|
2019-04-06 20:18:25 +00:00
|
|
|
return v.value.TypeName()
|
2019-02-23 15:39:44 +00: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 {
|
2020-01-09 20:52:19 +00:00
|
|
|
c, _ := ToInt(v.value)
|
2019-02-23 15:39:44 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// Int64 returns int64 value of the variable value. It returns 0 if the value
|
|
|
|
// is not convertible to int64.
|
2019-02-23 15:39:44 +00:00
|
|
|
func (v *Variable) Int64() int64 {
|
2020-01-09 20:52:19 +00:00
|
|
|
c, _ := ToInt64(v.value)
|
2019-02-23 15:39:44 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// Float returns float64 value of the variable value. It returns 0.0 if the
|
|
|
|
// value is not convertible to float64.
|
2019-02-23 15:39:44 +00:00
|
|
|
func (v *Variable) Float() float64 {
|
2020-01-09 20:52:19 +00:00
|
|
|
c, _ := ToFloat64(v.value)
|
2019-02-23 15:39:44 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// Char returns rune value of the variable value. It returns 0 if the value is
|
|
|
|
// not convertible to rune.
|
2019-02-23 15:39:44 +00:00
|
|
|
func (v *Variable) Char() rune {
|
2020-01-09 20:52:19 +00:00
|
|
|
c, _ := ToRune(v.value)
|
2019-02-23 15:39:44 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// Bool returns bool value of the variable value. It returns 0 if the value is
|
|
|
|
// not convertible to bool.
|
2019-02-23 15:39:44 +00:00
|
|
|
func (v *Variable) Bool() bool {
|
2020-01-09 20:52:19 +00:00
|
|
|
c, _ := ToBool(v.value)
|
2019-02-23 15:39:44 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// Array returns []interface value of the variable value. It returns 0 if the
|
|
|
|
// value is not convertible to []interface.
|
2019-02-23 15:39:44 +00:00
|
|
|
func (v *Variable) Array() []interface{} {
|
2019-04-06 20:18:25 +00:00
|
|
|
switch val := v.value.(type) {
|
2020-01-09 20:52:19 +00:00
|
|
|
case *Array:
|
2019-02-23 15:39:44 +00:00
|
|
|
var arr []interface{}
|
|
|
|
for _, e := range val.Value {
|
2020-01-09 20:52:19 +00:00
|
|
|
arr = append(arr, ToInterface(e))
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// Map returns map[string]interface{} value of the variable value. It returns
|
|
|
|
// 0 if the value is not convertible to map[string]interface{}.
|
2019-02-23 15:39:44 +00:00
|
|
|
func (v *Variable) Map() map[string]interface{} {
|
2019-04-06 20:18:25 +00:00
|
|
|
switch val := v.value.(type) {
|
2020-01-09 20:52:19 +00:00
|
|
|
case *Map:
|
2019-02-23 15:39:44 +00:00
|
|
|
kv := make(map[string]interface{})
|
|
|
|
for mk, mv := range val.Value {
|
2020-01-09 20:52:19 +00:00
|
|
|
kv[mk] = ToInterface(mv)
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
return kv
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// String returns string value of the variable value. It returns 0 if the value
|
|
|
|
// is not convertible to string.
|
2019-02-23 15:39:44 +00:00
|
|
|
func (v *Variable) String() string {
|
2020-01-09 20:52:19 +00:00
|
|
|
c, _ := ToString(v.value)
|
2019-02-23 15:39:44 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// Bytes returns a byte slice of the variable value. It returns nil if the
|
|
|
|
// value is not convertible to byte slice.
|
2019-02-23 15:39:44 +00:00
|
|
|
func (v *Variable) Bytes() []byte {
|
2020-01-09 20:52:19 +00:00
|
|
|
c, _ := ToByteSlice(v.value)
|
2019-02-23 15:39:44 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00:00
|
|
|
// Error returns an error if the underlying value is error object. If not,
|
|
|
|
// this returns nil.
|
2019-02-23 15:39:44 +00:00
|
|
|
func (v *Variable) Error() error {
|
2020-01-09 20:52:19 +00:00
|
|
|
err, ok := v.value.(*Error)
|
2019-02-23 15:39:44 +00:00
|
|
|
if ok {
|
|
|
|
return errors.New(err.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-09 20:52:19 +00: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() Object {
|
2019-04-06 20:18:25 +00:00
|
|
|
return v.value
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsUndefined returns true if the underlying value is undefined.
|
|
|
|
func (v *Variable) IsUndefined() bool {
|
2020-01-09 20:52:19 +00:00
|
|
|
return v.value == UndefinedValue
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|