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

Store ghc version for each snippet #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

trevorsibanda
Copy link

Proposed changes

  • Move Contents out of DB and rename to Paste
  • Add ghcVersion field to Paste
  • Alter paste table to include ghcVersion column
  • Modify play.mustache and play-index.ts
  • Add db migration script

Contributor checklist

@tomsmeding
Copy link
Collaborator

Haven't built or tested locally, but from reading the code, this looks good so far. :) Thanks!

@tomsmeding
Copy link
Collaborator

Something that I thought of (not sure if this is still applicable after your last commit): I'm not sure I'd like the string "default" to ever appear in a Version. This is kind of in-band signalling of an unusual value, which I usually prefer to model using an ADT, i.e. Maybe Version in this case. In the database, the same idea would apply by making the ghcVersion column nullable, and storing NULL whenever there is no version specified.

@trevorsibanda trevorsibanda marked this pull request as ready for review March 3, 2023 09:05
@trevorsibanda
Copy link
Author

I've made the ghcVersion column nullable and left it empty if it's not a supported version.

@trevorsibanda trevorsibanda changed the title WIP: Store ghc version for each snippet Store ghc version for each snippet Mar 3, 2023
Copy link

@piotr-lewandowski piotr-lewandowski left a comment

Choose a reason for hiding this comment

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

Honestly, can't really say much about the code, but you requested a review.

Anyway, thanks for the fix :)

@tomsmeding
Copy link
Collaborator

Oh oops -- apparently I do not get notifications of PRs being set to "ready for review", and I'd missed most of what happened here completely. Sorry for the silence.

I'll try to review this either today or early next week.

Copy link
Collaborator

@tomsmeding tomsmeding left a comment

Choose a reason for hiding this comment

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

Disclaimer: I didn't test yet! Just looked at the code in the PR here.

Mostly looks good, couple of small things. Perhaps you can have a look at those? If you have no time any more I can also address them if you want.

@@ -71,6 +69,7 @@ schemaVersion :: Int
,"CREATE TABLE pastes (\n\
\ id INTEGER PRIMARY KEY NOT NULL, \n\
\ key BLOB NOT NULL, \n\
\ ghcVersion TEXT, \n\
Copy link
Collaborator

Choose a reason for hiding this comment

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

please add an explicit NULL after TEXT

preload_ghc_version = {{&version}};
{{/version}}
{{^preload_ghc_version}}
preload_ghc_version = "default";
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't like the in-band signalling here very much. I think it would be better if the "default" signal was null instead. null here would correspond to NULL in the database.


UPDATE meta SET version = 6;

ALTER TABLE pastes ADD COLUMN ghcVersion TEXT;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also add an explicit NULL here after TEXT

const snippet = preload_script != null ? preload_script : example_snippets[Math.floor(Math.random() * example_snippets.length)];
if (preload_ghc_version == "default") {
preload_ghc_version = defaultGHCversion
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

When switching away from "default", this whole if-statement can go :)

else do body <- readRequestBody (fromIntegral @Int @Word64 maxSaveFileSize)
let body' = BSL.toStrict body
let contents = Contents [(Nothing, body')] Nothing Nothing
then lift $ httpError 429 "Please slow down a bit, you're rate limited"
Copy link
Collaborator

Choose a reason for hiding this comment

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

when isSpam $ do
  lift $ httpError 429 "Please slow down a bit, you're rate limited"
  exitEarly ()

postdata <- ...

_ -> do lift (httpError 400 "Invalid JSON")
exitEarly ()
versions <- liftIO (WP.getAvailableVersions (ctxPool ctx))
let version' = fromMaybe defaultGHCVersion $ Just <$> L.find (==version) versions
Copy link
Collaborator

Choose a reason for hiding this comment

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

So this evaluates to fromMaybe Nothing (fmap Just x), which is equivalent to x. I get that you want to abstract defaultGHCVersion out, but since the intuitive semantics of Maybe correspond quite nicely with what is meant here ("Nothing indicates no version specified"), an alternative is to embrace the Maybe, remove defaultGHCVersion (which doesn't really yield a default version, it yields no version which is typically interpreted as meaning some default version), and simplify this to L.find (==version) versions. Which, by the way, is a nice trick.

let contents = Contents [(Nothing, body')] Nothing Nothing
then lift $ httpError 429 "Please slow down a bit, you're rate limited"
else do postdata <- getRequestBodyEarlyExit maxSaveFileSize "Program too large"
ClientSavePasteReq{csprCode =code, csprVersion = version} <- case J.decodeStrict' postdata of
Copy link
Collaborator

Choose a reason for hiding this comment

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

missing space after =

const readable = versions[i] in ghcReadableVersion ? ghcReadableVersion[versions[i]] : versions[i];
opt.textContent = "GHC " + readable;
if (versions[i] == defaultGHCversion) opt.setAttribute("selected", "");
let readable = versions[i] in ghcReadableVersion ? ghcReadableVersion[versions[i]] : versions[i];
Copy link
Collaborator

Choose a reason for hiding this comment

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

No reason for readable to become non-const, right?

if (versions[i] === defaultGHCversion) {
verAnnotation = "(Default) "
}
opt.textContent = verAnnotation + "GHC " + readable;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I prefer the annotation to come at the end, so GHC <version> (Default). Then the entries in the list all start in a uniform way (with GHC ).


versionToMustache :: Maybe Version -> Mustache.Value
versionToMustache = \case
Just ( Version v) -> toMustache v
Copy link
Collaborator

Choose a reason for hiding this comment

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

Superfluous space after (.
Also, versionToMustache = mixinMaybeNull (\(Version v) -> v) if I'm not mistaken.

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.

3 participants