gott.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. func (r *Result) SetLevelLog(l LogLevel) *Result {
  50. r.LogLevel = l
  51. return r
  52. }
  53. // Bind performs fn on the receiver’s success value and assigns the returned
  54. // values to the receiver if the receiver is successful. In either case,
  55. // Bind returns the receiver.
  56. // Bind operates on functions that return value and error.
  57. func (r *Result) Bind(fn func(...interface{}) (interface{}, error)) *Result {
  58. if r.e == nil {
  59. if r.LogLevel >= Debug {
  60. logMsg("running:", fn)
  61. }
  62. if s, ok := r.s.(Tuple); ok {
  63. r.s, r.e = fn(s...)
  64. } else {
  65. r.s, r.e = fn(r.s)
  66. }
  67. if r.e != nil && r.LogLevel >= Error {
  68. logErr(r.e, fn)
  69. }
  70. } else {
  71. if r.LogLevel >= Info {
  72. logMsg("skipping:", fn)
  73. }
  74. }
  75. return r
  76. }
  77. // Map performs fn on the receiver’s success value and assigns the returned
  78. // value to the it if the receiver is successful. In either case, Map returns
  79. // the receiver.
  80. // Map operates on functions that are always successful and return only one
  81. // value
  82. func (r *Result) Map(fn func(...interface{}) interface{}) *Result {
  83. if r.e == nil {
  84. if r.LogLevel >= Debug {
  85. logMsg("running:", fn)
  86. }
  87. if s, ok := r.s.(Tuple); ok {
  88. r.s = fn(s...)
  89. } else {
  90. r.s = fn(r.s)
  91. }
  92. } else {
  93. if r.LogLevel >= Info {
  94. logMsg("skipping:", fn)
  95. }
  96. }
  97. return r
  98. }
  99. // Tee performs fn on the receiver’s success value and assigns the returned
  100. // error to the receiver if the receiver is successful. In either case, Tee
  101. // returns the receiver.
  102. // Tee operates on functions that perform side effects and might return an
  103. // error
  104. func (r *Result) Tee(fn func(...interface{}) error) *Result {
  105. if r.e == nil {
  106. if r.LogLevel >= Debug {
  107. logMsg("running:", fn)
  108. }
  109. if s, ok := r.s.(Tuple); ok {
  110. r.e = fn(s...)
  111. } else {
  112. r.e = fn(r.s)
  113. }
  114. if r.e != nil && r.LogLevel >= Error {
  115. logErr(r.e, fn)
  116. }
  117. } else {
  118. if r.LogLevel >= Info {
  119. logMsg("skipping:", fn)
  120. }
  121. }
  122. return r
  123. }
  124. // SafeTee performs fn on the receiver’s success value if the receiver is
  125. // successful. In either case, SafeTee returns the receiver.
  126. // SafeTee operates on functions that perform side effects and are always
  127. // successful.
  128. func (r *Result) SafeTee(fn func(...interface{})) *Result {
  129. if r.e == nil {
  130. if r.LogLevel >= Debug {
  131. logMsg("running:", fn)
  132. }
  133. if s, ok := r.s.(Tuple); ok {
  134. fn(s...)
  135. } else {
  136. fn(r.s)
  137. }
  138. } else {
  139. if r.LogLevel >= Info {
  140. logMsg("skipping:", fn)
  141. }
  142. }
  143. return r
  144. }
  145. // Catch performs fn on the receiver’s success value and assigns the returned
  146. // vale to it if the receiver is successful. If fn panics, Catch recovers and
  147. // stores the value passed to panic in receiver’s error as Exception. In either
  148. // case, Catch returns the receiver.
  149. func (r *Result) Catch(fn func(...interface{}) interface{}) (result *Result) {
  150. if r.e == nil {
  151. if r.LogLevel >= Debug {
  152. logMsg("running:", fn)
  153. }
  154. defer func() {
  155. if err := recover(); err != nil {
  156. r.e = Exception{err}
  157. result = r
  158. if r.e != nil && r.LogLevel >= Error {
  159. logErr(r.e, fn)
  160. }
  161. }
  162. }()
  163. if s, ok := r.s.(Tuple); ok {
  164. r.s = fn(s...)
  165. } else {
  166. r.s = fn(r.s)
  167. }
  168. } else {
  169. if r.LogLevel >= Info {
  170. logMsg("skipping:", fn)
  171. }
  172. }
  173. return r
  174. }
  175. // Revover tries to put processing back on the happy track.
  176. // If receiver is not successful, Recover calls the passed function and
  177. // assignes the returned values to the receiver. In either case, Recover
  178. // returns the receiver.
  179. // The passed function gets the error (on nil) as the last argument
  180. func (r *Result) Recover(fn func(...interface{}) (interface{}, error)) *Result {
  181. if r.e != nil {
  182. if r.LogLevel >= Debug {
  183. logMsg("running:", fn)
  184. }
  185. if s, ok := r.s.(Tuple); ok {
  186. args := []interface{}{}
  187. for i := 0; i < len(s); i++ {
  188. args = append(args, s[i])
  189. }
  190. args = append(args, r.e)
  191. r.s, r.e = fn(args...)
  192. } else {
  193. r.s, r.e = fn(r.s, r.e)
  194. }
  195. if r.e != nil && r.LogLevel >= Error {
  196. logErr(r.e, fn)
  197. }
  198. } else {
  199. if r.LogLevel >= Info {
  200. logMsg("skipping:", fn)
  201. }
  202. }
  203. return r
  204. }
  205. // Handle performs onSuccess on the receiver’s success value if the receiver is
  206. // successful, or onError on the receiver’s error otherwise. In either case,
  207. // Handle returns the receiver.
  208. func (r *Result) Handle(onSuccess func(...interface{}), onError func(error)) *Result {
  209. if r.e == nil {
  210. if r.LogLevel >= Debug {
  211. logMsg("running:", onSuccess)
  212. }
  213. if s, ok := r.s.(Tuple); ok {
  214. onSuccess(s...)
  215. } else {
  216. onSuccess(r.s)
  217. }
  218. } else {
  219. if r.LogLevel >= Debug {
  220. logMsg("running:", onError)
  221. }
  222. onError(r.e)
  223. }
  224. return r
  225. }
  226. // Finish returns the values stored in the receiver.
  227. func (r *Result) Finish() (interface{}, error) {
  228. if r.e != nil {
  229. return nil, r.e
  230. } else {
  231. return r.s, nil
  232. }
  233. }