-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
adapter.go
134 lines (116 loc) · 3.2 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package pio
import (
"io"
)
// Available sources.
type (
printFunc func(interface{})
printVariadicFunc func(...interface{})
printfFunc func(string, ...interface{})
printlnFunc func(string)
)
type writerFunc func([]byte) (int, error)
func (w writerFunc) Write(p []byte) (n int, err error) {
return w(p)
}
// Wrap returns a new output based on the "printfFn"
// if not a compatible output found then it will
// return a writer which writes nothing.
//
// To check if the wrapping worked
// you can check if the result `io.Writer`
// `IsNop`, i.e:
// std's log.Panicf is not a compatible interface
//
// output := Output(log.Panicf)
//
// if IsNop(output) {
// // conversation failed, do something or panic.
// }
func Wrap(printFn interface{}) io.Writer {
switch printFn.(type) {
case io.Writer:
return printFn.(io.Writer)
case writerFunc:
return printFn.(io.Writer)
case printFunc:
return OutputFrom.Print(printFn.(printFunc))
case printVariadicFunc:
return OutputFrom.PrintVardiadic(printFn.(printVariadicFunc))
case printfFunc:
return OutputFrom.Printf(printFn.(printfFunc))
case printlnFunc:
return OutputFrom.Println(printFn.(printlnFunc), false)
}
return NopOutput()
}
// OutputFrom is a variable
// which contains some helpers that can
// convert some forms of output to compatible `io.Writer`
// in order to be passed to the `NewPrinter` or `Register` functions.
var OutputFrom = OutputAdapters{}
// OutputAdapters is a struct
// which contains some forms of output
// and convert them to a compatible `io.Writer`
// in order to be passed to the `NewPrinter` or `Register` functions.
type OutputAdapters struct{}
// Print converts a func(v interface{}) to a compatible `io.Writer`.
func (a *OutputAdapters) Print(print func(v interface{})) io.Writer {
return &printAdapter{
print: print,
}
}
// PrintVardiadic converts a func(v ...interface{}) to a compatible `io.Writer`.
func (a *OutputAdapters) PrintVardiadic(print func(v ...interface{})) io.Writer {
return &printVariadicAdapter{
printVariadic: print,
}
}
// Printf converts a func(string, ...interface{}) to a compatible `io.Writer`.
func (a *OutputAdapters) Printf(printf func(format string, args ...interface{})) io.Writer {
return &printfAdapter{
printf: printf,
}
}
// Println converts a func(string) to a compatible `io.Writer`.
// if "newLine" is true then "\n" will be appended to the "s".
func (a *OutputAdapters) Println(println func(s string), newLine bool) io.Writer {
return &printlnAdapter{
println: println,
newLine: newLine,
}
}
type (
printAdapter struct {
print printFunc
}
printVariadicAdapter struct {
printVariadic printVariadicFunc
}
printfAdapter struct {
printf printfFunc
}
printlnAdapter struct {
println printlnFunc
newLine bool
}
)
func (p *printAdapter) Write(b []byte) (int, error) {
p.print(string(b))
return len(b), nil
}
func (p *printVariadicAdapter) Write(b []byte) (int, error) {
p.printVariadic(string(b))
return len(b), nil
}
func (p *printfAdapter) Write(b []byte) (int, error) {
p.printf(string(b))
return len(b), nil
}
func (p *printlnAdapter) Write(b []byte) (int, error) {
if p.newLine {
b = append(b, NewLine...)
}
p.println(string(b))
return len(b), nil
}