Skip to content

Commit

Permalink
Merge pull request #285 from mittal-aashay/master
Browse files Browse the repository at this point in the history
feat(goavro) Adding method to return typename associated with Codec
  • Loading branch information
mittal-aashay committed May 2, 2024
2 parents 8eb9f0e + 3cdeb7c commit d517b19
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
6 changes: 6 additions & 0 deletions codec.go
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
81 changes: 81 additions & 0 deletions codec_test.go
Expand Up @@ -145,6 +145,87 @@ func TestCodecRabin(t *testing.T) {
}
}

func TestTypeName(t *testing.T) {
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
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()
}
24 changes: 24 additions & 0 deletions name_test.go
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,26 @@ func TestNewNameFromSchemaMap(t *testing.T) {
t.Errorf("GOT: %q; WANT: %q", got, want)
}
}

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 d517b19

Please sign in to comment.