Skip to content

Commit

Permalink
feat(goavro) Adding method to return typename associated with Codec
Browse files Browse the repository at this point in the history
**Overview:**
In order to identify the kafka topic name from a stream of topics, we
need to identify the underlying type associated with the provided Codec
data
The `Codec.typeName` field already contains this information but is
currently package private and cannot be used outside of the package

This PR adds the `TypeName` method to `Codec` to expose the underlying
type name outside the goavro package scope
This also adds the `ShortName` method to `Name` to expose the short
name (i.e. record name excluding classpath) as a public method
  • Loading branch information
Aashay Mittal committed May 2, 2024
1 parent 8eb9f0e commit caf1f15
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
6 changes: 6 additions & 0 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,12 @@ func (c *Codec) SchemaCRC64Avro() int64 {
return int64(c.Rabin)
}

// TypeName returns the name of the type described by the
// schema used to create the Codec.
func (c *Codec) TypeName() name {
return *c.typeName
}

// 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
83 changes: 83 additions & 0 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,89 @@ func TestCodecRabin(t *testing.T) {
}
}

func TestTypeName(t* testing.T) {

Check failure on line 148 in codec_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
cases := []struct {
Schema string
expectedFullName string
expectedNamespace string
}{
{
Schema: `"null"`,
expectedFullName: "null",
expectedNamespace: "",
},
{
Schema: `"boolean"`,
expectedFullName: "boolean",
expectedNamespace: "",
},
{
Schema: `"int"`,
expectedFullName: "int",
expectedNamespace: "",
},
{
Schema: `"long"`,
expectedFullName: "long",
expectedNamespace: "",
},
{
Schema: `"float"`,
expectedFullName: "float",
expectedNamespace: "",
},
{
Schema: `[ "int" ]`,
expectedFullName: "union",
expectedNamespace: "",
},
{
Schema: `[ "int" , {"type":"boolean"} ]`,
expectedFullName: "union",
expectedNamespace: "",
},
{
Schema: `{"fields":[], "type":"record", "name":"foo"}`,
expectedFullName: "foo",
expectedNamespace: "",
},
{
Schema: `{"type":"enum", "name":"foo", "symbols":["A1"]}`,
expectedFullName: "foo",
expectedNamespace: "",
},
{
Schema: `{"name":"foo","type":"fixed","size":15}`,
expectedFullName: "foo",
expectedNamespace: "",
},
{
Schema: `{"fields":[], "type":"record", "name":"foo", "namespace":"x.y"}`,
expectedFullName: "foo",
expectedNamespace: "x.y",
},
{
Schema: `{"namespace":"x.y.z", "type":"enum", "name":"foo", "doc":"foo bar", "symbols":["A1", "A2"]}`,
expectedFullName: "foo",
expectedNamespace: "x.y.z",
},
}

for _, c := range cases {
codec, err := NewCodec(c.Schema)
if err != nil {
t.Fatalf("CASE: %s; cannot create codec: %s", c.Schema, err)
}
typeName := codec.TypeName()
expected, _ := newName(c.expectedFullName, c.expectedNamespace, "")
if typeName != *expected {
t.Errorf("CASE: %s; GOT: %s; WANT: %s", c.Schema, codec.TypeName(), *expected)
}
}
}



func TestSingleObjectEncoding(t *testing.T) {
t.Run("int", func(*testing.T) {
schema := `"int"`
Expand Down
6 changes: 6 additions & 0 deletions name.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,9 @@ func (n *name) short() string {
}
return n.fullName
}

// Shortname returns the name without the prefixed namespace.
// This uses the short method underneath but is visible outside the package.
func (n *name) ShortName() string {
return n.short()
}

Check failure on line 149 in name.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
25 changes: 25 additions & 0 deletions name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package goavro
// NOTE: part of goavro package because it tests private functionality

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -98,3 +99,27 @@ func TestNewNameFromSchemaMap(t *testing.T) {
t.Errorf("GOT: %q; WANT: %q", got, want)
}
}


Check failure on line 103 in name_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
func TestShortName(t *testing.T) {
cases := []struct {
name string
namespace string
want string
}{
{"bar", "", "bar"},
{"foo", "org.bar", "foo"},
{"bar.foo", "org", "foo"},
}

for _, c := range cases {
n, err := newName(c.name, c.namespace, nullNamespace)
if err != nil {
t.Fatal(err)
}
fmt.Printf("n: %#v fullName: %v shortName: %v\n", n, n.String(), n.ShortName())
if actual, expected := n.ShortName(), c.want; actual != expected {
t.Errorf("GOT: %#v; WANT: %#v", actual, expected)
}
}
}

0 comments on commit caf1f15

Please sign in to comment.