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

decoder: Add support for LiteralValue as Constraint #187

Merged
merged 3 commits into from
Mar 30, 2023

Conversation

radeksimko
Copy link
Member

@radeksimko radeksimko commented Jan 16, 2023

@radeksimko radeksimko added the enhancement New feature or request label Jan 16, 2023
@radeksimko radeksimko self-assigned this Jan 23, 2023
@radeksimko radeksimko force-pushed the f-expr-cons-literal-value branch 2 times, most recently from 1b308c0 to 0b459ab Compare January 27, 2023 14:08
@radeksimko radeksimko removed their assignment Feb 16, 2023
@radeksimko radeksimko changed the title decoder/schema: Add support for LiteralValue as Constraint decoder: Add support for LiteralValue as Constraint Mar 7, 2023
@radeksimko radeksimko force-pushed the f-expr-cons-literal-value branch from 0b459ab to 1aa46b1 Compare March 20, 2023 17:13
@jpogran jpogran force-pushed the f-expr-cons-literal-value branch 2 times, most recently from 99eca7e to 2e952d9 Compare March 22, 2023 16:39
@jpogran jpogran force-pushed the f-expr-cons-literal-value branch from 305645c to b8f42e4 Compare March 30, 2023 13:39
Copy link
Member Author

@radeksimko radeksimko left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:

Screenshot 2023-03-30 at 18 03 53

Comment on lines +48 to +51
if editRange.End.Line != pos.Line {
// account for quotes or brackets that are not closed
editRange.End = pos
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Nitpick: This would not account for completion inside a (valid) multi-line expression, which is one of the reasons we use slightly more sophisticated solution for such an edge case, e.g. in Map

// check any incomplete configuration up to a terminating character
fileBytes := m.pathCtx.Files[eType.Range().Filename].Bytes
recoveredBytes := recoverLeftBytes(fileBytes, pos, func(offset int, r rune) bool {
return isObjectItemTerminatingRune(r) && offset > recoveryPos.Byte
})
trimmedBytes := bytes.TrimRight(recoveredBytes, " \t")

Given that we only get to deal with primitive types here (which are mostly single-line), it should be fine to ignore. Multiline strings do exist (HEREDOC syntax) but we don't really use them in Terraform as constraint anywhere.

Implementing a perfect solution would be unnecessarily costly as we'd have to be looking for different terminating character or even a sequence of characters, depending on what type we can "guess".

TL;DR it's fine as is.


Side note - I am surprised that config such as this

attr = "foo

is parsed by HCL at all and returns expression with range that we can take advantage of here, because a lot of other invalid configuration variations end up missing from the AST entirely (including the attribute), which is also what motivated that recovery logic linked above. 😅

@radeksimko radeksimko marked this pull request as ready for review March 30, 2023 17:04
@radeksimko radeksimko requested a review from a team as a code owner March 30, 2023 17:04
@radeksimko radeksimko merged commit 5b41b42 into main Mar 30, 2023
@radeksimko radeksimko deleted the f-expr-cons-literal-value branch March 30, 2023 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants