gott.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package gott
  2. import (
  3. "fmt"
  4. "log"
  5. "reflect"
  6. "runtime"
  7. )
  8. type LogLevel int
  9. // LogLevel specifies what to log:
  10. // Quiet logs nothing,
  11. // Error logs only errors,
  12. // Debug logs functions that run,
  13. // Info logs run and skipped functions
  14. const (
  15. Quiet LogLevel = iota
  16. Error
  17. Debug
  18. Info
  19. )
  20. func logErr(e error, fn interface{}) {
  21. fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
  22. log.Printf("Function %s returned error: %v", fnName, e)
  23. }
  24. func logMsg(msg string, fn interface{}) {
  25. fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
  26. log.Printf("%s %s", msg, fnName)
  27. }
  28. // Exception is a type encapsulating anything contained in panic.
  29. // It implements Error() and therefore can be used as error.
  30. type Exception struct {
  31. E interface{}
  32. }
  33. func (e Exception) Error() string {
  34. return fmt.Sprintf("function panicked")
  35. }
  36. // Tuple is a type encapsulating a slice of interface{}.
  37. type Tuple []interface{}
  38. // Result is a simplification of Either monad. It’s either successful—and
  39. // carries an interface{}—or unsuccessful—and carries an error.
  40. type Result struct {
  41. s interface{}
  42. e error
  43. LogLevel LogLevel
  44. }
  45. // NewResult creates initial Result passed to functions.
  46. func NewResult(s interface{}) *Result {
  47. return &Result{s, nil, Quiet}
  48. }
  49. // Bind performs fn on the receiver’s success value and assigns the returned
  50. // values to the receiver if the receiver is successful. In either case,
  51. // Bind returns the receiver.
  52. // Bind operates on functions that return value and error.
  53. func (r *Result) Bind(fn func(...interface{}) (interface{}, error)) *Result {
  54. if r.e == nil {
  55. if r.LogLevel >= Debug {
  56. logMsg("running:", fn)
  57. }
  58. if s, ok := r.s.(Tuple); ok {
  59. r.s, r.e = fn(s...)
  60. } else {
  61. r.s, r.e = fn(r.s)
  62. }
  63. if r.e != nil && r.LogLevel >= Error {
  64. logErr(r.e, fn)
  65. }
  66. } else {
  67. if r.LogLevel >= Info {
  68. logMsg("skipping:", fn)
  69. }
  70. }
  71. return r
  72. }
  73. // Map performs fn on the receiver’s success value and assigns the returned
  74. // value to the it if the receiver is successful. In either case, Map returns
  75. // the receiver.
  76. // Map operates on functions that are always successful and return only one
  77. // value
  78. func (r *Result) Map(fn func(...interface{}) interface{}) *Result {
  79. if r.e == nil {
  80. if r.LogLevel >= Debug {
  81. logMsg("running:", fn)
  82. }
  83. if s, ok := r.s.(Tuple); ok {
  84. r.s = fn(s...)
  85. } else {
  86. r.s = fn(r.s)
  87. }
  88. } else {
  89. if r.LogLevel >= Info {
  90. logMsg("skipping:", fn)
  91. }
  92. }
  93. return r
  94. }
  95. // Tee performs fn on the receiver’s success value and assigns the returned
  96. // error to the receiver if the receiver is successful. In either case, Tee
  97. // returns the receiver.
  98. // Tee operates on functions that perform side effects and might return an
  99. // error
  100. func (r *Result) Tee(fn func(...interface{}) error) *Result {
  101. if r.e == nil {
  102. if r.LogLevel >= Debug {
  103. logMsg("running:", fn)
  104. }
  105. if s, ok := r.s.(Tuple); ok {
  106. r.e = fn(s...)
  107. } else {
  108. r.e = fn(r.s)
  109. }
  110. if r.e != nil && r.LogLevel >= Error {
  111. logErr(r.e, fn)
  112. }
  113. } else {
  114. if r.LogLevel >= Info {
  115. logMsg("skipping:", fn)
  116. }
  117. }
  118. return r
  119. }
  120. // SafeTee performs fn on the receiver’s success value if the receiver is
  121. // successful. In either case, SafeTee returns the receiver.
  122. // SafeTee operates on functions that perform side effects and are always
  123. // successful.
  124. func (r *Result) SafeTee(fn func(...interface{})) *Result {
  125. if r.e == nil {
  126. if r.LogLevel >= Debug {
  127. logMsg("running:", fn)
  128. }
  129. if s, ok := r.s.(Tuple); ok {
  130. fn(s...)
  131. } else {
  132. fn(r.s)
  133. }
  134. } else {
  135. if r.LogLevel >= Info {
  136. logMsg("skipping:", fn)
  137. }
  138. }
  139. return r
  140. }
  141. // Catch performs fn on the receiver’s success value and assigns the returned
  142. // vale to it if the receiver is successful. If fn panics, Catch recovers and
  143. // stores the value passed to panic in receiver’s error as Exception. In either
  144. // case, Catch returns the receiver.
  145. func (r *Result) Catch(fn func(...interface{}) interface{}) (result *Result) {
  146. if r.e == nil {
  147. if r.LogLevel >= Debug {
  148. logMsg("running:", fn)
  149. }
  150. defer func() {
  151. if err := recover(); err != nil {
  152. r.e = Exception{err}
  153. result = r
  154. if r.e != nil && r.LogLevel >= Error {
  155. logErr(r.e, fn)
  156. }
  157. }
  158. }()
  159. if s, ok := r.s.(Tuple); ok {
  160. r.s = fn(s...)
  161. } else {
  162. r.s = fn(r.s)
  163. }
  164. } else {
  165. if r.LogLevel >= Info {
  166. logMsg("skipping:", fn)
  167. }
  168. }
  169. return r
  170. }
  171. // Revover tries to put processing back on the happy track.
  172. // If receiver is not successful, Recover calls the passed function and
  173. // assignes the returned values to the receiver. In either case, Recover
  174. // returns the receiver.
  175. func (r *Result) Recover(fn func(...interface{}) (interface{}, error)) *Result {
  176. if r.e != nil {
  177. if r.LogLevel >= Debug {
  178. logMsg("running:", fn)
  179. }
  180. if s, ok := r.s.(Tuple); ok {
  181. r.s, r.e = fn(s...)
  182. } else {
  183. r.s, r.e = fn(r.s)
  184. }
  185. if r.e != nil && r.LogLevel >= Error {
  186. logErr(r.e, fn)
  187. }
  188. } else {
  189. if r.LogLevel >= Info {
  190. logMsg("skipping:", fn)
  191. }
  192. }
  193. return r
  194. }
  195. // Handle performs onSuccess on the receiver’s success value if the receiver is
  196. // successful, or onError on the receiver’s error otherwise. In either case,
  197. // Handle returns the receiver.
  198. func (r *Result) Handle(onSuccess func(...interface{}), onError func(error)) *Result {
  199. if r.e == nil {
  200. if r.LogLevel >= Debug {
  201. logMsg("running:", onSuccess)
  202. }
  203. if s, ok := r.s.(Tuple); ok {
  204. onSuccess(s...)
  205. } else {
  206. onSuccess(r.s)
  207. }
  208. } else {
  209. if r.LogLevel >= Debug {
  210. logMsg("running:", onError)
  211. }
  212. onError(r.e)
  213. }
  214. return r
  215. }
  216. // Finish returns the values stored in the receiver.
  217. func (r *Result) Finish() (interface{}, error) {
  218. if r.e != nil {
  219. return nil, r.e
  220. } else {
  221. return r.s, nil
  222. }
  223. }