-
Notifications
You must be signed in to change notification settings - Fork 22
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
Add support for built-in functions #145
Conversation
232bb2b
to
fb17b61
Compare
ca85fec
to
83f7af5
Compare
7dd453d
to
41034ba
Compare
41034ba
to
3b18e7d
Compare
3240078
to
2edfec1
Compare
2edfec1
to
4a6f85e
Compare
4a6f85e
to
ec8c2f8
Compare
ec8c2f8
to
aad1908
Compare
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.
I'm assuming this is now expected, until 1.4.0 final actually lands, just surfacing it here for posterity:
2023/02/16 12:39:16 ensuring terraform is installed
2023/02/16 12:39:17 failed to obtain product version from "https://releases.hashicorp.com/terraform/1.4.0/index.json": 404 Not Found
exit status 1
internal/funcs/generated/doc.go:4: running "go": exit status 1
I left some comments in-line, mostly minor, but the escaping one should certainly be resolved before we merge it, especially if it affects upstream as well (i.e. the JSON TF Core produces is HTML escaped).
}, | ||
}, | ||
ReturnType: cty.String, | ||
Description: "`base64sha256` computes the SHA256 hash of a given string and encodes it with Base64. This is not equivalent to `base64encode(sha256("test"))` since `sha256()` returns hexadecimal representation.", |
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.
I'm not sure where this came from and whether it affects other places in Core as well, but I see some HTML escaping in effect (&#...
) which probably shouldn't be.
The context in which we need to escape the (Markdown) description text would be single-line Go string, i.e. "
would need to be \"
but that's about it. Single quotes and probably most other characters can stay as-is.
I don't know if there's any escaping function we could use in this context in the generator, here I suppose it's just a simple matter of manual Find+Replace.
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.
Good catch! I fixed it in Terraform in hashicorp/terraform#32710 and replaced all occurrences here.
internal/funcs/generated/README.md
Outdated
|
||
This package can generate function signature files for Terraform >= 1.4 automatically. | ||
|
||
It is intended to run whenever HashiCorp releases a new Terraform. If the Terraform version contains updated function signatures, it generates a new file for that version. When no changes are detected, one should commit the version bump in `gen/gen.go`. |
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.
It is intended to run whenever HashiCorp releases a new Terraform. If the Terraform version contains updated function signatures, it generates a new file for that version. When no changes are detected, one should commit the version bump in `gen/gen.go`. | |
It is intended to run whenever HashiCorp releases a new Terraform version. If the Terraform version contains updated function signatures, it generates a new file for that version. When no changes are detected, one should commit the version bump in `gen/gen.go`. |
internal/funcs/generated/gen/gen.go
Outdated
"escapeDescription": escapeDescription, | ||
}).Parse(outputTpl) | ||
if err != nil { | ||
log.Fatal(err) |
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.
Nitpick: I know this is a script which will be executed by a maintainer, but it's still a good practice for any downstream functions to return error and only the top level to actually deal with surfacing it somehow.
This enables testability and reusability and helps readability (it's easier to understand the flow of the program as a whole), even though admittedly we probably won't be testing or reusing this code any time soon.
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.
Oh, missed updating the error handling for the two templating functions. Thanks for pointing it out.
internal/funcs/generated/gen/gen.go
Outdated
} | ||
|
||
func escapeDescription(description string) string { | ||
return strings.ReplaceAll(description, `\`, `\\`) |
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.
Again - not sure if the HTML escape characters come already in the JSON output - which would be something to address upstream, but either way we should make sure that double quotes are escaped correctly here too.
schema/functions.go
Outdated
} | ||
} | ||
|
||
return nil, NoCompatibleFunctionsErr{Constraints: vc} |
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.
Given this should never happen, I'd consider not exporting this error - since we should never be catching it in LS.
We could go as far as panic
ing here, but it's probably still better not to, since it's a library and as such it should not decide how errors are surfaced. LS could however turn that error into panic
.
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.
Agreed — replaced it with a simple error string which we can turn into a panic
upstream.
schema/functions.go
Outdated
) | ||
|
||
func FunctionsForVersion(v *version.Version) (map[string]schema.FunctionSignature, error) { | ||
ver, err := semVer(v) |
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.
I think the semVer
predates the Core()
method upstream, which could now be used directly and we could get rid of semVer()
.
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.
LGTM
This PR adds the function signatures for all previous Terraform versions, starting with 0.12. For versions >= 1.4, we use the new
terraform metadata functions -json
command to generate the signatures.The changes are currently a no-op for users. However, we will follow up with modifications to the language server to obtain the functions for a specific Terraform version using
FunctionsForVersion
.I've created a list of all Terraform functions with their introduction versions for a better overview and used that as a source for all the signatures.
TODO
metadata functions -json
command terraform-exec#358 is merged