-
Notifications
You must be signed in to change notification settings - Fork 350
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
base: main
Are you sure you want to change the base?
Conversation
Note to reviewers: it's probably going to be easiest to read this one commit at a time. |
There was a problem hiding this 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),
object({type: constant("ok")}), | ||
object({type: constant("ok"), value: number}), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"`), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"`) |
There was a problem hiding this comment.
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.
// 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)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind adding this note and an example to this doc? https://khanacademy.atlassian.net/wiki/spaces/ENG/pages/3524690040/Content+Issues+for+Backfill
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue: LEMS-2582
Test plan:
yarn test