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

accept UTF8-BOM when reading dashboard or monitor from JSON file #622

Merged
merged 9 commits into from
Jan 22, 2024

Conversation

kmuto
Copy link
Member

@kmuto kmuto commented Jan 17, 2024

Passing a JSON file with a UTF-8 BOM to mkr dashboards push --file-path causes an error that is difficult for users to understand.

     error invalid character 'ï' looking for beginning of value

I understand that "JSON files must not contain a BOM", but I thought it would be more user-friendly to ignore the BOM part and read the file without making an error, so I made a patch.

Test

# original version 0.54.0
$ mkr dashboards push --file-path t-nobom.json
$ mkr dashboards push --file-path t-withbom.json
     error invalid character 'ï' looking for beginning of value

# (remove dashboards)

# patched version
$ ./mkr dashboards push --file-path t-nobom.json
$ ./mkr dashboards push --file-path t-withbom.json

t-nobom.json
t-withbom.json

Comment on lines +98 to +99
fallback := unicode.UTF8.NewDecoder()
r := transform.NewReader(src, unicode.BOMOverride(fallback))
Copy link
Member

Choose a reason for hiding this comment

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

I do like it a lot!

Copy link
Member

@lufia lufia left a comment

Choose a reason for hiding this comment

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

Could you add a test?

@kmuto kmuto changed the title accept UTF8-BOM when reading dashboard JSON file accept UTF8-BOM when reading dashboard or monitor from JSON file Jan 19, 2024
@kmuto
Copy link
Member Author

kmuto commented Jan 19, 2024

I added similar BOM handling to monitors code. (it will be called by mkr monitors diff or mkr monitors push)
I added the test unit to monitors/command_test.go also, but gave up to create a test for dashboards because doPushDashboard() is too large to only test BOM.

monitors/command_test.go Outdated Show resolved Hide resolved
Comment on lines +205 to +209
tmpFile, err := os.CreateTemp("", "")
if err != nil {
t.Errorf("should not raise error: %v", err)
}
defer os.Remove(tmpFile.Name())
Copy link
Member

Choose a reason for hiding this comment

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

testing.T.TempDir is useful.

Copy link
Member Author

Choose a reason for hiding this comment

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

I had tried it on 8df6538 , 3295eed , and db949f8 but all of them went failed on Windows (I saw golang/go#50510 )
It would be better to use it but I keep CreateTemp code at this time for Windows.

@kmuto kmuto merged commit ceaac83 into mackerelio:master Jan 22, 2024
13 checks passed
@kmuto kmuto deleted the dashboard-bom branch January 22, 2024 04:37
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.

2 participants