forked from actgardner/gogen-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_schema.go
executable file
·115 lines (97 loc) · 3.14 KB
/
demo_schema.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
// Code generated by github.com/actgardner/gogen-avro. DO NOT EDIT.
/*
* SOURCE:
* example.avsc
*/
package avro
import (
"io"
"github.com/actgardner/gogen-avro/compiler"
"github.com/actgardner/gogen-avro/container"
"github.com/actgardner/gogen-avro/vm"
"github.com/actgardner/gogen-avro/vm/types"
)
type DemoSchema struct {
IntField int32
DoubleField float64
StringField string
BoolField bool
BytesField []byte
}
func NewDemoSchemaWriter(writer io.Writer, codec container.Codec, recordsPerBlock int64) (*container.Writer, error) {
str := &DemoSchema{}
return container.NewWriter(writer, codec, recordsPerBlock, str.Schema())
}
func DeserializeDemoSchema(r io.Reader) (*DemoSchema, error) {
t := NewDemoSchema()
deser, err := compiler.CompileSchemaBytes([]byte(t.Schema()), []byte(t.Schema()))
if err != nil {
return nil, err
}
err = vm.Eval(r, deser, t)
return t, err
}
func NewDemoSchema() *DemoSchema {
return &DemoSchema{}
}
func (r *DemoSchema) Schema() string {
return "{\"fields\":[{\"name\":\"IntField\",\"type\":\"int\"},{\"name\":\"DoubleField\",\"type\":\"double\"},{\"name\":\"StringField\",\"type\":\"string\"},{\"name\":\"BoolField\",\"type\":\"boolean\"},{\"name\":\"BytesField\",\"type\":\"bytes\"}],\"name\":\"DemoSchema\",\"type\":\"record\"}"
}
func (r *DemoSchema) Serialize(w io.Writer) error {
return writeDemoSchema(r, w)
}
func (_ *DemoSchema) SetBoolean(v bool) { panic("Unsupported operation") }
func (_ *DemoSchema) SetInt(v int32) { panic("Unsupported operation") }
func (_ *DemoSchema) SetLong(v int64) { panic("Unsupported operation") }
func (_ *DemoSchema) SetFloat(v float32) { panic("Unsupported operation") }
func (_ *DemoSchema) SetDouble(v float64) { panic("Unsupported operation") }
func (_ *DemoSchema) SetBytes(v []byte) { panic("Unsupported operation") }
func (_ *DemoSchema) SetString(v string) { panic("Unsupported operation") }
func (_ *DemoSchema) SetUnionElem(v int64) { panic("Unsupported operation") }
func (r *DemoSchema) Get(i int) types.Field {
switch i {
case 0:
return (*types.Int)(&r.IntField)
break
case 1:
return (*types.Double)(&r.DoubleField)
break
case 2:
return (*types.String)(&r.StringField)
break
case 3:
return (*types.Boolean)(&r.BoolField)
break
case 4:
return (*types.Bytes)(&r.BytesField)
break
}
panic("Unknown field index")
}
func (_ *DemoSchema) AppendMap(key string) types.Field { panic("Unsupported operation") }
func (_ *DemoSchema) AppendArray() types.Field { panic("Unsupported operation") }
func (_ *DemoSchema) Finalize() {}
type DemoSchemaReader struct {
r io.Reader
p *vm.Program
}
func NewDemoSchemaReader(r io.Reader) (*DemoSchemaReader, error) {
containerReader, err := container.NewReader(r)
if err != nil {
return nil, err
}
t := NewDemoSchema()
deser, err := compiler.CompileSchemaBytes([]byte(containerReader.AvroContainerSchema()), []byte(t.Schema()))
if err != nil {
return nil, err
}
return &DemoSchemaReader{
r: containerReader,
p: deser,
}, nil
}
func (r *DemoSchemaReader) Read() (*DemoSchema, error) {
t := NewDemoSchema()
err := vm.Eval(r.r, r.p, t)
return t, err
}