-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.go
More file actions
42 lines (34 loc) · 786 Bytes
/
interface.go
File metadata and controls
42 lines (34 loc) · 786 Bytes
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
package gogen
import (
"bytes"
"context"
"fmt"
)
type InterfaceBuilder struct {
name string
body []Code
}
func Interface(name string) (r *InterfaceBuilder) {
r = &InterfaceBuilder{}
r.name = name
return
}
func (b *InterfaceBuilder) BodySnippet(template string, vars ...string) (r *InterfaceBuilder) {
b.Body(Snippet(template, vars...))
return b
}
func (b *InterfaceBuilder) Body(decls ...Code) (r *InterfaceBuilder) {
b.body = append(b.body, decls...)
return b
}
func (b *InterfaceBuilder) MarshalCode(ctx context.Context) (r []byte, err error) {
buf := bytes.NewBuffer(nil)
buf.WriteString(fmt.Sprintf("\ntype %s interface {\n", b.name))
err = Fprint(buf, Snippets(b.body...), ctx)
if err != nil {
return
}
buf.WriteString("}\n")
r = buf.Bytes()
return
}