This repository has been archived by the owner on Feb 12, 2020. It is now read-only.
forked from actgardner/gogen-avro
/
bool.go
95 lines (80 loc) · 1.63 KB
/
bool.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
package generator
const byteWriterInterface = `
type ByteWriter interface {
Grow(int)
WriteByte(byte) error
}
`
const byteReaderInterface = `
type ByteReader interface {
ReadByte() (byte, error)
}
`
const writeBoolMethod = `
func writeBool(r bool, w io.Writer) error {
var b byte
if r {
b = byte(1)
}
var err error
if bw, ok := w.(ByteWriter); ok {
err = bw.WriteByte(b)
} else {
bb := make([]byte, 1)
bb[0] = b
_, err = w.Write(bb)
}
if err != nil {
return err
}
return nil
}
`
const readBoolMethod = `
func readBool(r io.Reader) (bool, error) {
var b byte
var err error
if br, ok := r.(ByteReader); ok {
b, err = br.ReadByte()
} else {
bs := make([]byte, 1)
_, err = io.ReadFull(r, bs)
if err != nil {
return false, err
}
b = bs[0]
}
return b == 1, nil
}
`
type boolField struct {
name string
defaultValue bool
hasDefault bool
}
func (s *boolField) Name() string {
return toPublicName(s.name)
}
func (s *boolField) FieldType() string {
return "Bool"
}
func (s *boolField) GoType() string {
return "bool"
}
func (s *boolField) SerializerMethod() string {
return "writeBool"
}
func (s *boolField) DeserializerMethod() string {
return "readBool"
}
func (s *boolField) AddStruct(*Package) {}
func (s *boolField) AddSerializer(p *Package) {
p.addStruct(UTIL_FILE, "ByteWriter", byteWriterInterface)
p.addFunction(UTIL_FILE, "", "writeBool", writeBoolMethod)
p.addImport(UTIL_FILE, "io")
}
func (s *boolField) AddDeserializer(p *Package) {
p.addStruct(UTIL_FILE, "ByteReader", byteReaderInterface)
p.addFunction(UTIL_FILE, "", "readBool", readBoolMethod)
p.addImport(UTIL_FILE, "io")
}