sequencer.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package seq
  2. import (
  3. "reflect"
  4. "dev.hexasoftware.com/hxs/prettylog"
  5. )
  6. var (
  7. log = prettylog.New("Seq")
  8. )
  9. type Entry struct {
  10. desc string
  11. fn reflect.Value
  12. seqFn func(sc *Context) error
  13. param []Param
  14. grab []reflect.Value
  15. }
  16. // Helper
  17. type Seq struct {
  18. seq []*Entry
  19. last *Entry
  20. }
  21. func NewSeq() *Seq {
  22. return &Seq{[]*Entry{}, nil}
  23. }
  24. func (s *Seq) Add(params ...interface{}) *Seq {
  25. entry := &Entry{}
  26. fni := 0
  27. if v, ok := params[0].(string); ok {
  28. entry.desc = v
  29. fni = 1
  30. }
  31. fn := params[fni]
  32. if v, ok := fn.(func(s *Context) error); ok {
  33. log.Println("Special func")
  34. entry.seqFn = v
  35. } else {
  36. entry.fn = reflect.ValueOf(fn)
  37. fnTyp := reflect.TypeOf(fn)
  38. if fnTyp.Kind() != reflect.Func {
  39. log.Fatal("First param must be a function")
  40. }
  41. vparam := []Param{}
  42. for _, p := range params[fni+1:] {
  43. sp := Param{}
  44. switch v := p.(type) {
  45. case Param:
  46. sp = v
  47. default:
  48. sp.typ = 0
  49. sp.param = reflect.ValueOf(p)
  50. }
  51. vparam = append(vparam, sp)
  52. }
  53. entry.param = vparam
  54. }
  55. s.seq = append(s.seq, entry)
  56. s.last = entry
  57. return s
  58. }
  59. func (s *Seq) Grab(grab ...interface{}) {
  60. if s.last == nil {
  61. return
  62. }
  63. for _, g := range grab {
  64. gv := reflect.ValueOf(g)
  65. if gv.Kind() != reflect.Ptr {
  66. panic("Grab should be pointer")
  67. }
  68. s.last.grab = append(s.last.grab, gv)
  69. }
  70. }
  71. func (s *Seq) Exec(eparam ...interface{}) error {
  72. sCon := NewContext()
  73. sCon.params = eparam
  74. for _, e := range s.seq {
  75. if e.seqFn != nil {
  76. err := e.seqFn(sCon)
  77. sCon.Log("E:", e.desc, "=>", err)
  78. if err != nil {
  79. return err
  80. }
  81. continue
  82. }
  83. param := []reflect.Value{}
  84. for _, p := range e.param {
  85. var val reflect.Value
  86. switch p.typ {
  87. case 0:
  88. val = p.param
  89. case 1:
  90. val = reflect.ValueOf(sCon.Ret(p.nres, p.ires))
  91. case 2:
  92. val = reflect.Indirect(reflect.ValueOf(eparam[p.nres]))
  93. }
  94. param = append(param, val)
  95. }
  96. retVal := e.fn.Call(param)
  97. iretList := []interface{}{}
  98. for i, r := range retVal { // Populate grabs
  99. if i < len(e.grab) {
  100. e.grab[i].Elem().Set(r)
  101. }
  102. iret := r.Interface()
  103. if err, ok := iret.(error); ok {
  104. return err
  105. }
  106. iretList = append(iretList, iret)
  107. }
  108. sCon.ret = append(sCon.ret, iretList)
  109. if e.desc != "" { // Only if we have a description
  110. sCon.Log("E:", e.desc, "=>", iretList)
  111. }
  112. }
  113. sCon.LogRep()
  114. return nil
  115. }