Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose codec full name #281

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions codec.go
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions codec_test.go
Expand Up @@ -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
Expand Down