Created
September 25, 2015 05:00
-
-
Save lixin9311/6e117363d1b709b8411e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/linkedin/goavro" | |
"log" | |
"bytes" | |
) | |
var ( | |
schema = ` | |
{ | |
"name" : "event", | |
"type" : "record", | |
"namespace" : "mobi.bigtree.avro", | |
"fields" : [ | |
{ "type": "string", "name" : "string" }, | |
{ "type": ["string", "null"], "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 just leave it unset(then it should be nil) | |
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