State interfaces

Tim Penhey tim.penhey at canonical.com
Thu Oct 31 19:43:01 UTC 2013


-----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-----



More information about the Juju-dev mailing list