-
Notifications
You must be signed in to change notification settings - Fork 601
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
json: Walk expressions like hclsyntax.Walk
#543
Comments
Hi @wata727! Thanks for sharing this proposal. A common challenge with providing this kind of functionality against the JSON syntax is that the JSON syntax is always ambiguous until the caller provides a schema. That's why the API of this package is relatively opaque compared to I think there is one compromise we could potentially support here, though: it's required in the specification that when evaluating an expression in "full expression mode" a string is parsed as a string template in the native syntax, which in practice means using the parser and node types from the I think the minimum possible interface for this would be for the caller to explicitly choose between the two expression evaluation modes: literal-only mode or full expression mode. Once we know the mode we can in principle return a
It would be nice to also include the static analysis modes, where in particular the "static call" and "static traversal" modes cause the JSON syntax to treat strings as native syntax expressions rather than native syntax templates, but we don't necessarily need to solve this all at once. With the benefit of hindsight, the Note that the API design I've described here intentionally only helps if you're already holding an I don't think it's viable to allow schemaless analysis of the body structure around the expressions because working successfully with that API would require the caller to reimplement all of the different possibilities from the Structural Elements part of the JSON syntax specification, which is non-trivial. HCL is intentionally schema-based and I don't consider it a priority to offer APIs to analyze structure without already knowing the schema of the data you plan to obtain. Unfortunately I don't think I have the spare capacity to design, implement, or review a large change to the |
Thank you for telling me about the language design for this feature. I agree that it is better to provide an interface to the structure of JSON expression by converting it to the HCL native expression. I understand that maintaining two different mechanisms is hard and that it is difficult to change the way the current JSON syntax works. For now, leave this issue open. I think it will be difficult to tackle this for a while, but I would like to share with other users that the discussion has stopped due to the above difficulties. Fortunately, the problem we are currently facing is not yet important, so we will leave it as a future improvement. If the problem becomes apparent, it may be possible to fix it with a fork that adopted your proposal. Thank you again for taking the time to this issue! |
In the context of static analysis, there is a use case where you want to visit all expressions in a JSON body. Imagine getting an expression for a string containing a reference to
terraform.workspace
from the following configuration:If you know the schema of this configuration, you can get the exact
"${terraform.workspace}"
expression, but if you don't know the schema, it's a difficult problem. With the following way, an expression larger than the original expression will be obtained:The HCL native syntax provides the
hclsyntax.Walk
to solve the issue, but thejson.Walk
in the JSON syntax does not exist. Is it possible to provide such an API?Proposal
If you add
json.Walk
, the interface will look like this:In this case, the challenge is that
json.node
is private. I remember that exposing these structures was negative in previous discussions #332 (comment)My idea is to provide an interface that depends on the
hcl.Body
andhcl.Expression
instead ofjson.node
:In the above interface, there is a problem that you can pass an unintended body such as
hclsyntax.Body
, but in that case, it halts walking and reports diagnostics.Refer to
expr.Variables()
for the behavior of walking all expressions. In this way, the expression is recursively created from the value and passed to the walker. In addition to this, it should be enough to consider thenumberVal
,booleanVal
, andnullVal
.https://github.com/hashicorp/hcl/blob/v2.13.0/json/structure.go#L513-L555
In addition to these, we need utilities to determine the type of expression. I believe this can provide interfaces like
IsJSONObjectExpression
,IsJSONArrayExpression
likeIsJSONExpression
.https://github.com/hashicorp/hcl/blob/v2.13.0/json/is.go#L7-L31
How about the above proposal? If there is no problem, I would like to work with the implementation. Thank you.
References
terraform_deprecated_interpolation
: deeply inspect map/list attributes terraform-linters/tflint#1257The text was updated successfully, but these errors were encountered: