-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
53 lines (46 loc) · 1.03 KB
/
api.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
package main
import (
"fmt"
"strings"
"sync"
"github.com/betawaffle/kafka-gen-go/codegen"
"github.com/betawaffle/kafka-gen-go/schema"
"github.com/iancoleman/strcase"
)
type apiGenerator struct {
mu sync.Mutex
req *schema.MessageData
res *schema.MessageData
}
func (g *apiGenerator) addMessage(m *schema.MessageData) bool {
g.mu.Lock()
defer g.mu.Unlock()
var ok bool
switch m.Type {
case "request":
if g.req != nil {
panic("request conflict")
}
g.req, ok = m, g.res != nil
case "response":
if g.res != nil {
panic("response conflict")
}
g.res, ok = m, g.req != nil
default:
panic("BUG: unexpected message type: " + m.Type)
}
return ok
}
func (g *apiGenerator) getFileName() string {
req := strings.TrimSuffix(g.req.Name, "Request")
res := strings.TrimSuffix(g.res.Name, "Response")
if req != res {
panic(fmt.Errorf("mismatched req/res names; %s != %s", req, res))
}
return strcase.ToSnake(req) + "_gen.go"
}
func (g *apiGenerator) run(w *codegen.File) {
genMessage(w, g.req)
genMessage(w, g.res)
}