-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
196 lines (173 loc) · 4.06 KB
/
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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package avrogo
import (
"fmt"
"io"
"json"
"os"
)
type SchemaError struct {
msg string
}
func (e SchemaError) String() string {
return e.msg
}
type Type interface {
Read(r io.Reader) (o interface{}, err os.Error)
}
type Field struct {
name string
doc string
ftype Type
defvalue interface{}
}
type Record struct {
name string
namespace string
doc string
aliases []string
fields []Field
}
type Fixed struct {
name string
namespace string
aliases []string
size int
}
func (r Record) Id() string {
if r.namespace == "" {
return r.name
}
return r.namespace + "." + r.name
}
func (rec Record) Read(r io.Reader) (o interface{}, err os.Error) {
vals := make(map[string]interface{})
for _, f := range rec.fields {
if val, err := f.ftype.Read(r); err == nil {
vals[f.name] = val
} else {
return nil, err
}
}
return vals, nil
}
func (f Fixed) Read(r io.Reader) (interface{}, os.Error) {
b := make([]byte, f.size)
_, err := io.ReadFull(r, b)
if err != nil {
return nil, err
}
return b, nil
}
type Map struct {
values Type
}
func (m Map) Read(r io.Reader) (interface{}, os.Error) {
vals := make(map[string]interface{})
for count, err := readLong(r); count != 0; count, err = readLong(r) {
if err != nil {
return nil, err
}
if count < 0 {
if _, err := readLong(r); err != nil { // Ignore the size
return nil, err
}
count = -count
}
for ; count > 0; count-- {
if k, err := readString(r); err != nil {
return nil, err
} else if v, err := m.values.Read(r); err != nil {
return nil, err
} else {
vals[k] = v
}
}
}
return vals, nil
}
func getString(obj map[string]interface{}, name string) string {
if v, ok := obj[name]; !ok {
return ""
} else if s, ok := v.(string); ok {
return s
}
panic("errrrrr")
}
func requireString(obj map[string]interface{}, name string) string {
if v, ok := obj[name]; !ok {
panic(SchemaError{"Missing required field '" + name + "'"})
} else if s, ok := v.(string); ok {
return s
}
panic(SchemaError{name + " must be a string"})
}
func getStringArray(obj map[string]interface{}, name string) []string {
if v, ok := obj[name]; !ok {
return []string{}
} else if a, ok := v.([]string); ok {
return a
}
panic(SchemaError{name + " must be an array of string, not " + obj[name].(string)})
}
func loadField(obj map[string]interface{}) Field {
return Field{requireString(obj, "name"), getString(obj, "doc"), loadType(obj["type"]),
getString(obj, "default")}
}
func loadMap(obj map[string]interface{}) Map {
return Map{loadType(requireString(obj, "values"))}
}
func loadRecord(obj map[string]interface{}) Record {
var fields []Field
for _, v := range obj["fields"].([]interface{}) {
fields = append(fields, loadField(v.(map[string]interface{})))
}
return Record{requireString(obj, "name"), getString(obj, "namespace"), getString(obj, "doc"),
getStringArray(obj, "aliases"), fields}
}
func loadFixed(obj map[string]interface{}) Fixed {
var size, ok = obj["size"]
if !ok {
panic(SchemaError{""})
}
return Fixed{requireString(obj, "name"), getString(obj, "namespace"),
getStringArray(obj, "aliases"), size.(int)}
}
func loadType(i interface{}) Type {
switch v := i.(type) {
case string:
if p, ok := primitives[v]; ok {
return p
} else {
panic(SchemaError{"Unknown type name " + v})
}
case []interface{}:
panic(SchemaError{"Not handling unions yet!"})
case map[string]interface{}:
if t, ok := v["type"]; !ok {
panic(SchemaError{fmt.Sprintf("Type object without type name %s", v)})
} else if t == "map" {
return loadMap(v)
} else if t == "record" {
return loadRecord(v)
} else if t == "fixed" {
return loadFixed(v)
}
}
panic(SchemaError{fmt.Sprintf("Unknown type: %v %T", i, i)})
}
func LoadJsonSchema(r io.Reader) (Type, os.Error) {
var i interface{}
d := json.NewDecoder(r)
d.Decode(&i)
return LoadSchema(i)
}
func LoadSchema(i interface{}) (loaded Type, err os.Error) {
defer func() {
if e := recover(); e != nil {
loaded = nil
err = e.(SchemaError)
}
}()
loaded = loadType(i)
return
}