This repository has been archived by the owner on Feb 12, 2020. It is now read-only.
forked from actgardner/gogen-avro
/
union.go
136 lines (118 loc) · 3.57 KB
/
union.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
134
135
136
package generator
import (
"fmt"
)
const unionSerializerTemplate = `
func %v(r %v, w io.Writer) error {
err := writeLong(int64(r.UnionType), w)
if err != nil {
return err
}
switch r.UnionType{
%v
}
return fmt.Errorf("Invalid value for %v")
}
`
const unionDeserializerTemplate = `
func %v(r io.Reader) (%v, error) {
field, err := readLong(r)
var unionStr %v
if err != nil {
return unionStr, err
}
unionStr.UnionType = %v(field)
switch unionStr.UnionType {
%v
default:
return unionStr, fmt.Errorf("Invalid value for %v")
}
return unionStr, nil
}
`
type unionField struct {
name string
hasDef bool
itemType []field
}
func (s *unionField) Name() string {
return toPublicName(s.name)
}
func (s *unionField) FieldType() string {
var unionFields = "Union"
for _, i := range s.itemType {
unionFields += i.FieldType()
}
return unionFields
}
func (s *unionField) GoType() string {
return s.FieldType()
}
func (s *unionField) unionEnumType() string {
return fmt.Sprintf("%vTypeEnum", s.FieldType())
}
func (s *unionField) unionEnumDef() string {
var unionTypes string
for i, item := range s.itemType {
unionTypes += fmt.Sprintf("%v %v = %v\n", s.unionEnumType()+item.FieldType(), s.unionEnumType(), i)
}
return fmt.Sprintf("type %v int\nconst(\n%v)\n", s.unionEnumType(), unionTypes)
}
func (s *unionField) unionTypeDef() string {
var unionFields string
for _, i := range s.itemType {
unionFields += fmt.Sprintf("%v %v\n", i.FieldType(), i.GoType())
}
unionFields += fmt.Sprintf("UnionType %v", s.unionEnumType())
return fmt.Sprintf("type %v struct{\n%v\n}\n", s.FieldType(), unionFields)
}
func (s *unionField) unionSerializer() string {
switchCase := ""
for _, t := range s.itemType {
switchCase += fmt.Sprintf("case %v:\nreturn %v(r.%v, w)\n", s.unionEnumType()+t.FieldType(), t.SerializerMethod(), t.FieldType())
}
return fmt.Sprintf(unionSerializerTemplate, s.SerializerMethod(), s.GoType(), switchCase, s.GoType())
}
func (s *unionField) unionDeserializer() string {
switchCase := ""
for _, t := range s.itemType {
switchCase += fmt.Sprintf("case %v:\nval, err := %v(r)\nif err != nil {return unionStr, err}\nunionStr.%v = val\n", s.unionEnumType()+t.FieldType(), t.DeserializerMethod(), t.FieldType())
}
return fmt.Sprintf(unionDeserializerTemplate, s.DeserializerMethod(), s.GoType(), s.GoType(), s.unionEnumType(), switchCase, s.GoType())
}
func (s *unionField) filename() string {
return toSnake(s.GoType()) + ".go"
}
func (s *unionField) SerializerMethod() string {
return fmt.Sprintf("write%v", s.FieldType())
}
func (s *unionField) DeserializerMethod() string {
return fmt.Sprintf("read%v", s.FieldType())
}
func (s *unionField) AddStruct(p *Package) {
p.addStruct(s.filename(), s.unionEnumType(), s.unionEnumDef())
p.addStruct(s.filename(), s.FieldType(), s.unionTypeDef())
for _, f := range s.itemType {
f.AddStruct(p)
}
}
func (s *unionField) AddSerializer(p *Package) {
p.addImport(UTIL_FILE, "fmt")
p.addFunction(UTIL_FILE, "", s.SerializerMethod(), s.unionSerializer())
p.addStruct(UTIL_FILE, "ByteWriter", byteWriterInterface)
p.addFunction(UTIL_FILE, "", "writeLong", writeLongMethod)
p.addFunction(UTIL_FILE, "", "encodeInt", encodeIntMethod)
p.addImport(UTIL_FILE, "io")
for _, f := range s.itemType {
f.AddSerializer(p)
}
}
func (s *unionField) AddDeserializer(p *Package) {
p.addImport(UTIL_FILE, "fmt")
p.addFunction(UTIL_FILE, "", s.DeserializerMethod(), s.unionDeserializer())
p.addFunction(UTIL_FILE, "", "readLong", readLongMethod)
p.addImport(UTIL_FILE, "io")
for _, f := range s.itemType {
f.AddDeserializer(p)
}
}