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

Feature: Implement data type comparison #161

Open
alencarandre opened this issue Aug 9, 2019 · 4 comments
Open

Feature: Implement data type comparison #161

alencarandre opened this issue Aug 9, 2019 · 4 comments

Comments

@alencarandre
Copy link

alencarandre commented Aug 9, 2019

In order to compare between data types, I am doing

type (
  CustomInterface interface {
    Foo() string
  }

  CustomType1 struct { }
  CustomType2 struct { }
)

func (ct1 CustomType1) Foo() {}
func (ct2 CustomType2) Foo() {}

...

func TestType(t testing.T) {
   var i interface{} = &CustomType1{}
 
   _, isType1 := i.(CustomType1)
   _, isType2 := i.(CustomType1)

   assert.Assert(t, isType1) // Success
   assert.Assert(t, isType2) // Fail
}

It would be better if

customType := &CustomType1{}

assert.Assert(t, is.SameType(customType, CustomType1) // Success
assert.Assert(t, is.SameType(customType, CustomType2) // Fail
@alencarandre alencarandre changed the title Feature: Implement data type comparation Feature: Implement data type comparison Aug 9, 2019
@dnephin
Copy link
Member

dnephin commented Aug 12, 2019

A little while ago I considered adding something like Implements or Type. When I was experimenting with it I realized that most of the time if I cared about the type I probably wanted to use the type afterward.

Ex:

asCustomType, isType1 := i.(CustomType1)
assert.Assert(t, isType1)

asCustomType.Foo()

Pure type assertions didn't seem very useful because the compiler will already check the type for you. Do you have a more concrete example of where you would want to use this comparison but not use the type afterward?

@alencarandre
Copy link
Author

alencarandre commented Aug 13, 2019

Well, I implemented an interface and I am testing if the returned driver is correct type:

type (
  DataSource struct {
    Adapter string
  }

  DriverInterface interface {
    BlockList() []string
  }

  YamlDriver struct { }
  XmlDriver struct { }
)

func (yamlDriver *YamlDriver) BlockList() []string {
  ...
}

func (xmlDriver *XmlDriver) BlockList() []string {
  ...
}

func BuildDriver(DataSource ds) {
  ...
}
func TestBuildDriverYaml(t *testing.T) {
  var ds = NewDataSource()
  ds.Adapter = "yaml"

  var i interface{} = BuildDriver(ds)
  _, isYamlDriver := i.(*YamlDriver)

  assert.Assert(t, isYamlDriver)
}

@dnephin
Copy link
Member

dnephin commented Aug 13, 2019

You can have the compiler perform that test for you by adding this line to a package:

var _ DriverInterface = &YamlDriver{}

I believe that's the recommended practice for checking a type implements an interface.

@dnephin
Copy link
Member

dnephin commented May 1, 2020

A better example from #147

doer, ok := something.(Doer)
assert.Assert(t, ok, "got: %T", something)
var _ DriverInterface = (*YamlDriver)(nil)

@dnephin dnephin added this to the next up milestone May 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants