/
bytes.go
66 lines (55 loc) · 1.37 KB
/
bytes.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
package generator
const writeBytesMethod = `
func writeBytes(r []byte, w io.Writer) error {
err := writeLong(int64(len(r)), w)
if err != nil {
return err
}
_, err = w.Write(r)
return err
}
`
const readBytesMethod = `
func readBytes(r io.Reader) ([]byte, error) {
size, err := readLong(r)
if err != nil {
return nil, err
}
bb := make([]byte, size)
_, err = io.ReadFull(r, bb)
return bb, err
}
`
type bytesField struct {
name string
defaultValue []byte
hasDefault bool
}
func (s *bytesField) Name() string {
return toPublicName(s.name)
}
func (s *bytesField) FieldType() string {
return "Bytes"
}
func (s *bytesField) GoType() string {
return "[]byte"
}
func (s *bytesField) SerializerMethod() string {
return "writeBytes"
}
func (s *bytesField) DeserializerMethod() string {
return "readBytes"
}
func (s *bytesField) AddStruct(*Package) {}
func (s *bytesField) AddSerializer(p *Package) {
p.addStruct(UTIL_FILE, "ByteWriter", byteWriterInterface)
p.addFunction(UTIL_FILE, "", "writeBytes", writeBytesMethod)
p.addFunction(UTIL_FILE, "", "writeLong", writeLongMethod)
p.addFunction(UTIL_FILE, "", "encodeInt", encodeIntMethod)
p.addImport(UTIL_FILE, "io")
}
func (s *bytesField) AddDeserializer(p *Package) {
p.addFunction(UTIL_FILE, "", "readBytes", readBytesMethod)
p.addFunction(UTIL_FILE, "", "readLong", readLongMethod)
p.addImport(UTIL_FILE, "io")
}