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

type guards for SPAnalysisDataModel #93

Merged
merged 5 commits into from
Jun 28, 2024
Merged

type guards for SPAnalysisDataModel #93

merged 5 commits into from
Jun 28, 2024

Conversation

magland
Copy link
Collaborator

@magland magland commented Jun 26, 2024

Fixes #89

Added some type guards which are used to check the input of local storage and also in loadFromProjectFiles

This was somewhat difficult to test because if you manually put an error in the local storage, that gets overwritten upon page reload. So I managed to achieve this test by putting a temporary error in the write-to-local-storage code and verified that the page did not crash and I got the warning message upon reload.

@magland magland requested review from jsoules and WardBrian June 26, 2024 21:21
Copy link
Collaborator

@WardBrian WardBrian left a comment

Choose a reason for hiding this comment

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

Two small things

Comment on lines +15 to +24
export const isSamplingOpts = (x: any): x is SamplingOpts => {
if (!x) return false
if (typeof x !== 'object') return false
if (typeof x.num_chains !== 'number') return false
if (typeof x.num_warmup !== 'number') return false
if (typeof x.num_samples !== 'number') return false
if (typeof x.init_radius !== 'number') return false
if (x.seed !== undefined && typeof x.seed !== 'number') return false
return true
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we also verify that e.g. num_chains is an integer?

Not sure what the best way to check this is in JS -- Math.round(x) === x?

Copy link
Collaborator Author

@magland magland Jun 27, 2024

Choose a reason for hiding this comment

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

We could check that elsewhere, but I don't think this belongs in the type guard. I believe a type guard should have the sole purpose of guaranteeing that the thing is of the right javascript type.

gui/src/app/StanSampler/StanSampler.ts Show resolved Hide resolved
@WardBrian WardBrian merged commit acdfe1d into main Jun 28, 2024
2 checks passed
@WardBrian WardBrian deleted the type-guards branch June 28, 2024 15:33
Copy link
Collaborator

@jsoules jsoules left a comment

Choose a reason for hiding this comment

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

I actually did have some suggestions on this, but I see it's merged... I'll go do it separately

gui/src/app/SPAnalysis/SPAnalysisContextProvider.tsx Outdated Show resolved Hide resolved
gui/src/app/SPAnalysis/SPAnalysisDataModel.ts Show resolved Hide resolved
Comment on lines +100 to +105
if (isSamplingOpts(parsed)) {
data.samplingOpts = parsed
}
else {
console.error('Invalid sampling_opts in fetchRemoteAnalysis', parsed)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we're coming this far, it'd probably be good to include a validity check beyond just a type-guard--e.g. natural numbers where appropriate. (I'm okay with the discussion below that suggests it shouldn't be in the type guard proper, but I think if we're going to do the deserialization validity check it's worth it to go the whole way in another fn)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm imagining most likely a formal parsing function, not in this file but closer to the type definition, that would replace the JSON.parse() line above and return either a valid SamplingOpts or undefined?

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.

type guard needed when loading from localStorage
3 participants