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

POC: migrate expression widget to latest version in PerseusItem parser #1908

Open
wants to merge 2 commits into
base: benc/parser-regression-tests-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {parseExpressionWidget} from "./expression-widget";
import {parse} from "../parse";
import {success} from "../result";

describe("parseExpressionWidget", () => {
it("migrates v0 options to v1", () => {
const widget = {
"type": "expression",
"graded": true,
"options": {
"value": "the value",
"form": false,
"simplify": false,
"times": false,
"buttonsVisible": "never",
"buttonSets": [
"basic"
],
"functions": [
"f",
"g",
"h"
],
},
"version": {
"major": 0,
"minor": 1
}
}

expect(parse(widget, parseExpressionWidget)).toEqual(success({
"type": "expression",
"graded": true,
static: undefined,
key: undefined,
"alignment": undefined,
"options": {
"times": false,
ariaLabel: undefined,
visibleLabel: undefined,
"buttonsVisible": "never",
"buttonSets": [
"basic"
],
"functions": [
"f",
"g",
"h"
],
"answerForms": [
{
considered: "correct",
form: false,
simplify: false,
value: "the value",
},
],
},
"version": {
"major": 1,
"minor": 0
}
}))
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ import {
union,
} from "../general-purpose-parsers";

import {parseWidget} from "./widget";
import {parseWidgetWithVersion, parseWidget} from "./widget";

import Expression from "../../../widgets/expression/expression"

import type {
ExpressionWidget,
PerseusExpressionAnswerForm,
} from "../../../perseus-types";
import type {Parser} from "../parser-types";
import type {
ParseContext,
ParsedValue,
Parser,
ParseResult
} from "../parser-types";

const parseAnswerForm: Parser<PerseusExpressionAnswerForm> = object({
value: string,
Expand All @@ -29,7 +36,7 @@ const parseAnswerForm: Parser<PerseusExpressionAnswerForm> = object({
).parser,
});

export const parseExpressionWidget: Parser<ExpressionWidget> = parseWidget(
const parseExpressionWidgetLatest: Parser<ExpressionWidget> = parseWidget(
constant("expression"),
object({
answerForms: array(parseAnswerForm),
Expand All @@ -51,3 +58,58 @@ export const parseExpressionWidget: Parser<ExpressionWidget> = parseWidget(
buttonsVisible: optional(enumeration("always", "never", "focused")),
}),
);

const parseExpressionWidgetV0 = parseWidgetWithVersion(
optional(object({major: constant(0), minor: number})),
constant("expression"),
object({
functions: array(string),
times: boolean,
visibleLabel: optional(string),
ariaLabel: optional(string),
form: boolean,
simplify: boolean,
value: string,
buttonSets: array(
enumeration(
"basic",
"basic+div",
"trig",
"prealgebra",
"logarithms",
"basic relations",
"advanced relations",
),
),
buttonsVisible: optional(enumeration("always", "never", "focused")),
}),
);

function migrateV0ToLatest(widget: ParsedValue<typeof parseExpressionWidgetV0>, ctx: ParseContext): ParseResult<ExpressionWidget> {
const {options} = widget;
return ctx.success({
...widget,
version: Expression.version,
options: {
times: options.times,
buttonSets: options.buttonSets,
functions: options.functions,
buttonsVisible: options.buttonsVisible,
visibleLabel: options.visibleLabel,
ariaLabel: options.ariaLabel,

answerForms: [
{
considered: "correct",
form: options.form,
simplify: options.simplify,
value: options.value,
},
],
},
})
}

export const parseExpressionWidget: Parser<ExpressionWidget> =
union(pipeParsers(parseExpressionWidgetV0).then(migrateV0ToLatest).parser)
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 will attempt to parse using the v0 and migration parsers and fall back to the parseExpressionWidgetLatest if that fails, right?

This might be extremely over-the-top optimization, but I wonder if we should order these in the opposite order given we'll more likely get a latest version set of options rather than v0 options? 🤔

.or(parseExpressionWidgetLatest).parser
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,19 @@ export function parseWidget<Type extends string, Options>(
),
});
}

export function parseWidgetWithVersion<Version extends {major: number, minor: number} | undefined, Type extends string, Options>(
parseVersion: Parser<Version>,
parseType: Parser<Type>,
parseOptions: Parser<Options>,
): Parser<WidgetOptions<Type, Options>> {
return object({
type: parseType,
static: optional(boolean),
graded: optional(boolean),
alignment: optional(string),
options: parseOptions,
key: optional(number),
version: parseVersion,
});
}
Loading