-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_producer_gen.go
81 lines (67 loc) · 1.99 KB
/
php_producer_gen.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
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"bytes"
"os"
"strings"
"text/template"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/yoozoo/protocli/generator/data"
)
type phpProdGen struct {
producerTpl *template.Template
}
func (g *phpProdGen) Init(request *plugin.CodeGeneratorRequest) {
g.producerTpl = util.getTpl("/template/php/producer.gophp")
}
func (g *phpProdGen) genProducer(applicationName, packageName, className, topic string, msg *data.ProtoMessage) string {
buf := bytes.NewBufferString("")
if len(packageName) <= 0 {
packageName = applicationName
}
data := map[string]interface{}{
"StrongType": !(len(msg.Proto.Field) == 1 &&
msg.Proto.Field[0].GetType().String() == "TYPE_STRING"),
"PackageName": strings.Title(packageName),
"ClassName": className,
"Topic": topic,
}
if data["StrongType"].(bool) {
data["QueueType"] = msg.Proto.GetName()
}
err := g.producerTpl.Execute(buf, data)
if err != nil {
util.Die(err)
}
return buf.String()
}
func (g *phpProdGen) Gen(applicationName string, packageName string, services []*data.ServiceData, messages []*data.MessageData, enums []*data.EnumData, options data.OptionMap) (result map[string]string, err error) {
packageName = strings.Title(packageName)
result = make(map[string]string)
topicMap, err := util.RetriveTopics(messages)
if err != nil {
return nil, err
}
for _, msg := range messages {
topic, found := topicMap[msg.Name]
// only care about msg with mqcommon.topic option
if !found {
continue
}
m, _ := data.GetMessageProtoAndFile(msg.Name)
className := m.Proto.GetName() + "Producer"
fileName := className + ".php"
if len(packageName) > 0 {
fileName = packageName + string(os.PathSeparator) + fileName
}
content := g.genProducer(applicationName, packageName, className, topic, m)
result[fileName] = content
println("gen", topic)
}
return
}
func (g *phpProdGen) GetLang() string {
return "php"
}
func init() {
data.RegisterCodeGenerator("phpproducer", &phpProdGen{})
}