diff --git a/filesystem.go b/filesystem.go index b1896f8..4514e52 100644 --- a/filesystem.go +++ b/filesystem.go @@ -39,12 +39,17 @@ func (f *filesystem) UUID() string { func filesystem2_0(source map[string]interface{}) (*filesystem, error) { fields := schema.Fields{ "fstype": schema.String(), - "mount_point": schema.String(), - "label": schema.String(), + "mount_point": schema.OneOf(schema.Nil(""), schema.String()), + "label": schema.OneOf(schema.Nil(""), schema.String()), "uuid": schema.String(), - // TODO: mount_options when we know the type. + // TODO: mount_options when we know the type (note it can be + // nil). } - checker := schema.FieldMap(fields, nil) + defaults := schema.Defaults{ + "mount_point": "", + "label": "", + } + checker := schema.FieldMap(fields, defaults) coerced, err := checker.Coerce(source, nil) if err != nil { return nil, WrapWithDeserializationError(err, "filesystem 2.0 schema check failed") @@ -52,11 +57,12 @@ func filesystem2_0(source map[string]interface{}) (*filesystem, error) { valid := coerced.(map[string]interface{}) // From here we know that the map returned from the schema coercion // contains fields of the right type. - + mount_point, _ := valid["mount_point"].(string) + label, _ := valid["label"].(string) result := &filesystem{ fstype: valid["fstype"].(string), - mountPoint: valid["mount_point"].(string), - label: valid["label"].(string), + mountPoint: mount_point, + label: label, uuid: valid["uuid"].(string), } return result, nil diff --git a/filesystem_test.go b/filesystem_test.go index 6959e6c..d4db169 100644 --- a/filesystem_test.go +++ b/filesystem_test.go @@ -27,6 +27,21 @@ func (*filesystemSuite) TestParse2_0(c *gc.C) { c.Check(fs.UUID(), gc.Equals, "fake-uuid") } +func (*filesystemSuite) TestParse2_Defaults(c *gc.C) { + source := map[string]interface{}{ + "fstype": "ext4", + "mount_point": nil, + "label": nil, + "uuid": "fake-uuid", + } + fs, err := filesystem2_0(source) + c.Assert(err, jc.ErrorIsNil) + c.Check(fs.Type(), gc.Equals, "ext4") + c.Check(fs.MountPoint(), gc.Equals, "") + c.Check(fs.Label(), gc.Equals, "") + c.Check(fs.UUID(), gc.Equals, "fake-uuid") +} + func (*filesystemSuite) TestParse2_0BadSchema(c *gc.C) { source := map[string]interface{}{ "mount_point": "/",