diff --git a/codec.go b/codec.go index ee5bda1..b3a0cc2 100644 --- a/codec.go +++ b/codec.go @@ -604,6 +604,14 @@ func (c *Codec) SchemaCRC64Avro() int64 { return int64(c.Rabin) } +// Name returns the name of the Avro schema used to create the Codec. +func (c *Codec) Name() (string, error) { + if c.typeName == nil { + return "", fmt.Errorf("codec has no name") + } + return c.typeName.String(), nil +} + // convert a schema data structure to a codec, prefixing with specified // namespace func buildCodec(st map[string]*Codec, enclosingNamespace string, schema interface{}, cb *codecBuilder) (*Codec, error) { diff --git a/codec_test.go b/codec_test.go index f2efe19..b213b52 100644 --- a/codec_test.go +++ b/codec_test.go @@ -27,6 +27,22 @@ func ExampleCodec_CanonicalSchema() { // Output: {"type":"map","values":{"name":"foo","type":"enum","symbols":["alpha","bravo"]}} } +func ExampleCodec_Name() { + schema := `{"type":"record","name":"name","namespace":"com.domain","fields":[{"name":"key","type":{"type":"string"}}]}` + codec, err := NewCodec(schema) + if err != nil { + fmt.Println(err) + } else { + name, errName := codec.Name() + if errName != nil { + fmt.Println(errName) + } else { + fmt.Println(name) + } + } + // Output: com.domain.name +} + func TestCodecRabin(t *testing.T) { cases := []struct { Schema string