variable.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package tengo
  2. import (
  3. "errors"
  4. )
  5. // Variable is a user-defined variable for the script.
  6. type Variable struct {
  7. name string
  8. value Object
  9. }
  10. // NewVariable creates a Variable.
  11. func NewVariable(name string, value interface{}) (*Variable, error) {
  12. obj, err := FromInterface(value)
  13. if err != nil {
  14. return nil, err
  15. }
  16. return &Variable{
  17. name: name,
  18. value: obj,
  19. }, nil
  20. }
  21. // Name returns the name of the variable.
  22. func (v *Variable) Name() string {
  23. return v.name
  24. }
  25. // Value returns an empty interface of the variable value.
  26. func (v *Variable) Value() interface{} {
  27. return ToInterface(v.value)
  28. }
  29. // ValueType returns the name of the value type.
  30. func (v *Variable) ValueType() string {
  31. return v.value.TypeName()
  32. }
  33. // Int returns int value of the variable value.
  34. // It returns 0 if the value is not convertible to int.
  35. func (v *Variable) Int() int {
  36. c, _ := ToInt(v.value)
  37. return c
  38. }
  39. // Int64 returns int64 value of the variable value. It returns 0 if the value
  40. // is not convertible to int64.
  41. func (v *Variable) Int64() int64 {
  42. c, _ := ToInt64(v.value)
  43. return c
  44. }
  45. // Float returns float64 value of the variable value. It returns 0.0 if the
  46. // value is not convertible to float64.
  47. func (v *Variable) Float() float64 {
  48. c, _ := ToFloat64(v.value)
  49. return c
  50. }
  51. // Char returns rune value of the variable value. It returns 0 if the value is
  52. // not convertible to rune.
  53. func (v *Variable) Char() rune {
  54. c, _ := ToRune(v.value)
  55. return c
  56. }
  57. // Bool returns bool value of the variable value. It returns 0 if the value is
  58. // not convertible to bool.
  59. func (v *Variable) Bool() bool {
  60. c, _ := ToBool(v.value)
  61. return c
  62. }
  63. // Array returns []interface value of the variable value. It returns 0 if the
  64. // value is not convertible to []interface.
  65. func (v *Variable) Array() []interface{} {
  66. switch val := v.value.(type) {
  67. case *Array:
  68. var arr []interface{}
  69. for _, e := range val.Value {
  70. arr = append(arr, ToInterface(e))
  71. }
  72. return arr
  73. }
  74. return nil
  75. }
  76. // Map returns map[string]interface{} value of the variable value. It returns
  77. // 0 if the value is not convertible to map[string]interface{}.
  78. func (v *Variable) Map() map[string]interface{} {
  79. switch val := v.value.(type) {
  80. case *Map:
  81. kv := make(map[string]interface{})
  82. for mk, mv := range val.Value {
  83. kv[mk] = ToInterface(mv)
  84. }
  85. return kv
  86. }
  87. return nil
  88. }
  89. // String returns string value of the variable value. It returns 0 if the value
  90. // is not convertible to string.
  91. func (v *Variable) String() string {
  92. c, _ := ToString(v.value)
  93. return c
  94. }
  95. // Bytes returns a byte slice of the variable value. It returns nil if the
  96. // value is not convertible to byte slice.
  97. func (v *Variable) Bytes() []byte {
  98. c, _ := ToByteSlice(v.value)
  99. return c
  100. }
  101. // Error returns an error if the underlying value is error object. If not,
  102. // this returns nil.
  103. func (v *Variable) Error() error {
  104. err, ok := v.value.(*Error)
  105. if ok {
  106. return errors.New(err.String())
  107. }
  108. return nil
  109. }
  110. // Object returns an underlying Object of the variable value. Note that
  111. // returned Object is a copy of an actual Object used in the script.
  112. func (v *Variable) Object() Object {
  113. return v.value
  114. }
  115. // IsUndefined returns true if the underlying value is undefined.
  116. func (v *Variable) IsUndefined() bool {
  117. return v.value == UndefinedValue
  118. }