-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
45 lines (38 loc) · 864 Bytes
/
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
package jot
import (
"fmt"
"io"
)
type InterfaceSpec struct {
Doc string
Name string
Methods []*FunctionSpec
}
func Interface(name string) *InterfaceSpec {
return &InterfaceSpec{Name: name}
}
func (s *InterfaceSpec) AddMethod(fn *FunctionSpec) *InterfaceSpec {
s.Methods = append(s.Methods, fn)
return s
}
func (s *InterfaceSpec) SetDoc(doc string) *InterfaceSpec {
s.Doc = doc
return s
}
func (s *InterfaceSpec) Write(w io.Writer) error {
WriteDoc(w, s.Doc)
io.WriteString(w, fmt.Sprintf("type %s interface {", s.Name))
if len(s.Methods) > 0 {
io.WriteString(w, "\n")
for _, f := range s.Methods {
io.WriteString(w, fmt.Sprintf("%s(", f.Name))
f.writeParameters(w)
io.WriteString(w, fmt.Sprintf(")"))
f.writeReturnTypes(w)
io.WriteString(w, "\n")
}
}
io.WriteString(w, "}")
io.WriteString(w, "\n")
return nil
}