Go library for working with Avro schema and data.
Build a record-based Avro schema.
sexEnum := &avro.Enum{
Name: "sex",
Symbols: []string{
"Male",
"Female",
"Unknown",
},
}
participantSchema := &avro.Record{
Name: "participant",
Fields: []*avro.Field{
{
Name: "id",
Type: String,
},
{
Name: "birth_date",
Type: avro.Union{
avro.Null,
avro.Date,
},
},
{
Name: "sex",
Type: avro.Union{
avro.Null,
sexEnum,
},
},
},
}
Marshal schema to JSON-encoded representation.
bytes, err := avro.Marshal(participantSchema)
Unmarshal encoded schema into its native type.
var participantSchema Record
avro.UnmarshalSchema(bytes, &participantSchema)
Integration with the LinkedIn GoAvro library
// SchemaToCodec returns a new goavro.Codec from the provided avro.Schema.
func SchemaToCodec(s avro.Schema) (*goavro.Codec, error) {
b, err := avro.Marshal(s)
if err != nil {
return nil, err
}
return goavro.NewCodec(string(b))
}
// CodecToSchema derives an avro.Schema from the provided goavro.Codec.
func CodecToSchema(c *goavro.Codec) (avro.Schema, error) {
return avro.Unmarshal([]byte(c.Schema()))
}