vnode.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package vnode
  2. import (
  3. "fmt"
  4. "log"
  5. "reflect"
  6. )
  7. type IVNode interface {
  8. Result() interface{}
  9. }
  10. const (
  11. OPT_VAR = iota
  12. OPT_FUNC
  13. OPT_FUNCI
  14. OPT_SFUNC
  15. )
  16. type Result interface{}
  17. type VNodeFunc func(*VNode) Result
  18. type SourceEntry struct {
  19. fnInputType reflect.Type
  20. srcInputInterface interface{}
  21. srcInputValue reflect.Value //
  22. srcInputFn func() interface{}
  23. //iface interface{}
  24. Value func() reflect.Value
  25. //fnHandler func() reflect.Value
  26. opType int
  27. }
  28. type VNode struct {
  29. sourceList []SourceEntry
  30. mainFuncValue reflect.Value
  31. specialFn func(...interface{}) interface{}
  32. }
  33. func New(fns ...interface{}) *VNode {
  34. var vn VNode
  35. fn := fns[0]
  36. nInputs := 0
  37. if len(fns) > 1 {
  38. nInputs = fns[1].(int)
  39. }
  40. rfn := reflect.ValueOf(fn)
  41. // Or Pointer
  42. // Check type func
  43. if rfn.Kind() != reflect.Func {
  44. log.Println("Argument should be a function")
  45. return nil
  46. }
  47. vn.mainFuncValue = rfn
  48. // could this reduce complexity??
  49. /*var Func func(...interface{}) interface{}
  50. if rfn.Type() != reflect.TypeOf(Func) {
  51. nInputs = rfn.Type().NumIn()
  52. madeFn := reflect.MakeFunc(reflect.TypeOf(Func), func(in []reflect.Value) []reflect.Value {
  53. nargs := []reflect.Value{}
  54. for i := 0; i < in[0].Len(); i++ {
  55. narg := in[0].Index(i).Elem()
  56. nargs = append(nargs, narg)
  57. }
  58. res := rfn.Call(nargs)
  59. v := res[0].Interface()
  60. return []reflect.Value{reflect.ValueOf(&v).Elem()}
  61. })
  62. reflect.ValueOf(&Func).Elem().Set(madeFn)
  63. fn = Func
  64. }*/
  65. switch fn.(type) {
  66. case func(...interface{}) interface{}:
  67. vn.specialFn = fn.(func(...interface{}) interface{})
  68. vn.sourceList = make([]SourceEntry, nInputs)
  69. for i := 0; i < nInputs; i++ {
  70. vn.sourceList[i].fnInputType = nil
  71. }
  72. default:
  73. nInputs = rfn.Type().NumIn()
  74. vn.sourceList = make([]SourceEntry, nInputs)
  75. for i := 0; i < nInputs; i++ {
  76. vn.sourceList[i].fnInputType = rfn.Type().In(i)
  77. }
  78. }
  79. return &vn
  80. }
  81. func (vn *VNode) SourceAt(i int, val interface{}) error {
  82. se := &vn.sourceList[i]
  83. // If fninputType is interface we can accept anything
  84. // Do some type checks here
  85. se.srcInputValue = reflect.ValueOf(val)
  86. if se.srcInputValue.Type() == reflect.TypeOf(vn) {
  87. se.srcInputValue = reflect.ValueOf(func() interface{} {
  88. return val.(*VNode).Exec()
  89. })
  90. }
  91. var t func() interface{}
  92. if se.srcInputValue.Type() == reflect.TypeOf(t) {
  93. se.Value = func() reflect.Value {
  94. return reflect.ValueOf(se.srcInputValue.Interface().(func() interface{})())
  95. }
  96. se.srcInputFn = se.srcInputValue.Interface().(func() interface{})
  97. se.opType = OPT_SFUNC
  98. return nil
  99. }
  100. // Especial case:
  101. if se.srcInputValue.Kind() == reflect.Func {
  102. if se.srcInputValue.Type().NumOut() != 1 { // we could ignore this
  103. return fmt.Errorf("Input: %d Invalid number of returns", i)
  104. }
  105. out := se.srcInputValue.Type().Out(0)
  106. // check output type if not interface or same type
  107. /*if out.Kind() != reflect.Interface && out != se.fnInputType {
  108. return fmt.Errorf("Input: %d Type mismatch", i)
  109. }*/
  110. if out.Kind() == reflect.Interface {
  111. se.Value = func() reflect.Value {
  112. return se.srcInputValue.Call([]reflect.Value{})[0].Elem()
  113. }
  114. se.opType = OPT_FUNCI
  115. } else {
  116. se.Value = func() reflect.Value {
  117. return se.srcInputValue.Call([]reflect.Value{})[0]
  118. }
  119. se.opType = OPT_FUNC
  120. }
  121. } else if se.srcInputValue.Type() == se.fnInputType || se.fnInputType == nil {
  122. se.Value = func() reflect.Value {
  123. return se.srcInputValue
  124. }
  125. se.srcInputInterface = se.srcInputValue.Interface()
  126. se.opType = OPT_VAR
  127. } else {
  128. return fmt.Errorf("Input: %d Didn't match", i)
  129. }
  130. //vn.sourceList[i] = se
  131. return nil
  132. }
  133. func (vn *VNode) Source(vnodes ...interface{}) error {
  134. for i, vnode := range vnodes {
  135. err := vn.SourceAt(i, vnode)
  136. if err != nil {
  137. return err
  138. }
  139. }
  140. return nil
  141. }
  142. // Change to execute
  143. func (vn *VNode) Exec() Result {
  144. //special case
  145. if vn.specialFn != nil {
  146. iargs := make([]interface{}, len(vn.sourceList))
  147. for i, se := range vn.sourceList {
  148. switch se.opType {
  149. case OPT_VAR: // Direct value input
  150. iargs[i] = se.srcInputInterface
  151. case OPT_SFUNC: // Function returning interface , optimizations
  152. iargs[i] = se.srcInputFn()
  153. default: // Function call
  154. iargs[i] = se.srcInputValue.Call([]reflect.Value{})[0].Interface()
  155. //iargs[i] = se.Value().Interface()
  156. }
  157. }
  158. return vn.specialFn(iargs...)
  159. }
  160. rargs := make([]reflect.Value, len(vn.sourceList))
  161. for i, se := range vn.sourceList {
  162. switch se.opType {
  163. case OPT_VAR: // Direct value input
  164. rargs[i] = se.srcInputValue
  165. case OPT_FUNC:
  166. rargs[i] = se.srcInputValue.Call([]reflect.Value{})[0]
  167. case OPT_FUNCI:
  168. rargs[i] = se.srcInputValue.Call([]reflect.Value{})[0].Elem()
  169. case OPT_SFUNC: // Function returning interface , optimizations
  170. rargs[i] = se.Value()
  171. default: // Function call
  172. rargs[i] = se.Value()
  173. //iargs[i] = se.srcInputValue.Call([]reflect.Value{})[0].Interface()
  174. //iargs[i] = se.Value().Interface()
  175. }
  176. }
  177. return vn.mainFuncValue.Call(rargs)[0].Interface()
  178. }
  179. // Exec input here
  180. /*func (vn *VNode) input(i int) interface{} {
  181. in := vn.sourceList[i]
  182. return in.fnHandler().Interface()
  183. }*/