-
Notifications
You must be signed in to change notification settings - Fork 83
/
union_null_int.go
145 lines (118 loc) · 3.45 KB
/
union_null_int.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
137
138
139
140
141
142
143
144
145
// Code generated by github.com/actgardner/gogen-avro/v8. DO NOT EDIT.
/*
* SOURCE:
* schema.avsc
*/
package avro
import (
"encoding/json"
"fmt"
"io"
"github.com/actgardner/gogen-avro/v9/compiler"
"github.com/actgardner/gogen-avro/v9/vm"
"github.com/actgardner/gogen-avro/v9/vm/types"
)
type UnionNullIntTypeEnum int
const (
UnionNullIntTypeEnumInt UnionNullIntTypeEnum = 1
)
type UnionNullInt struct {
Null *types.NullVal
Int int32
UnionType UnionNullIntTypeEnum
}
func writeUnionNullInt(r *UnionNullInt, w io.Writer) error {
if r == nil {
err := vm.WriteLong(0, w)
return err
}
err := vm.WriteLong(int64(r.UnionType), w)
if err != nil {
return err
}
switch r.UnionType {
case UnionNullIntTypeEnumInt:
return vm.WriteInt(r.Int, w)
}
return fmt.Errorf("invalid value for *UnionNullInt")
}
func NewUnionNullInt() *UnionNullInt {
return &UnionNullInt{}
}
func (r *UnionNullInt) Serialize(w io.Writer) error {
return writeUnionNullInt(r, w)
}
func DeserializeUnionNullInt(r io.Reader) (*UnionNullInt, error) {
t := NewUnionNullInt()
deser, err := compiler.CompileSchemaBytes([]byte(t.Schema()), []byte(t.Schema()))
if err != nil {
return t, err
}
err = vm.Eval(r, deser, t)
if err != nil {
return t, err
}
return t, err
}
func DeserializeUnionNullIntFromSchema(r io.Reader, schema string) (*UnionNullInt, error) {
t := NewUnionNullInt()
deser, err := compiler.CompileSchemaBytes([]byte(schema), []byte(t.Schema()))
if err != nil {
return t, err
}
err = vm.Eval(r, deser, t)
if err != nil {
return t, err
}
return t, err
}
func (r *UnionNullInt) Schema() string {
return "[\"null\",\"int\"]"
}
func (_ *UnionNullInt) SetBoolean(v bool) { panic("Unsupported operation") }
func (_ *UnionNullInt) SetInt(v int32) { panic("Unsupported operation") }
func (_ *UnionNullInt) SetFloat(v float32) { panic("Unsupported operation") }
func (_ *UnionNullInt) SetDouble(v float64) { panic("Unsupported operation") }
func (_ *UnionNullInt) SetBytes(v []byte) { panic("Unsupported operation") }
func (_ *UnionNullInt) SetString(v string) { panic("Unsupported operation") }
func (r *UnionNullInt) SetLong(v int64) {
r.UnionType = (UnionNullIntTypeEnum)(v)
}
func (r *UnionNullInt) Get(i int) types.Field {
switch i {
case 0:
return r.Null
case 1:
return &types.Int{Target: (&r.Int)}
}
panic("Unknown field index")
}
func (_ *UnionNullInt) NullField(i int) { panic("Unsupported operation") }
func (_ *UnionNullInt) SetDefault(i int) { panic("Unsupported operation") }
func (_ *UnionNullInt) AppendMap(key string) types.Field { panic("Unsupported operation") }
func (_ *UnionNullInt) AppendArray() types.Field { panic("Unsupported operation") }
func (_ *UnionNullInt) Finalize() {}
func (r *UnionNullInt) MarshalJSON() ([]byte, error) {
if r == nil {
return []byte("null"), nil
}
switch r.UnionType {
case UnionNullIntTypeEnumInt:
return json.Marshal(map[string]interface{}{"int": r.Int})
}
return nil, fmt.Errorf("invalid value for *UnionNullInt")
}
func (r *UnionNullInt) UnmarshalJSON(data []byte) error {
var fields map[string]json.RawMessage
if err := json.Unmarshal(data, &fields); err != nil {
return err
}
if len(fields) > 1 {
return fmt.Errorf("more than one type supplied for union")
}
if value, ok := fields["int"]; ok {
r.UnionType = 1
return json.Unmarshal([]byte(value), &r.Int)
}
return fmt.Errorf("invalid value for *UnionNullInt")
}