fix: support array-based JSON syntax in WalkExpressions #422
+90
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes
WalkExpressionsto handle Terraform JSON files using array-based root syntax like[{"import": {...}}]. Previously these files would fail becauseJustAttributes()doesn't work with JSON arrays.Issue
Terraform JSON files can use either object-based syntax
{"resource": {...}}or array-based syntax[{"resource": {...}}]. TheWalkExpressionsmethod in the plugin SDK only supported object-based syntax.When processing array-based JSON files,
Body.JustAttributes()fails because HCL's JSON parser doesn't expose attributes at the array level. This causedWalkExpressionsto return errors and skip processing these files entirely.Closes terraform-linters/tflint#2279
Changes
WalkExpressionsto support array-based JSON syntax in addition to object-based syntaxextractJSONKeysfunction to parse JSON bytes withencoding/jsonand extract attribute names from both objects and arrays of objectsgetJSONAttributesfunction that falls back to schema-based extraction whenJustAttributes()failsJustAttributes()for backward compatibility with object-based JSONextractJSONKeysto discover attribute namesPartialContentto extract proper*json.expressionobjectsjson.IsJSONExpression()checks used by rulesTesting
resourceandvariableblocksReferences