-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
Try ConversionFrom if ConversionTo returns nil #194
Conversation
} | ||
} | ||
if in.IsCapsuleType() { | ||
if fn := in.CapsuleOps().ConversionFrom; fn != nil { | ||
return conversionFromCapsule(in, out, fn) | ||
if conv := conversionFromCapsule(in, out, fn); conv != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is strictly speaking unnecessary, but decided to change the conversionFrom case in the same way for uniformity / in case more conversion methods would be added
P.S.: |
Thanks for working on this! I can't review it immediately, but I'll try to look at it soon. In answer to the follow-up question: so far I've been holding the line that I'm not sure what sort of application you're using https://github.com/hashicorp/hcl/blob/d20d07fa73707e58954493bdd600c1fb0a0d4c77/ops.go#L26 https://github.com/hashicorp/hcl/blob/d20d07fa73707e58954493bdd600c1fb0a0d4c77/ops.go#L265 HCL admittedly has some different goals in its versions of these functions -- it's primarily motivated by HCL's automatic type conversion rules, rather than by capsule types -- but if HCL did have some special capsule types that ought to support HCL's attribute access and index syntax then those would be plausible places for HCL to implement those extensions. |
Ok, got it, this seems perfectly reasonable line to hold. For context, I'm passing those maps of maps to HCL so it could be evaluated there, but I've bumped into limitations of HCL often enough to already rewrite a large chunk of it. I guess expression evaluator is next 😉 Thanks for your time and your library! |
Hello again! Sorry for the delay. I had originally implemented this mechanism primarily as a way to convert from normal types to capsule types and vice-versa, so that it would be easier to use capsule types in a language that doesn't have direct support for them like HashiCorp's HCL. I hadn't really considered capsule-to-capsule conversions, but I don't see any reason why that shouldn't be allowed, and if they are going to be allowed then it seems very reasonable to support converting "the wrong way" if there's only a conversion in one direction. Therefore I'm going to merge this. Thanks for working on it! |
Currently when converting between two capsule types if
ConversionTo
returnsnil
then the conversion fails, even if the target type defines a suitableConversionFrom
.Example
This PR checks the
ConversionFrom
ifConversionTo
fails