Start supplying attr.Type to reflection.#41
Conversation
422e52c to
3c0addf
Compare
|
Actually, I'm not sure I like having to duplicate our type implementations inside the reflect package with |
Same, but the point of these tests is to test unexported functions, which we can't do if we use a I don't particularly like the copying, either, but I couldn't think of something that was less bad. |
|
Yes, good point. I suppose the only other option is to make those functions exported, which has no user impact because the reflect package is internal. |
|
This just came up because I'm having to copy my |
|
Hm, let me give it a shot and see how we like the result. |
1f156ae to
b017c7e
Compare
|
Looks good! |
|
Doing some last tweaking of comments in the reflect package to make |
|
Okay, golint wanted some pretty invasive changes, but I think we're good now if you're still happy, @kmoe |
Reflection has A Problem: We want to support using `attr.Value`s inside of structs, slices, etc. and just having the reflection package Do The Right Thing. This means the reflection package needs to be able to create them. We originally solved that in #30 by just instantiating empty values of their type, and then calling a `SetTerraformValue` method on that value. This was super annoying, because then we needed two methods for getting an `attr.Value` set to the right values: `attr.Type.ValueFromTerraform` and `attr.Value.SetTerraformValue` were basically the same thing. But whatever, it worked. Except, no it didn't. Complex types like lists and maps store their element/attribute types as properties on their structs. It's important that these be set. Only the `attr.Type` has this information, it's not passed in as part of the `tftypes.Value`. So reflect couldn't set those values, and produced broken `attr.Value`s as a result. (Their `ToTerraformValue` methods would run into trouble, because they wouldn't know what `tftypes.Type` to use for elements or attributes). To solve this problem, we decided to supply the `attr.Type` from the schema to the reflect package, wiring it through so that we could instantiate new `attr.Value`s when the opportunity presented itself. This solves our problem, because we got rid of the `attr.Value.SetTerraformValue` method and used the `attr.Type.ValueFromTerraform` directly to just instantiate a new value, which made sure it was set up correctly. But now we have a new problem: what if the `attr.Type` in the schema doesn't produce the `attr.Value` they're trying to assign to? We decided to just throw an error on that one, because there's no reasonable way around it. Depends on #44.
3a8147a to
0fe3b76
Compare
|
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. |
Reflection has A Problem:
We want to support using
attr.Values inside of structs, slices, etc. and just having the reflection package Do The Right Thing. This means the reflection package needs to be able to create them.We originally solved that in #30 by just instantiating empty values of their type, and then calling a
SetTerraformValuemethod on that value. This was super annoying, because then we needed two methods for getting anattr.Valueset to the right values:attr.Type.ValueFromTerraformandattr.Value.SetTerraformValuewere basically the same thing. But whatever, it worked.Except, no it didn't.
Complex types like lists and maps store their element/attribute types as properties on their structs. It's important that these be set. Only the
attr.Typehas this information, it's not passed in as part of thetftypes.Value. So reflect couldn't set those values, and produced brokenattr.Values as a result. (TheirToTerraformValuemethods would run into trouble, because they wouldn't know whattftypes.Typeto use for elements or attributes).To solve this problem, we decided to supply the
attr.Typefrom the schema to the reflect package, wiring it through so that we could instantiate newattr.Values when the opportunity presented itself.This solves our problem, because we got rid of the
attr.Value.SetTerraformValuemethod and used theattr.Type.ValueFromTerraformdirectly to just instantiate a new value, which made sure it was set up correctly. But now we have a new problem: what if theattr.Typein the schema doesn't produce theattr.Valuethey're trying to assign to? We decided to just throw an error on that one, because there's no reasonable way around it.Depends on #44.