State interfaces

Nate Finch nate.finch at canonical.com
Thu Oct 31 19:49:31 UTC 2013


Preferably, if you find yourself needing an interface that only has A, make
an interface that only has A, and have your function use that interface. :)

But that is a neat trick for when the above isn't possible for whatever
reason.


On Thu, Oct 31, 2013 at 3:43 PM, Tim Penhey <tim.penhey at canonical.com>wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 01/11/13 01:57, John Arbash Meinel wrote:
> > On 2013-10-31 16:21, Andrew Wilkins wrote:
> >> Does anybody object to converting the public entity structs in
> >> state to interfaces? i.e. State, Machine, Unit, etc.
> >
> >> I'd like to write some tests for new code that depends on state.
> >>  Setting up mongo and so on is overkill. and should be relegated
> >> to integration testing.
> >
> > I would say that if you want an interface you probably don't need
> > the whole set of functions that Unit/Machine/etc provide. And a
> > smaller interface means you don't have to create a fake one that
> > has a bunch of methods you don't actually use.
> >
> > Something to think about, at least.
> >
> > I personally like having interfaces, but I'm not sure that all of
> > Unit/Machine/etc need to be turned into an interface. There are
> > already a few that we have (like Entity, etc).
>
> I would also like to see interfaces used more.  However I don't like
> huge interfaces.  Consider the interface segregation principle [1]
>
> While I'm complaining, please don't append "er" to the end of all the
> interfaces, this is kinda horrible.
>
> As a side note, this is a trick that I have been using in tests when
> mocking interfaces.
>
> package foo
>
> type SomeInterface interface {
>   A()
>   B()
>   C()
> }
>
> and in the test if I only need to call A, but need something that
> supports the interface you can do this:
>
> type mockSomeInterface struct {
>   foo.SomeInterface
>   // whatever data I need in the test
> }
>
> func (*mockSomeInterface) A() {
>   // do stuff
> }
>
> Now &mockSomeInterface{} supports foo.SomeInterface, and A will work,
> but any other call to a SomeInterface method will panic will a null
> pointer dereference (or whatever it is called).
>
> Tim
>
> [1] http://en.wikipedia.org/wiki/Interface_segregation_principle
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.14 (GNU/Linux)
> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>
> iEYEARECAAYFAlJyssAACgkQd1fvI4G7WRAxNgCeKg412E0DWb+hkBr9NkLYiU0+
> KGAAnRmNxqxbPmmei9nVyMAtVYj75yq+
> =BRYr
> -----END PGP SIGNATURE-----
>
> --
> Juju-dev mailing list
> Juju-dev at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/juju-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/juju-dev/attachments/20131031/d49a8459/attachment-0001.html>


More information about the Juju-dev mailing list