Skip to content

Commit d517b19

Browse files
Merge pull request #285 from mittal-aashay/master
feat(goavro) Adding method to return typename associated with Codec
2 parents 8eb9f0e + 3cdeb7c commit d517b19

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

codec.go

+6
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,12 @@ func (c *Codec) SchemaCRC64Avro() int64 {
604604
return int64(c.Rabin)
605605
}
606606

607+
// TypeName returns the name of the type described by the
608+
// schema used to create the Codec.
609+
func (c *Codec) TypeName() name {
610+
return *c.typeName
611+
}
612+
607613
// convert a schema data structure to a codec, prefixing with specified
608614
// namespace
609615
func buildCodec(st map[string]*Codec, enclosingNamespace string, schema interface{}, cb *codecBuilder) (*Codec, error) {

codec_test.go

+81
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,87 @@ func TestCodecRabin(t *testing.T) {
145145
}
146146
}
147147

148+
func TestTypeName(t *testing.T) {
149+
cases := []struct {
150+
Schema string
151+
expectedFullName string
152+
expectedNamespace string
153+
}{
154+
{
155+
Schema: `"null"`,
156+
expectedFullName: "null",
157+
expectedNamespace: "",
158+
},
159+
{
160+
Schema: `"boolean"`,
161+
expectedFullName: "boolean",
162+
expectedNamespace: "",
163+
},
164+
{
165+
Schema: `"int"`,
166+
expectedFullName: "int",
167+
expectedNamespace: "",
168+
},
169+
{
170+
Schema: `"long"`,
171+
expectedFullName: "long",
172+
expectedNamespace: "",
173+
},
174+
{
175+
Schema: `"float"`,
176+
expectedFullName: "float",
177+
expectedNamespace: "",
178+
},
179+
{
180+
Schema: `[ "int" ]`,
181+
expectedFullName: "union",
182+
expectedNamespace: "",
183+
},
184+
{
185+
Schema: `[ "int" , {"type":"boolean"} ]`,
186+
expectedFullName: "union",
187+
expectedNamespace: "",
188+
},
189+
{
190+
Schema: `{"fields":[], "type":"record", "name":"foo"}`,
191+
expectedFullName: "foo",
192+
expectedNamespace: "",
193+
},
194+
{
195+
Schema: `{"type":"enum", "name":"foo", "symbols":["A1"]}`,
196+
expectedFullName: "foo",
197+
expectedNamespace: "",
198+
},
199+
{
200+
Schema: `{"name":"foo","type":"fixed","size":15}`,
201+
expectedFullName: "foo",
202+
expectedNamespace: "",
203+
},
204+
{
205+
Schema: `{"fields":[], "type":"record", "name":"foo", "namespace":"x.y"}`,
206+
expectedFullName: "foo",
207+
expectedNamespace: "x.y",
208+
},
209+
{
210+
Schema: `{"namespace":"x.y.z", "type":"enum", "name":"foo", "doc":"foo bar", "symbols":["A1", "A2"]}`,
211+
expectedFullName: "foo",
212+
expectedNamespace: "x.y.z",
213+
},
214+
}
215+
216+
for _, c := range cases {
217+
codec, err := NewCodec(c.Schema)
218+
if err != nil {
219+
t.Fatalf("CASE: %s; cannot create codec: %s", c.Schema, err)
220+
}
221+
typeName := codec.TypeName()
222+
expected, _ := newName(c.expectedFullName, c.expectedNamespace, "")
223+
if typeName != *expected {
224+
t.Errorf("CASE: %s; GOT: %s; WANT: %s", c.Schema, codec.TypeName(), *expected)
225+
}
226+
}
227+
}
228+
148229
func TestSingleObjectEncoding(t *testing.T) {
149230
t.Run("int", func(*testing.T) {
150231
schema := `"int"`

name.go

+6
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,9 @@ func (n *name) short() string {
141141
}
142142
return n.fullName
143143
}
144+
145+
// Shortname returns the name without the prefixed namespace.
146+
// This uses the short method underneath but is visible outside the package.
147+
func (n *name) ShortName() string {
148+
return n.short()
149+
}

name_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package goavro
1212
// NOTE: part of goavro package because it tests private functionality
1313

1414
import (
15+
"fmt"
1516
"testing"
1617
)
1718

@@ -98,3 +99,26 @@ func TestNewNameFromSchemaMap(t *testing.T) {
9899
t.Errorf("GOT: %q; WANT: %q", got, want)
99100
}
100101
}
102+
103+
func TestShortName(t *testing.T) {
104+
cases := []struct {
105+
name string
106+
namespace string
107+
want string
108+
}{
109+
{"bar", "", "bar"},
110+
{"foo", "org.bar", "foo"},
111+
{"bar.foo", "org", "foo"},
112+
}
113+
114+
for _, c := range cases {
115+
n, err := newName(c.name, c.namespace, nullNamespace)
116+
if err != nil {
117+
t.Fatal(err)
118+
}
119+
fmt.Printf("n: %#v fullName: %v shortName: %v\n", n, n.String(), n.ShortName())
120+
if actual, expected := n.ShortName(), c.want; actual != expected {
121+
t.Errorf("GOT: %#v; WANT: %#v", actual, expected)
122+
}
123+
}
124+
}

0 commit comments

Comments
 (0)