From c15c7d4d6b9917c41c024f38ac1185a4e3a6a86c Mon Sep 17 00:00:00 2001 From: Gustavo Okuyama Date: Mon, 6 Nov 2023 23:39:01 -0300 Subject: [PATCH 1/2] feat: expose codec full name --- codec.go | 8 ++++++++ codec_test.go | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) 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..72ebe9d 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":"map","values":{"type":"enum","name":"foo","symbols":["alpha","bravo"]}}` + 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: map +} + func TestCodecRabin(t *testing.T) { cases := []struct { Schema string From 06c7a84c5a1075ccc14f2946e5e482cebed3d5bc Mon Sep 17 00:00:00 2001 From: Gustavo Okuyama Date: Tue, 7 Nov 2023 00:51:25 -0300 Subject: [PATCH 2/2] test: improve codec name test --- codec_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codec_test.go b/codec_test.go index 72ebe9d..b213b52 100644 --- a/codec_test.go +++ b/codec_test.go @@ -28,7 +28,7 @@ func ExampleCodec_CanonicalSchema() { } func ExampleCodec_Name() { - schema := `{"type":"map","values":{"type":"enum","name":"foo","symbols":["alpha","bravo"]}}` + schema := `{"type":"record","name":"name","namespace":"com.domain","fields":[{"name":"key","type":{"type":"string"}}]}` codec, err := NewCodec(schema) if err != nil { fmt.Println(err) @@ -40,7 +40,7 @@ func ExampleCodec_Name() { fmt.Println(name) } } - // Output: map + // Output: com.domain.name } func TestCodecRabin(t *testing.T) {