Skip to content

refactor(misconf): migrate from custom Azure JSON parser #9222

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

nikpivkin
Copy link
Contributor

@nikpivkin nikpivkin commented Jul 18, 2025

Description

This PR replaces the custom JSON parser with github.com/go-json-experiment/json for parsing ARM templates. This significantly simplifies the codebase and reduces maintenance overhead.

Instead of manually parsing the JSON structure, and positional information, the unmarshaler from github.com/go-json-experiment/json and some helper functions from x/json are now used.

The only non-trivial part of the migration was correctly initializing the metadata object with:
• file system (fs.FS),
• file path,
• range (position of the node in the source file),
• reference to the corresponding JSON node in the tree.

To support populating this metadata during decoding, a hook system was introduced. Currently, only the After hook is implemented. It is triggered after decoding each object and provides access to:
• decoder,
• decoded object,
• source location of the JSON node.

Related issues

Remove this section if you don't have related PRs.

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@nikpivkin nikpivkin force-pushed the azure-json-parser branch from 9da60be to 308f3d1 Compare July 21, 2025 10:46
@nikpivkin nikpivkin marked this pull request as ready for review July 22, 2025 09:33
@nikpivkin nikpivkin requested a review from DmitriyLewen July 22, 2025 09:33
@simar7 simar7 requested a review from Copilot July 22, 2025 23:34
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the Azure ARM parser to replace a custom JSON parser with the standardized github.com/go-json-experiment/json library. This modernization simplifies the codebase significantly by removing approximately 1,500 lines of custom parsing logic while maintaining equivalent functionality.

  • Replaced custom armjson parsing library with go-json-experiment/json
  • Introduced a hook system for capturing metadata during JSON unmarshaling
  • Updated Value struct to use pointer-based metadata management

Reviewed Changes

Copilot reviewed 42 out of 42 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
pkg/x/json/json.go Enhanced with generic hooks and location tracking capabilities
pkg/iac/scanners/azure/value.go Refactored to implement json.UnmarshalerFrom interface
pkg/iac/scanners/azure/arm/parser/template.go New ParseTemplate function with metadata collection system
pkg/iac/scanners/azure/arm/parser/parser.go Simplified to use new ParseTemplate function
Multiple armjson files Removed entire custom JSON parser implementation
Comments suppressed due to low confidence (1)

pkg/iac/scanners/azure/arm/parser/template.go:69

  • Field name 'Loc' is inconsistent with other fields and unclear. It should be renamed to 'Location' to match the original field name and improve readability.
	Loc        azure.Value `json:"location"`

@simar7
Copy link
Member

simar7 commented Jul 22, 2025

@nikpivkin although somewhat unrelated - I recall us not being able to save positional arguments for helm charts. Could we use a similar hook based approach for that too?

@nikpivkin
Copy link
Contributor Author

@nikpivkin although somewhat unrelated - I recall us not being able to save positional arguments for helm charts. Could we use a similar hook based approach for that too?

Are you referring to the positions in the original Helm templates? I don't think that's possible. I left a comment in this discussion.

Signed-off-by: nikpivkin <[email protected]>
Comment on lines -53 to +69
Location azure.Value `json:"location"`
Loc azure.Value `json:"location"`
Copy link
Member

Choose a reason for hiding this comment

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

Any reason to change the field? Location seems self explanatory.

@simar7 simar7 self-requested a review August 15, 2025 04:43
Copy link
Member

@simar7 simar7 left a comment

Choose a reason for hiding this comment

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

lgtm, just left one small nit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor(azure): migrate from custom JSON parser
4 participants