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

Parse JSON blobs more leniently #5502

Closed
wants to merge 2 commits into from
Closed

Conversation

dlon
Copy link
Member

@dlon dlon commented Nov 25, 2023

Specifically:

  • Add missing outer braces, if missing
  • Ignore "comment" lines, i.e. lines beginning with '#'

As an example, this is now valid:

# Add my override
"relay_overrides": [{ "hostname": "test", "ipv4_addr_in": "1.3.3.7" }]

The above is equivalent to

{
    "relay_overrides": [{ "hostname": "test", "ipv4_addr_in": "1.3.3.7" }]
}

Fix DES-478.


This change is Reviewable

Specifically:
* Ignore trailing commas in objects and arrays
* Add missing outer braces, if missing
* Ignore "comment" lines, i.e. lines beginning with '#'
* If a value is expected to be an array but isn't, wrap it in an array
@dlon dlon force-pushed the parse-json-blobs-leniently branch from 18c226c to 8e6ccb9 Compare November 25, 2023 23:29
Copy link

linear bot commented Nov 27, 2023

DES-478 Parse settings patches less strictly

  1. Make the outer braces optional. This should be a valid patch:
"relay_overrides": [
  { "hostname": "se-got-wg-001", "ipv4_addr_in": "1.2.3.4" }
]
  1. Trim trailing commas in lists/objects.
  2. When an array is expected but some other value is found, treat it as an array containing that value.
  3. Ignore "comments" (i.e. lines beginning with #).

@dlon dlon marked this pull request as ready for review November 27, 2023 09:16
Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @dlon)


mullvad-daemon/src/settings/patch.rs line 206 at r2 (raw file):

        .filter(|line| !line.trim_start().starts_with('#'))
        .collect::<Vec<_>>()
        .join("\n");

This should be refactored to it's own function, and combining multiple transformations be done via composition / chaining function calls instead 😊

Code quote:

    // Remove commented lines
    let mut patch = json_patch
        .lines()
        .filter(|line| !line.trim_start().starts_with('#'))
        .collect::<Vec<_>>()
        .join("\n");

@dlon dlon force-pushed the parse-json-blobs-leniently branch from 8ffba1a to 32221f6 Compare November 30, 2023 08:58
Copy link
Member Author

@dlon dlon left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @MarkusPettersson98)


mullvad-daemon/src/settings/patch.rs line 206 at r2 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

This should be refactored to it's own function, and combining multiple transformations be done via composition / chaining function calls instead 😊

Done.

Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r3, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@dlon dlon added the On hold Means the PR is paused for some reason. No need to review it for now label Nov 30, 2023
@dlon dlon closed this Dec 8, 2023
@dlon dlon deleted the parse-json-blobs-leniently branch December 27, 2023 17:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
On hold Means the PR is paused for some reason. No need to review it for now
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants