A fast Go avro codec
Install with:
go get github.com/hamba/avro
type SimpleRecord struct {
A int64 `avro:"a"`
B string `avro:"b"`
}
schema, err := avro.Parse(`{
"type": "record",
"name": "simple",
"namespace": "org.hamba.avro",
"fields" : [
{"name": "a", "type": "long"},
{"name": "b", "type": "string"}
]
}`)
if err != nil {
log.Fatal(err)
}
in := SimpleRecord{A: 27, B: "foo"}
data, err := avro.Marshal(schema, in)
if err != nil {
log.Fatal(err)
}
fmt.Println(data)
// Outputs: [54 6 102 111 111]
out := SimpleRecord{}
err = avro.Unmarshal(schema, data, &out)
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
// Outputs: {27 foo}
Avro | Go Struct | Go Interface |
---|---|---|
null |
nil |
nil |
boolean |
bool |
bool |
bytes |
[]byte |
[]byte |
float |
float32 |
float32 |
double |
float64 |
float64 |
long |
int64 |
int64 |
int |
int , int32 , int16 , int8 |
int |
string |
string |
string |
array |
[]T |
[]interface{} |
enum |
string |
string |
fixed |
[n]byte |
[]byte |
map |
map[string]T{} |
map[string]interface{} |
record |
struct |
map[string]interface{} |
union |
see below | see below |
In Go structs, the following types are accepted: map[string]interface{}
, *T
,
and a struct
implementing avro.UnionType
. When en/decoding to an interface{}
, a
map[string]interface{}
will always be used.
- map[string]interface{}: If the union value is
nil
, anil
map will be en/decoded. When a non-nil
union value is encountered, a single key is en/decoded. The key is the avro type name, or scheam full name in the case of a named schema (enum, fixed or record). - *T: This is allowed in a "nullable" union. A nullable union is defined as a two schema union,
with the first being
null
(ie.["null", "string"]
), in this case a*T
is allowed, withT
matching the conversion table above. - avro.UnionType: A
struct
in implementingavro.UnionType
can be provided, allowing for strong type encoding. An example can be found in the godoc.
- Improve test coverage, docs and examples
- Logical Types
- Schema registry
- Schema
- Refactor parsing to be cleaner
- Better schema validation
- Aliases
- Avro Textual Form?