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(goavro) Adding method to return typename associated with Codec #285

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
81 changes: 81 additions & 0 deletions codec_test.go
Original file line number Diff line number Diff line change
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
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()
}
24 changes: 24 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,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)
}
}
}
Loading