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

Add and pass more regression tests for PerseusItem parser #1952

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

Conversation

benchristel
Copy link
Member

Issue: LEMS-2582

Test plan:

yarn test

@benchristel benchristel self-assigned this Dec 5, 2024
@benchristel
Copy link
Member Author

Note to reviewers: it's probably going to be easiest to read this one commit at a time.

Copy link
Collaborator

@jeremywiebe jeremywiebe left a comment

Choose a reason for hiding this comment

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

This is coming along nicely Ben! :)

One thing I noticed while reviewing. Do we have the concept of a tuple parser? I noticed some fields are definitely a [number, number] but we're parsing them as array(number).

Example from the matrix-widget.ts parser:

    matrixBoardSize: array(number),

Comment on lines +10 to +11
object({type: constant("ok")}),
object({type: constant("ok"), value: number}),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This confused me until I went and looked at the implementation and docs. I guess without having a "shape" that represents the discriminant, we'd have to rebuild how TypeScript decides what it is.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I agree that this is ugly. For comparison, here's how Zod does it:

const myUnion = z.discriminatedUnion("status", [
  z.object({ status: z.literal("success"), data: z.string() }),
  z.object({ status: z.literal("failed"), error: z.instanceof(Error) }),
]);

I suppose we could do something like:

function discriminatedUnion<
  DiscriminantKey extends keyof any,
  T extends {[d: DiscriminantKey]: string | number | boolean}
>(discriminantKey: DiscriminantKey, parseVariant: Parser<T>): DiscriminatedUnionBuilder {
  // ...
}

That would mirror how TypeScript thinks about discriminated unions. But the problem is, it wouldn't handle our versioned widget options. Those aren't (in TS's view) valid discriminated unions, since the discriminant, version.major, is a nested field. My current parser design allows us to treat them kind of like discriminated unions anyway, by splitting the choice of variant and the parsing of that variant into separate steps that can run arbitrary parsing logic.

If only version were simply a number (like it should have been all along), we could just do the thing that makes intuitive sense here. The curse of pre-TS code strikes again! :/

...and now, after writing all that out, I am tempted to create a special parser abstraction just for versioned widget options. Then we could simplify the one for discriminated unions to be more like Zod's API.

Copy link
Member Author

Choose a reason for hiding this comment

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

Something like:

const parseMyWidget = parseVersionedWidgetOptions(parseVersion2)
  .migratedFrom(parseVersion1, upgradeFromVersion1)
  .migratedFrom(parseVersion0, upgradeFromVersion0)
  .parser

const input = {type: "ok", value: "foobar"};

expect(parse(input, unionParser)).toEqual(
failure(`At (root).value -- expected number, but got "foobar"`),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be useful to include the discriminant info here? Otherwise we don't know which union entry has bad data. 🤔

At (root {type: "ok"}).value -- expected number, but got "footer"

Maybe that gets too verbose.

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 agree that would be better. The parser context currently doesn't have a way to track which variant of a union you're currently trying to parse, so I can't think of a way to add this info to the message easily.

I'll try adding a forVariant method to ParseContext, parallel to forSubtree. Maybe that will be easier than I'm imagining.

const input = {type: "triangle", width: -1, radius: 99};

expect(parse(input, unionParser)).toEqual(
failure(`At (root).type -- expected "rectangle", but got "triangle"`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It'd be nice of this could mention all eligible types.

Comment on lines 41 to 44
// TODO(benchristel): simplify should never be `true`, but we
// have some content where it is anyway. If we ever backfill
// the data, we should simplify `simplify`.
simplify: optional(nullable(pipeParsers(union(string).or(constant(true)).parser).then(convert(String)).parser)),
Copy link
Collaborator

Choose a reason for hiding this comment

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

@@ -35,7 +35,10 @@ export const parseNumericInputWidget: Parser<NumericInputWidget> = parseWidget(
answers: array(
object({
message: string,
value: optional(number),
// TODO(benchristel): value should never be null or undefined,
// but we have some content where it is anyway. If we backfill
Copy link
Collaborator

Choose a reason for hiding this comment

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

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