-
Notifications
You must be signed in to change notification settings - Fork 15
/
interface.go
85 lines (72 loc) · 1.72 KB
/
interface.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
package gg
import "io"
type isignature struct {
comments *group
name string
parameters *group
results *group
}
func signature(name string) *isignature {
i := &isignature{
name: name,
comments: newGroup("", "", "\n"),
parameters: newGroup("(", ")", ","),
results: newGroup("(", ")", ","),
}
// We should omit the `()` if result is empty
// Read about omit in NewFunction comments.
i.results.omitWrapIf = func() bool {
l := i.results.length()
if l == 0 {
// There is no result fields, we can omit `()` safely.
return true
}
return false
}
return i
}
func (i *isignature) render(w io.Writer) {
// Render function name
writeString(w, i.name)
// Render parameters
i.parameters.render(w)
// Render results
i.results.render(w)
}
func (i *isignature) AddParameter(name, typ interface{}) *isignature {
i.parameters.append(field(name, typ, " "))
return i
}
func (i *isignature) AddResult(name, typ interface{}) *isignature {
i.results.append(field(name, typ, " "))
return i
}
type iinterface struct {
name string
items *group
}
func Interface(name string) *iinterface {
return &iinterface{
name: name,
items: newGroup("{\n", "}", "\n"),
}
}
func (i *iinterface) render(w io.Writer) {
writeStringF(w, "type %s interface", i.name)
i.items.render(w)
}
func (i *iinterface) NewFunction(name string) *isignature {
sig := signature(name)
i.items.append(sig)
return sig
}
// AddLineComment will insert a new line comment.
func (i *iinterface) AddLineComment(content string, args ...interface{}) *iinterface {
i.items.append(LineComment(content, args...))
return i
}
// AddLine will insert a new line.
func (i *iinterface) AddLine() *iinterface {
i.items.append(Line())
return i
}