processor.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package backends
  2. import (
  3. "github.com/flashmob/go-guerrilla/mail"
  4. "io"
  5. )
  6. type SelectTask int
  7. const (
  8. TaskSaveMail SelectTask = iota
  9. TaskValidateRcpt
  10. TaskSaveMailStream
  11. )
  12. func (o SelectTask) String() string {
  13. switch o {
  14. case TaskSaveMail:
  15. return "save mail"
  16. case TaskValidateRcpt:
  17. return "validate recipient"
  18. case TaskSaveMailStream:
  19. return "save mail stream"
  20. }
  21. return "[unnamed task]"
  22. }
  23. var BackendResultOK = NewResult("200 OK")
  24. // Our processor is defined as something that processes the envelope and returns a result and error
  25. type Processor interface {
  26. Process(*mail.Envelope, SelectTask) (Result, error)
  27. }
  28. // Signature of Processor
  29. type ProcessWith func(*mail.Envelope, SelectTask) (Result, error)
  30. // Make ProcessWith will satisfy the Processor interface
  31. func (f ProcessWith) Process(e *mail.Envelope, task SelectTask) (Result, error) {
  32. // delegate to the anonymous function
  33. return f(e, task)
  34. }
  35. // DefaultProcessor is a undecorated worker that does nothing
  36. // Notice DefaultProcessor has no knowledge of the other decorators that have orthogonal concerns.
  37. type DefaultProcessor struct{}
  38. // do nothing except return the result
  39. // (this is the last call in the decorator stack, if it got here, then all is good)
  40. func (w DefaultProcessor) Process(e *mail.Envelope, task SelectTask) (Result, error) {
  41. return BackendResultOK, nil
  42. }
  43. // if no processors specified, skip operation
  44. type NoopProcessor struct{ DefaultProcessor }
  45. type StreamProcessor interface {
  46. io.Writer
  47. }
  48. type StreamProcessWith func(p []byte) (n int, err error)
  49. func (f StreamProcessWith) Write(p []byte) (n int, err error) {
  50. // delegate to the anonymous function
  51. return f(p)
  52. }
  53. type DefaultStreamProcessor struct{}
  54. func (w DefaultStreamProcessor) Write(p []byte) (n int, err error) {
  55. return 0, nil
  56. }
  57. type NoopStreamProcessor struct{ DefaultStreamProcessor }