refactoring OnAbort
no need for pointer receiver for wrapper methods
Vladimir Bauer
2 years ago
| 0 | 0 | package decor |
| 1 | 1 | |
| 2 | 2 | var ( |
| 3 | _ Decorator = (*onAbortWrapper)(nil) | |
| 4 | _ Wrapper = (*onAbortWrapper)(nil) | |
| 3 | _ Decorator = onAbortWrapper{} | |
| 4 | _ Wrapper = onAbortWrapper{} | |
| 5 | 5 | ) |
| 6 | 6 | |
| 7 | // OnAbort returns decorator, which wraps provided decorator with sole | |
| 8 | // purpose to display provided message on abort event. It has no effect | |
| 9 | // if bar.Abort(drop bool) is called with true argument. | |
| 7 | // OnAbort wrap decorator. | |
| 8 | // Displays provided message on abort event. | |
| 9 | // Has no effect if bar.Abort(true) is called. | |
| 10 | 10 | // |
| 11 | 11 | // `decorator` Decorator to wrap |
| 12 | // | |
| 13 | // `message` message to display on abort event | |
| 12 | // `message` message to display | |
| 14 | 13 | func OnAbort(decorator Decorator, message string) Decorator { |
| 15 | 14 | if decorator == nil { |
| 16 | 15 | return nil |
| 17 | 16 | } |
| 18 | d := &onAbortWrapper{ | |
| 19 | Decorator: decorator, | |
| 20 | msg: message, | |
| 21 | } | |
| 22 | if md, ok := decorator.(*mergeDecorator); ok { | |
| 23 | d.Decorator, md.Decorator = md.Decorator, d | |
| 24 | return md | |
| 25 | } | |
| 26 | return d | |
| 17 | return onAbortWrapper{decorator, message} | |
| 27 | 18 | } |
| 28 | 19 | |
| 29 | 20 | type onAbortWrapper struct { |
| 31 | 22 | msg string |
| 32 | 23 | } |
| 33 | 24 | |
| 34 | func (d *onAbortWrapper) Decor(s Statistics) string { | |
| 25 | func (d onAbortWrapper) Decor(s Statistics) (string, int) { | |
| 35 | 26 | if s.Aborted { |
| 36 | return d.GetConf().FormatMsg(d.msg) | |
| 27 | return d.Format(d.msg) | |
| 37 | 28 | } |
| 38 | 29 | return d.Decorator.Decor(s) |
| 39 | 30 | } |
| 40 | 31 | |
| 41 | func (d *onAbortWrapper) Unwrap() Decorator { | |
| 32 | func (d onAbortWrapper) Unwrap() Decorator { | |
| 42 | 33 | return d.Decorator |
| 43 | 34 | } |