object.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package muesli
  2. type ObjectGetter func() Value
  3. type ObjectSetter func(Value) Value
  4. type ObjectMethod func(...Value) []Value
  5. // Object is an interface for a Value that has fields
  6. // which can be get, set or invoked. The field names must be strings.
  7. // It is allowed, but not mandatory that names for Get, Set and Invoke
  8. // are stored separately.
  9. type Object interface {
  10. Value
  11. Get(key string) Value
  12. Set(key string, to Value) Value
  13. Invoke(key string, args... Value) []Value
  14. }
  15. // BasicObject is strut that implements Object.
  16. // An object may have a parent it inherit's it's fields and methods from.
  17. // Include it in your own struct to endow it with getters and setters,
  18. // and methods more easily.
  19. type BasicObject struct {
  20. RedispatchCallable
  21. Parent * BasicObject
  22. Object interface{}
  23. Getters map[string] ObjectGetter
  24. Setters map[string] ObjectSetter
  25. Methods map[string] ObjectMethod
  26. }
  27. const ObjectType = TypeValue("Object")
  28. func (v BasicObject) Type() TypeValue { return ObjectType }
  29. func (val BasicObject) String() string {
  30. res := "{"
  31. sep := ""
  32. for k, v := range val.Getters {
  33. res = res + sep + k + "=>" + v().String()
  34. sep = ", "
  35. }
  36. res += "}"
  37. return res
  38. }
  39. // Adds a getter to an object
  40. func (o *BasicObject) Getter(name string, getter ObjectGetter) *BasicObject {
  41. o.Getters[name] = getter
  42. return o
  43. }
  44. // Adds a setter to an object
  45. func (o *BasicObject) Setter(name string, setter ObjectSetter) *BasicObject {
  46. o.Setters[name] = setter
  47. return o
  48. }
  49. // Adds a method to an object
  50. func (o *BasicObject) Method(name string, method ObjectMethod) *BasicObject {
  51. o.Methods[name] = method
  52. return o
  53. }
  54. func NewBasicObject(object interface {}, parent *BasicObject) *BasicObject {
  55. mv := &BasicObject{Object:object,
  56. Parent: parent,
  57. Getters: make(map[string] ObjectGetter),
  58. Setters: make(map[string] ObjectSetter),
  59. Methods: make(map[string] ObjectMethod),
  60. }
  61. mv.RedispatchCallable = NewRedispatchCallable("Object", mv)
  62. return mv
  63. }
  64. func (m *BasicObject) Get(key string) Value {
  65. var res ObjectGetter
  66. var ok bool
  67. if res, ok = m.Getters[key] ; !ok {
  68. if m.Parent != nil {
  69. return m.Parent.Get(key)
  70. }
  71. return NilValue
  72. }
  73. return res()
  74. }
  75. func (m *BasicObject) Set(key string, to Value) Value {
  76. var res ObjectSetter
  77. var ok bool
  78. if res, ok = m.Setters[key] ; !ok {
  79. if m.Parent != nil {
  80. return m.Parent.Set(key, to)
  81. }
  82. return NilValue
  83. }
  84. return res(to)
  85. }
  86. func (m *BasicObject) Invoke(key string, args... Value) []Value {
  87. var res ObjectMethod
  88. var ok bool
  89. if res, ok = m.Methods[key] ; !ok {
  90. if m.Parent != nil {
  91. return m.Parent.Invoke(key, args...)
  92. }
  93. return Fail(NewErrorValuef("No such method %s", key))
  94. }
  95. return res(args...)
  96. }
  97. func (from *BasicObject) Convert(to interface{}) error {
  98. switch toPtr := to.(type) {
  99. case **BasicObject:
  100. (*toPtr) = from
  101. case *Value:
  102. (*toPtr) = from
  103. default:
  104. return NewErrorValuef("Cannot convert value %v to %v", from, to)
  105. }
  106. return nil
  107. }
  108. // BasicObject implements Object
  109. var _ Object = &BasicObject{}
  110. // Flex can have any field just by setting it, it can be wrapped in
  111. // an BasicObject.
  112. type Flex struct {
  113. Fields map [string]Value
  114. }