-
Notifications
You must be signed in to change notification settings - Fork 8
/
generator.go
55 lines (49 loc) · 1.43 KB
/
generator.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
package internal
import (
"fmt"
"io"
"os"
"strings"
"github.com/eatmoreapple/juice"
)
type Generator struct {
cfg juice.IConfiguration
impl *Implement
namespace string
}
func (g *Generator) Generate() (io.Reader, error) {
for _, m := range g.impl.iface.Methods() {
method := m
key := fmt.Sprintf("%s.%s", g.namespace, method.Name())
statement, err := g.cfg.GetStatement(key)
if err != nil {
return nil, err
}
if statement.Attribute("gen") == "false" || statement.Attribute("generate") == "false" { // skip
continue
}
function := &Function{method: method, receiver: g.impl.dst, typename: g.impl.src}
maker := FunctionBodyMaker{statement: statement, function: function}
if err = maker.Make(); err != nil {
return nil, err
}
g.impl.methods = append(g.impl.methods, function)
}
builder := strings.Builder{}
args := strings.Join(os.Args[:], " ")
builder.WriteString(fmt.Sprintf("// Code generated by \"%s\"; DO NOT EDIT.", args))
builder.WriteString("\n\n")
builder.WriteString(g.impl.String())
return strings.NewReader(builder.String()), nil
}
// WriteTo writes generated code to writer.
func (g *Generator) WriteTo(writer io.Writer) (int64, error) {
reader, err := g.Generate()
if err != nil {
return 0, err
}
return io.Copy(writer, reader)
}
func newGenerator(namespace string, cfg juice.IConfiguration, impl *Implement) *Generator {
return &Generator{cfg: cfg, impl: impl, namespace: namespace}
}