-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg.go
69 lines (57 loc) · 1.09 KB
/
pkg.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
package main
import (
"log"
"path/filepath"
"sync"
"github.com/betawaffle/kafka-gen-go/codegen"
"github.com/betawaffle/kafka-gen-go/schema"
)
type genImpl interface {
addMessage(*schema.MessageData) bool
getFileName() string
run(*codegen.File)
}
type pkgGenerator struct {
wg sync.WaitGroup
mu sync.Mutex
dst string
hdr hdrGenerator
api map[int16]*apiGenerator
}
func (g *pkgGenerator) addFile(src string) {
defer g.wg.Done()
m, err := schema.ReadMessageData(src)
if err != nil {
log.Print(err)
return
}
var impl genImpl
switch m.Type {
case "header":
impl = &g.hdr
case "request", "response":
impl = g.getAPI(*m.ApiKey)
default:
panic("unexpected message type: " + m.Type)
}
if !impl.addMessage(m) {
return
}
f := codegen.NewFile(filepath.Join(g.dst, impl.getFileName()))
impl.run(f)
if err = f.Flush(); err != nil {
log.Print(err)
}
}
func (g *pkgGenerator) getAPI(key int16) *apiGenerator {
g.mu.Lock()
defer g.mu.Unlock()
if a, ok := g.api[key]; ok {
return a
}
a := &apiGenerator{}
g.api[key] = a
return a
}
func (g *pkgGenerator) finish() {
}