/
polymorph.go
62 lines (52 loc) · 1.38 KB
/
polymorph.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
package extra
import (
"strconv"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
"github.com/insolar/assured-ledger/ledger-core/insproto"
)
type Polymorph struct {
*generator.Generator
generator.PluginImports
customRegistrations []string
}
func (p *Polymorph) Init(g *generator.Generator, imports generator.PluginImports) {
p.Generator = g
p.PluginImports = imports
p.customRegistrations = nil
}
func (p *Polymorph) GenerateMsg(file *generator.FileDescriptor, message *generator.Descriptor, ccTypeName string, isHead bool) {
id := insproto.GetPolymorphID(message.DescriptorProto)
if id == 0 {
return
}
idStr := strconv.FormatUint(id, 10)
p.P(`const Type`, ccTypeName, `PolymorphID = `, idStr)
p.P()
p.P(`func (*`, ccTypeName, `) GetDefaultPolymorphID() uint64 {`)
p.In()
p.P(`return `, idStr)
p.Out()
p.P(`}`)
p.P()
if customReg := insproto.GetCustomRegister(file.FileDescriptorProto, message.DescriptorProto); customReg != "" {
special := ``
if isHead {
special = message.GetName()
}
customReg = importCustomName(customReg, p.PluginImports)
p.customRegistrations = append(p.customRegistrations,
customReg+`(`+idStr+`, "`+special+`", (*`+ccTypeName+`)(nil))`)
}
}
func (p *Polymorph) GenerateFile() {
if len(p.customRegistrations) > 0 {
p.P(`func init() {`)
p.In()
for _, s := range p.customRegistrations {
p.P(s)
}
p.Out()
p.P(`}`)
p.P()
}
}