-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeconv.go
133 lines (115 loc) · 4.29 KB
/
typeconv.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package fproto_gowrap
import "github.com/RangelReale/fproto"
// Interface for custom type converters
type TypeConverter interface {
// Returns the list of type identifications this converter supports.
GetSources() []TypeConverterSource
// Returns a type name. If pbsource=true, returns the Golang protobuf type, else returns the GoWrap type.
GetType(g *Generator, fldtype string, pbsource bool) (string, bool)
// Generates a field inside a struct definition.
GenerateField(g *Generator, message *fproto.MessageElement, fld fproto.FieldElementTag) (bool, error)
// Generates a field helper if needed (oneof needs it).
GenerateFieldHelper(g *Generator, message *fproto.MessageElement, fld fproto.FieldElementTag) (bool, error)
// Generates code to import one fields.
GenerateFieldImport(g *Generator, message *fproto.MessageElement, fld fproto.FieldElementTag) (bool, error)
// Generates code to export one fields.
GenerateFieldExport(g *Generator, message *fproto.MessageElement, fld fproto.FieldElementTag) (bool, error)
// Generate codee to import one field into service definitions.
GenerateSrvImport(srvtype string, g *Generator, reqVarName string, retVarName string, fldtype string) (bool, error)
// Generate codee to export one field into service definitions.
GenerateSrvExport(srvtype string, g *Generator, reqVarName string, retVarName string, fldtype string) (bool, error)
// Returns the empty value for the type.
EmptyValue(g *Generator, fldtype string, pbsource bool) (string, bool)
}
// Identification of a type
type TypeConverterSource struct {
// protobuf file path (ex: google/protobuf/empty.proto)
FilePath string
// protobuf package name (ex: google.protobuf)
PackageName string
}
// Wrapper to use more than one TypeConverter at the same time
type TypeConverterList struct {
list []TypeConverter
}
func NewTypeConverterList(list []TypeConverter) *TypeConverterList {
return &TypeConverterList{list}
}
func (t *TypeConverterList) GetSources() []TypeConverterSource {
if len(t.list) > 0 {
return t.list[0].GetSources()
}
return []TypeConverterSource{}
}
func (t *TypeConverterList) GetType(g *Generator, fldtype string, pbsource bool) (string, bool) {
for _, item := range t.list {
s, ok := item.GetType(g, fldtype, pbsource)
if ok {
return s, ok
}
}
return "", false
}
func (t *TypeConverterList) GenerateField(g *Generator, message *fproto.MessageElement, fld fproto.FieldElementTag) (bool, error) {
for _, item := range t.list {
ok, err := item.GenerateField(g, message, fld)
if ok || err != nil {
return ok, err
}
}
return false, nil
}
func (t *TypeConverterList) GenerateFieldHelper(g *Generator, message *fproto.MessageElement, fld fproto.FieldElementTag) (bool, error) {
for _, item := range t.list {
ok, err := item.GenerateFieldHelper(g, message, fld)
if ok || err != nil {
return ok, err
}
}
return false, nil
}
func (t *TypeConverterList) GenerateFieldImport(g *Generator, message *fproto.MessageElement, fld fproto.FieldElementTag) (bool, error) {
for _, item := range t.list {
ok, err := item.GenerateFieldImport(g, message, fld)
if ok || err != nil {
return ok, err
}
}
return false, nil
}
func (t *TypeConverterList) GenerateFieldExport(g *Generator, message *fproto.MessageElement, fld fproto.FieldElementTag) (bool, error) {
for _, item := range t.list {
ok, err := item.GenerateFieldExport(g, message, fld)
if ok || err != nil {
return ok, err
}
}
return false, nil
}
func (t *TypeConverterList) GenerateSrvImport(srvtype string, g *Generator, reqVarName string, retVarName string, fldtype string) (bool, error) {
for _, item := range t.list {
ok, err := item.GenerateSrvImport(srvtype, g, reqVarName, retVarName, fldtype)
if ok || err != nil {
return ok, err
}
}
return false, nil
}
func (t *TypeConverterList) GenerateSrvExport(srvtype string, g *Generator, reqVarName string, retVarName string, fldtype string) (bool, error) {
for _, item := range t.list {
ok, err := item.GenerateSrvExport(srvtype, g, reqVarName, retVarName, fldtype)
if ok || err != nil {
return ok, err
}
}
return false, nil
}
func (t *TypeConverterList) EmptyValue(g *Generator, fldtype string, pbsource bool) (string, bool) {
for _, item := range t.list {
s, ok := item.EmptyValue(g, fldtype, pbsource)
if ok {
return s, ok
}
}
return "", false
}