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

Fix decoded maps when unmarshaling fixtures from a YAML file #23

Merged
merged 2 commits into from
Oct 3, 2017

Conversation

brandur-stripe
Copy link
Contributor

As shown in #22, when we try to unmarshal fixtures from a YAML file, we
end up with a crash in the generator due to a type mismatch.

This turns out to be because of [1]. Because YAML technically supports
boolean keys, the YAML library unmarshals to maps in a way to handle
them (map[interface{}]interface{}), even if it's not suitable for most
users (the issue strongly suggests that a lot of people would like
map[string]interface{}).

Here we work around the problem by post-processing fixtures that we've
loaded via YAML by recursing through and changing all instances of
map[interface{}]interface{} to map[string]interface{}. I've added a
few basic tests, but also tested that the call reported in #22 now works
as expected.

Fixes #22.

[1] go-yaml/yaml#139

r? @tmaxwell-stripe

As shown in #22, when we try to unmarshal fixtures from a YAML file, we
end up with a crash in the generator due to a type mismatch.

This turns out to be because of [1]. Because YAML technically supports
boolean keys, the YAML library unmarshals to maps in a way to handle
them (`map[interface{}]interface{}`), even if it's not suitable for most
users (the issue strongly suggests that a lot of people would like
`map[string]interface{}`).

Here we work around the problem by post-processing fixtures that we've
loaded via YAML by recursing through and changing all instances of
`map[interface{}]interface{}` to `map[string]interface{}`. I've added a
few basic tests, but also tested that the call reported in #22 now works
as expected.

Fixes #22.

[1] go-yaml/yaml#139
context, schema, example.value))
panic(fmt.Sprintf(
"%sSchema is an object:\n%s\n\nBut example is (type: %v):\n%s",
context, schema, reflect.TypeOf(example.value), example.value))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also added a type hint here to help with debugging in the future (it's how I found this problem).

mapstr.go Outdated
func stringifyKeysInterfaceMap(in map[interface{}]interface{}) map[string]interface{} {
res := make(map[string]interface{})
for k, v := range in {
res[fmt.Sprintf("%v", k)] = stringifyKeysMapValue(v)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should check that the key is already a string and raise an error if it isn't. Otherwise, this can lead to some really strange behavior. Suppose the original YAML file looks like:

in: a
on: b
under: c

The YAML parser will interpret in and under as strings but it will interpret on as the boolean true. So the output of stringifyKeysMapValue will be:

map[string]interface{}{
  "in": "a",
  "true": "b",
  "under": "c",
}

which is super confusing and almost certainly not what was intended.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great point. I adapted this code from elsewhere, but yep, in our case I think it makes perfect sense to be as loud as possible about this problem because we expect it to never happen.

Patched in 24a2bb4. Thanks for taking a look.

@tmaxwell-stripe
Copy link
Contributor

Thanks for tracking down that bug! I left a comment suggesting a slightly different behavior, but otherwise it looks good.

We never expect to have a non-string key and this is an internal
library so it makes sense in our case to be as loud as possible about
this sort of problem.
@brandur-stripe brandur-stripe merged commit 30a764e into master Oct 3, 2017
@tmaxwell-stripe
Copy link
Contributor

LGTM, thanks!

@brandur-stripe brandur-stripe deleted the brandur-fix-yaml-loading branch October 3, 2017 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Panic when running with custom fixtures specified
3 participants