func()

in tflint-ruleset-aws-serverless/rules/aws_apigatewayv2_stage_structured_logging.go [49:95]


func (r *AwsApigatewayV2StageStructuredLoggingRule) Check(runner tflint.Runner) error {
	// Regexp to substitute all $context. variables
	re := regexp.MustCompile(`\$context\.[a-zA-Z\.]+`)

	return runner.WalkResourceBlocks(r.resourceType, r.blockName, func(block *hcl.Block) error {
		body, _, diags := block.Body.PartialContent(&hcl.BodySchema{
			Attributes: []hcl.AttributeSchema{
				{
					Name: r.attributeName,
				},
			},
		})

		if diags.HasErrors() {
			return diags
		}

		var attrValue string
		attribute, ok := body.Attributes[r.attributeName]
		if !ok {
			runner.EmitIssue(
				r,
				fmt.Sprintf("\"%s\" is not present.", r.attributeName),
				body.MissingItemRange,
			)
		} else {
			err := runner.EvaluateExpr(attribute.Expr, &attrValue, nil)
			if err != nil {
				return err
			}

			attrValue = re.ReplaceAllLiteralString(attrValue, "4")

			// TODO: test if JSON
			var js map[string]interface{}
			if json.Unmarshal([]byte(attrValue), &js) != nil {
				runner.EmitIssueOnExpr(
					r,
					fmt.Sprintf("\"%s\" is not valid JSON.", r.attributeName),
					attribute.Expr,
				)
			}
		}

		return nil
	})
}