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

Handle nils in filesystem #50

Merged
merged 2 commits into from
Apr 28, 2016
Merged

Handle nils in filesystem #50

merged 2 commits into from
Apr 28, 2016

Conversation

voidspace
Copy link

coerced, err := checker.Coerce(source, nil)
if err != nil {
return nil, WrapWithDeserializationError(err, "filesystem 2.0 schema check failed")
}
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, ok := valid["mount_point"].(string)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for the if !ok check, as when not present mount_point will be "".

mount_point, _ := valid["mount_point"].(string)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dimitern This won't work if they're present but null in the JSON.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duh, misunderstood what you're saying, sorry.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works, but it can be misleading at first glance:

value, ok := someMapStringString["missing-key"] -> value = "", ok = false
value, _ := someMapStringString["missing-key"] -> value = "", no panic
value := someMapStringString["missing-key"] -> value = "", no panic

On the other hand:
value, ok := someNonStringInterfaceValue.(string) -> value = "", ok = false
value, _ := someNonStringInterfaceValue.(string) -> value = "", no panic
value := someNonStringInterfaceValue.(string) -> panics

We have both cases in one, so:
value, _ := mapStringInterface["missing-key"].(string) -> value = "" (i.e. we got a non-nil interface{} value with a wrapped nil in it due to the map lookup, but then trying to type assert that value to a string still returns "" and will panic without _ on the second return)

@frobware
Copy link

LGTM

if !ok {
mount_point = ""
}
label, ok := valid["label"].(string)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: label, _ := valid["label"].(string) is enough.

@dimitern
Copy link

LGTM with a couple of suggestions for simplification.

@@ -39,24 +39,36 @@ 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()),
Copy link
Contributor

@babbageclunk babbageclunk Apr 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need the schema.OneOfs here if you're putting in defaults below? Or is it that they're sometimes null in the JSON?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed if the value is nil it fails the schema check - even with a default. The default handles it being missing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the defaults are applied only after coercing the json blob to the field map.

@babbageclunk
Copy link
Contributor

LGTM

@voidspace
Copy link
Author

$$merge$$

@jujubot
Copy link
Contributor

jujubot commented Apr 28, 2016

Status: merge request accepted. Url: http://juju-ci.vapour.ws:8080/job/github-merge-juju-gomaasapi

@jujubot jujubot merged commit b5d5d8e into juju:master Apr 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants