Skip to content

Instantly share code, notes, and snippets.

@karrick
Created September 25, 2015 20:09
Show Gist options
  • Save karrick/d06a228d2704efc49e8c to your computer and use it in GitHub Desktop.
Save karrick/d06a228d2704efc49e8c to your computer and use it in GitHub Desktop.
This should work, but does not
package main
import (
"bytes"
"log"
"github.com/linkedin/goavro"
)
var (
schema = `
{
"name" : "event",
"type" : "record",
"namespace" : "mobi.bigtree.avro",
"fields" : [
{ "type": "string", "name" : "string" },
{ "type": ["null", "string"], "name" : "nil_or_string" }
]
}
`
)
func main() {
codec, err := goavro.NewCodec(schema)
if err != nil {
log.Fatalln("Failed to create codec.", err)
}
record, err := goavro.NewRecord(goavro.RecordSchema(schema))
if err != nil {
log.Fatalln("Failed to create record.", err)
}
record.Set("string", "this field cannnot be nil.")
record.Set("nil_or_string", nil) // or a string
buf := new(bytes.Buffer)
if err = codec.Encode(buf, record); err != nil {
log.Fatalln("Failed to encode.", err)
}
decode, err := codec.Decode(buf)
if err != nil {
log.Fatalln("Failed to decode.", err)
}
log.Println(decode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment