context.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package seq
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type Context struct {
  7. vars map[string]interface{}
  8. params []interface{}
  9. ret [][]interface{}
  10. rep []string
  11. }
  12. func NewContext() *Context {
  13. return &Context{
  14. vars: map[string]interface{}{},
  15. ret: [][]interface{}{},
  16. rep: []string{},
  17. }
  18. }
  19. func (sc *Context) Log(param ...interface{}) {
  20. sc.rep = append(sc.rep, fmt.Sprint(param...))
  21. }
  22. func (sc *Context) LogRep() {
  23. log.Println("Report:")
  24. for _, r := range sc.rep {
  25. log.Println(r)
  26. }
  27. }
  28. // Context
  29. func (sc *Context) Set(name string, value interface{}) {
  30. sc.vars[name] = value
  31. }
  32. func (sc *Context) Get(name string, valPtr ...interface{}) interface{} {
  33. if len(valPtr) > 0 {
  34. pv := reflect.ValueOf(valPtr[0])
  35. pv.Elem().Set(reflect.ValueOf(sc.vars[name]))
  36. }
  37. return sc.vars[name]
  38. }
  39. func (sc *Context) Param(n uint, valPtr ...interface{}) interface{} {
  40. if n > uint(len(sc.params)) {
  41. return nil
  42. }
  43. if len(valPtr) > 0 {
  44. pv := reflect.ValueOf(valPtr[0])
  45. pv.Elem().Set(reflect.ValueOf(sc.params[n]))
  46. }
  47. return sc.params[n]
  48. }
  49. //Ret return the value from previous returns
  50. func (sc *Context) Ret(n, i int) interface{} {
  51. return sc.ret[n][i]
  52. }