Skip to content

Commit

Permalink
Kind is way to generic for the interface. Discriminator is less likel…
Browse files Browse the repository at this point in the history
…y to be a conflict
  • Loading branch information
Robert Weber committed Aug 14, 2019
1 parent 1093ac0 commit 7f7d9ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions decode/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type SubRecord struct {
Name string
}

func (r SubRecord) Kind() string {
func (r SubRecord) Discriminator() string {
return r.kind
}

Expand All @@ -30,7 +30,7 @@ type SubRecord2 struct {
Subs []SubRecord
}

func (r SubRecord2) Kind() string {
func (r SubRecord2) Discriminator() string {
return r.kind
}

Expand All @@ -49,7 +49,7 @@ type Record struct {
Sub decode.Decodeable
}

func (r Record) Kind() string {
func (r Record) Discriminator() string {
return r.kind
}

Expand Down Expand Up @@ -145,10 +145,10 @@ func TestDecodeNestedObject(t *testing.T) {
Convey("Decode a nested object", t, func(){
dec, err := decode.Decode(m, "kind", MyTestFactory)
So(err, ShouldBeNil)
So(dec.Kind(), ShouldEqual, "record")
So(dec.Discriminator(), ShouldEqual, "record")
rec, ok := dec.(*Record)
So(ok, ShouldBeTrue)
So(rec.Sub.Kind(), ShouldEqual, "sub_record")
So(rec.Sub.Discriminator(), ShouldEqual, "sub_record")
So(rec, ShouldResemble, &Record{kind: "record", Name: "foo", Slice: []string{"foo", "bar"}, Sub: &SubRecord{kind: "sub_record", Name: "bar"}})
})
Convey("Unmarshal a nested object, different subtype", t, func(){
Expand All @@ -170,10 +170,10 @@ func TestDecodeNestedObject(t *testing.T) {
So(err, ShouldBeNil)
dec, err := decode.UnmarshalJSON(b, "kind", MyTestFactory)
So(err, ShouldBeNil)
So(dec.Kind(), ShouldEqual, "record")
So(dec.Discriminator(), ShouldEqual, "record")
rec, ok := dec.(*Record)
So(ok, ShouldBeTrue)
So(rec.Sub.Kind(), ShouldEqual, "sub_record2")
So(rec.Sub.Discriminator(), ShouldEqual, "sub_record2")
So(rec, ShouldResemble, &Record{
kind: "record",
Name: "foo",
Expand Down Expand Up @@ -206,10 +206,10 @@ func TestDecodeNestedObject(t *testing.T) {
}
dec, err := decode.Decode(m, "kind", MyTestFactory)
So(err, ShouldBeNil)
So(dec.Kind(), ShouldEqual, "record")
So(dec.Discriminator(), ShouldEqual, "record")
rec, ok := dec.(*Record)
So(ok, ShouldBeTrue)
So(rec.Sub.Kind(), ShouldEqual, "sub_record2")
So(rec.Sub.Discriminator(), ShouldEqual, "sub_record2")
So(rec, ShouldResemble, &Record{
kind: "record",
Name: "foo",
Expand All @@ -230,10 +230,10 @@ func TestDecodeNestedObject(t *testing.T) {
So(err, ShouldBeNil)
dec, err := decode.UnmarshalJSON(b, "kind", MyTestFactory)
So(err, ShouldBeNil)
So(dec.Kind(), ShouldEqual, "record")
So(dec.Discriminator(), ShouldEqual, "record")
rec, ok := dec.(*Record)
So(ok, ShouldBeTrue)
So(rec.Sub.Kind(), ShouldEqual, "sub_record")
So(rec.Sub.Discriminator(), ShouldEqual, "sub_record")
So(rec, ShouldResemble, &Record{kind: "record", Name: "foo", Slice: []string{"foo", "bar"}, Sub: &SubRecord{kind: "sub_record", Name: "bar"}})
})
Convey("Unmarshal bad JSON", t, func(){
Expand Down
4 changes: 2 additions & 2 deletions decode/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/iancoleman/strcase"
)

// Decodeable things have a "Kind" that is the content of the encoded "discriminator"
// Decodeable things have a "discriminator" to indicate their underlying type
type Decodeable interface {
Kind() string
Discriminator() string
}

// Factory makes Decodeable things described by their kind
Expand Down

0 comments on commit 7f7d9ab

Please sign in to comment.