-
Notifications
You must be signed in to change notification settings - Fork 22
/
doc.go
66 lines (51 loc) · 1.7 KB
/
doc.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 avro encodes/decodes avro schemas to your struct or a map.
Overview
Go-avro parses .avsc schemas from files and then lets you work with them.
schema, err := avro.ParseSchemaFile("person.avsc")
// important: handle err!
Struct Mapping
When using SpecificDecoder, the implementation uses struct tags to map avro
messages into your struct. This helps because it makes schema evolution
easier and avoids having a ton of casting and dictionary checks in your
user code.
Say you had the schema:
{
"type": "record",
"name": "Person",
"fields" : [
{"name": "id", "type": "int"},
{"name": "name", "type": "string"}
{"name": "location", "type": {
"type": "record",
"name": "Location",
"fields": [
{"name": "latitude", "type": "double"},
{"name": "longitude", "type": "double"}
]
}}
]
}
This could be mapped to a SpecificRecord by using structs:
type Person struct {
Id int32 `avro:"id"`
Name string `avro:"name"`
Location *Location `avro:"location"`
}
type Location struct {
Latitude float64
Longitude float64
}
If the `avro:` struct tag is omitted, the default mapping lower-cases the first
letter only. It's better to just explicitly define where possible.
Mapped types:
- avro 'int' is always 32-bit, so maps to golang 'int32'
- avro 'long' is always mapped to 'int64'
- avro 'float' -> float32
- avro 'double' -> 'float64'
- most other ones are obvious
Type unions are a bit more tricky. For a complex type union, the only valid
mapping is interface{}. However, for a type union with only "null" and one
other type (very typical) you can map it as a pointer type and keep type safety.
*/
package avro