Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Replace caps.Type struct with an interface #322
Conversation
niemeyer
reviewed
Jan 13, 2016
| @@ -42,8 +40,9 @@ func (s *MiscSuite) TestLoadBuiltInTypes(c *C) { | ||
| repo := NewRepository() | ||
| err := LoadBuiltInTypes(repo) | ||
| c.Assert(err, IsNil) | ||
| - c.Assert(repo.types, testutil.Contains, BoolFileType) | ||
| - c.Assert(repo.types, HasLen, 1) // Update this whenever new built-in type is added | ||
| + c.Assert(repo.types, DeepEquals, []Type{ |
niemeyer
Jan 13, 2016
Contributor
For the record, I generally try to avoid such whitebox tests. There's really not much benefit in simply knowing something is inside a given attribute. The best kind of test is those that verify that the value interface behaves as it should. In other words, what is the observable result of this repository knowing about the bool-file type? This outcome cannot change because it's a promise we're making to clients of this API, so it's a better thing to ensure in a test too. The internals can be modified as long as this promise isn't broken.
zyga
Jan 13, 2016
Contributor
Thanks, I agree about the internal-vs-contract point. I've tweaked the test to check what is really relevant -- that bool-file type is registered.
niemeyer
reviewed
Jan 13, 2016
| - // capability of this type. | ||
| - RequiredAttrs []string | ||
| +type Type interface { | ||
| + fmt.Stringer |
zyga
Jan 13, 2016
Contributor
Yeah, I think I just copied that over from older Type. I'll remove it.
niemeyer
reviewed
Jan 13, 2016
| - return json.Marshal(t.Name) | ||
| +// MockType is a type for various kind of tests. | ||
| +// It is public so that it can be consumed from other packages. | ||
| +type MockType struct { |
niemeyer
Jan 13, 2016
Contributor
s/Mock/Test/? Mock tends to imply a certain kind of behavior from the value.
|
A few comments above, but LGTM! |
zyga
added some commits
Jan 13, 2016
mvo5
reviewed
Jan 14, 2016
| + // Unique and public name of this type. | ||
| + Name() string | ||
| + // Sanitize a capability (altering if necessary). | ||
| + Sanitize(c *Capability) error |
mvo5
Jan 14, 2016
Collaborator
I'm curious, why was the name changed to Sanitize? That is something you usually do on a toilet to clean it (and in the hospital of course). I'm not a native speaker so I could be wrong and this is bike-shed territory for sure. What exactly will this function do? What does "altering if necessary" means? What kind of alteration?
zyga
Jan 14, 2016
Contributor
The name changed because the method can now modify the capability and we wanted to make it explicit. I'm open to a better name, we picked sanitize in a quick discussion.
Edit: modify == modify attributes.
pedronis
Jan 14, 2016
Contributor
@zyga @mvo5 I have seen "sanitize" used but usually it has a connotation of also making something potentially unsafe safe, (like taking html text and removing dangerous tags), is that the case here? otherwise is a strange term here
zyga
Jan 14, 2016
Contributor
@pedronis yes, we explicitly reserve the right to modify capability data or reject it entirely.
Sanitize() == Validate() + Alter()
pedronis
Jan 14, 2016
Contributor
@zyga that's the weird bit, "sanitize" implies that you almost never reject but make innocuous
mvo5
Jan 14, 2016
Collaborator
I don't know what "Alter()" will do so I don't really know what to suggest. Its just a name, if there is no better idea lets use, we can always change it later :)
|
Code looks good, just one question about the terminology. I'm not hung up on it so no blocker. I just wonder if there is something more fitting. Part of the problem is that I don't know enough about the future, i.e. what "altering the capability" means. |
zyga commentedJan 13, 2016
The capability type is now an interface. This allows each type to freely
define appropriate logic in a non-declarative way. Some small changes
related to that are the change of the Validate() method to Sanitize()
which can also explicitly mutate a capability (currently this is not
used yet).
Along with this change, caps.Capability.Type is replaced with .TypeName
which is simply a string so that capabilities can be serialized easily.
Signed-off-by: Zygmunt Krynicki zygmunt.krynicki@canonical.com