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

Exports tagging #2969

Merged
merged 21 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/nervous-berries-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@quri/squiggle-lang": patch
"@quri/squiggle-components": patch
---

Squiggle-Lang exports SqValue with "imports". All exports are Tagged with "exportData" tag.
93 changes: 72 additions & 21 deletions packages/components/src/components/SquiggleOutputViewer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import {

import { SquiggleViewer } from "../../index.js";
import { SquiggleOutput } from "../../lib/hooks/useSquiggle.js";
import { getResultValue, getResultVariables } from "../../lib/utility.js";
import {
getResultExports,
getResultImports,
getResultValue,
getResultVariables,
} from "../../lib/utility.js";
import { CodeEditorHandle } from "../CodeEditor/index.js";
import { ErrorBoundary } from "../ErrorBoundary.js";
import { PartialPlaygroundSettings } from "../PlaygroundSettings.js";
Expand Down Expand Up @@ -51,25 +56,39 @@ export const SquiggleOutputViewer = forwardRef<SquiggleViewerHandle, Props>(
({ squiggleOutput, isRunning, editor, ...settings }, viewerRef) => {
const resultItem = getResultValue(squiggleOutput);
const resultVariables = getResultVariables(squiggleOutput);
const resultImports = getResultImports(squiggleOutput);
const resultExports = getResultExports(squiggleOutput);

const hasResult = Boolean(resultItem?.ok);
const variablesCount = resultVariables?.ok
? resultVariables.value.value.entries().length
: 0;
const importsCount = resultImports?.ok
? resultImports.value.value.entries().length
: 0;
const exportsCount = resultExports?.ok
? resultExports.value.value.entries().length
: 0;

const [mode, setMode] = useState<"variables" | "result">(
hasResult ? "result" : "variables"
);
const [mode, setMode] = useState<
"Imports" | "Exports" | "Variables" | "Result"
>(hasResult ? "Result" : exportsCount > 0 ? "Exports" : "Variables");

let squiggleViewer: JSX.Element | null = null;
if (squiggleOutput.code) {
let usedResult: result<SqValue, SqError> | undefined;
switch (mode) {
case "result":
case "Result":
usedResult = resultItem;
break;
case "variables":
case "Variables":
usedResult = resultVariables;
break;
case "Imports":
usedResult = resultImports;
break;
case "Exports":
usedResult = resultExports;
}

if (usedResult) {
Expand Down Expand Up @@ -97,19 +116,51 @@ export const SquiggleOutputViewer = forwardRef<SquiggleViewerHandle, Props>(
<Dropdown
render={({ close }) => (
<DropdownMenu>
<DropdownMenuActionItem
icon={CodeBracketIcon}
title={
<MenuItemTitle
title="Variables"
type={variablesCount ? `{}${variablesCount}` : null}
/>
}
onClick={() => {
setMode("variables");
close();
}}
/>
{Boolean(importsCount) && (
<DropdownMenuActionItem
icon={CodeBracketIcon}
title={
<MenuItemTitle
title="Imports"
type={importsCount ? `{}${importsCount}` : null}
/>
}
onClick={() => {
setMode("Imports");
close();
}}
/>
)}
{Boolean(variablesCount) && (
<DropdownMenuActionItem
icon={CodeBracketIcon}
title={
<MenuItemTitle
title="Variables"
type={variablesCount ? `{}${variablesCount}` : null}
/>
}
onClick={() => {
setMode("Variables");
close();
}}
/>
)}
{Boolean(exportsCount) && (
<DropdownMenuActionItem
icon={CodeBracketIcon}
title={
<MenuItemTitle
title="Exports"
type={exportsCount ? `{}${exportsCount}` : null}
/>
}
onClick={() => {
setMode("Exports");
close();
}}
/>
)}
<DropdownMenuActionItem
icon={CodeBracketIcon}
title={
Expand All @@ -119,7 +170,7 @@ export const SquiggleOutputViewer = forwardRef<SquiggleViewerHandle, Props>(
/>
}
onClick={() => {
setMode("result");
setMode("Result");
close();
}}
/>
Expand All @@ -128,7 +179,7 @@ export const SquiggleOutputViewer = forwardRef<SquiggleViewerHandle, Props>(
>
<Button size="small">
<div className="flex items-center space-x-1.5">
<span>{mode === "variables" ? "Variables" : "Result"}</span>
<span>{mode}</span>
<TriangleIcon
className="rotate-180 text-slate-400"
size={10}
Expand Down
16 changes: 6 additions & 10 deletions packages/components/src/components/SquiggleViewer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ function isTopLevel(path: SqValuePath): boolean {
return path.items.length === 0;
}

const topLevelResultName = "Result";
export const topLevelBindingsName = "Variables";

function topLevelName(path: SqValuePath): string {
if (path.root === "result") {
return topLevelResultName;
} else if (path.root === "bindings") {
return topLevelBindingsName;
} else {
return path.root;
}
return {
result: "Result",
bindings: "Variables",
imports: "Imports",
exports: "Exports",
}[path.root];
}

export function pathAsString(path: SqValuePath) {
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/lib/hooks/useSquiggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type SquiggleOutput = {
{
exports: SqDict;
result: SqValue;
imports: SqDict;
bindings: SqDict;
},
SqError
Expand Down
12 changes: 12 additions & 0 deletions packages/components/src/lib/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ export function getResultVariables({
return resultMap(output, (value) => value.bindings.asValue());
}

export function getResultImports({
output,
}: SquiggleOutput): result<SqDictValue, SqError> {
return resultMap(output, (value) => value.imports.asValue());
}

export function getResultExports({
output,
}: SquiggleOutput): result<SqDictValue, SqError> {
return resultMap(output, (value) => value.exports.asValue());
}

export function getResultValue({
output,
}: SquiggleOutput): result<SqValue, SqError> | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const VersionedSquiggleModelExportPage: FC<
<VersionedSquiggleChart
version={version}
code={code}
rootPathOverride={rootPath}
// TODO - we still don't have a good way to sync up squiggle-lang and versioned squiggle-components types
rootPathOverride={rootPath as any}
project={project as any}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ lib.x`
"main",
`
import './lib' as lib
lib.x`
lib`
);

await project.run("main");

expect(project.getResult("main").ok).toEqual(true);
expect(project.getResult("main").value.toString()).toEqual("5");
expect(project.getResult("main").value.toString()).toEqual(
'{x: 5, with tags {exportData: {sourceId: "./lib", path: ["x"]}}}, with tags {exportData: {sourceId: "./lib", path: []}}'
);
});

describe("Mix imports and continues", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ test("test bindings", async () => {
test("test exports", async () => {
const project = SqProject.create();
project.setSource("main", "x = 5; export y = 6; z = 7; export t = 8");
expect(await runFetchExports(project, "main")).toBe("{y: 6,t: 8}");
expect(await runFetchExports(project, "main")).toBe(
'{y: 6, with tags {exportData: {sourceId: "main", path: ["y"]}}, t: 8, with tags {exportData: {sourceId: "main", path: ["t"]}}}, with tags {name: "main", exportData: {sourceId: "main", path: []}}'
);
});

test("test decorated exports", async () => {
Expand All @@ -51,7 +53,7 @@ test("test decorated exports", async () => {
`
);
expect(await runFetchExports(project, "main")).toBe(
'{x: 5, with params name: "X",y: 6, with params name: "Y", doc: "whatever"}'
'{x: 5, with tags {name: "X", exportData: {sourceId: "main", path: ["x"]}}, y: 6, with tags {name: "Y", doc: "whatever", exportData: {sourceId: "main", path: ["y"]}}}, with tags {name: "main", exportData: {sourceId: "main", path: []}}'
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe("SqProject with imports", () => {

const output = project.getOutput("main");

expect(output).toBeOkOutput("6", "{a: 6,b: 2}");
expect(output).toBeOkOutput("6", "{a: 6, b: 2}");

/* `getDependencies` returns the list of all dependency ids for a given source id, both continues and imports. */
expect(project.getDependencies("main")).toEqual([
Expand Down
2 changes: 1 addition & 1 deletion packages/squiggle-lang/__tests__/ast/parse_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Peggy parse", () => {
});

describe("units", () => {
testEvalToBe("100%", '1, with params numberFormat: ".2~p"');
testEvalToBe("100%", '1, with tags {numberFormat: ".2~p"}');
testEvalToBe("1-0%", "1");
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("Peggy to Expression", () => {
});

describe("dicts", () => {
testToExpression("{a: 1, b: 2}", '{"a": 1, "b": 2}', "{a: 1,b: 2}");
testToExpression("{a: 1, b: 2}", '{"a": 1, "b": 2}', "{a: 1, b: 2}");
testToExpression("{1+0: 1, 2+0: 2}", "{(add)(1, 0): 1, (add)(2, 0): 2}"); // key can be any expression
testToExpression("dict.property", "Error(dict is not defined)");
testToExpression(
Expand Down
2 changes: 1 addition & 1 deletion packages/squiggle-lang/__tests__/library/danger_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Danger functions", () => {
testEvalToBe("Danger.json([1,2,3])", "[1,2,3]");
testEvalToBe(
"Danger.json({foo: 'bar'})",
'{vtype: "Dict",value: {foo: "bar"}}'
'{vtype: "Dict", value: {foo: "bar"}}'
);
testEvalToBe("Danger.jsonString([1,2,3])", '"[1,2,3]"');
testEvalToBe(
Expand Down
30 changes: 15 additions & 15 deletions packages/squiggle-lang/__tests__/library/dict_test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import { testEvalToBe } from "../helpers/reducerHelpers.js";

describe("Dict", () => {
testEvalToBe("Dict.set({a: 1, b: 2}, 'c', 3)", "{a: 1,b: 2,c: 3}");
testEvalToBe("d={a: 1, b: 2}; _=Dict.set(d, 'c', 3); d", "{a: 1,b: 2}"); // Immutable
testEvalToBe("Dict.set({a: 1, b: 2}, 'c', 3)", "{a: 1, b: 2, c: 3}");
testEvalToBe("d={a: 1, b: 2}; _=Dict.set(d, 'c', 3); d", "{a: 1, b: 2}"); // Immutable
testEvalToBe(
"Dict.merge({a: 1, b: 2}, {b: 3, c: 4, d: 5})",
"{a: 1,b: 3,c: 4,d: 5}"
"{a: 1, b: 3, c: 4, d: 5}"
);
testEvalToBe(
"Dict.mergeMany([{a: 1, b: 2}, {c: 3, d: 4}, {c: 5, e: 6}])",
"{a: 1,b: 2,c: 5,d: 4,e: 6}"
"{a: 1, b: 2, c: 5, d: 4, e: 6}"
);
testEvalToBe("Dict.keys({a: 1, b: 2})", '["a","b"]');
testEvalToBe("Dict.values({a: 1, b: 2})", "[1,2]");
testEvalToBe("Dict.toList({a: 1, b: 2})", '[["a",1],["b",2]]');
testEvalToBe("Dict.fromList([['a', 1], ['b', 2]])", "{a: 1,b: 2}");
testEvalToBe("Dict.map({a: 1, b: 2}, {|x| x * 2})", "{a: 2,b: 4}");
testEvalToBe("Dict.fromList([['a', 1], ['b', 2]])", "{a: 1, b: 2}");
testEvalToBe("Dict.map({a: 1, b: 2}, {|x| x * 2})", "{a: 2, b: 4}");
testEvalToBe(
"Dict.mapKeys({a: 1, b: 2}, {|x| concat(x, 'hi')})",
"{ahi: 1,bhi: 2}"
"{ahi: 1, bhi: 2}"
);

describe("Dict.pick", () => {
testEvalToBe('Dict.pick({a: 1,b: 2,c: 3}, ["a", "b"])', "{a: 1,b: 2}");
testEvalToBe('Dict.pick({a: 1,b: 2,c: 3}, ["a", "d"])', "{a: 1}");
testEvalToBe('Dict.pick({a: 1,b: 2,c: 3}, ["d", "e"])', "{}");
testEvalToBe("Dict.pick({a: 1,b: 2,c: 3}, [])", "{}");
testEvalToBe('Dict.pick({a: 1, b: 2, c: 3}, ["a", "b"])', "{a: 1, b: 2}");
testEvalToBe('Dict.pick({a: 1, b: 2, c: 3}, ["a", "d"])', "{a: 1}");
testEvalToBe('Dict.pick({a: 1, b: 2, c: 3}, ["d", "e"])', "{}");
testEvalToBe("Dict.pick({a: 1, b: 2, c: 3}, [])", "{}");
});

describe("Dict.omit", () => {
testEvalToBe('Dict.omit({a: 1,b: 2,c: 3}, ["a", "b"])', "{c: 3}");
testEvalToBe('Dict.omit({a: 1,b: 2,c: 3}, ["a", "d"])', "{b: 2,c: 3}");
testEvalToBe('Dict.omit({a: 1, b: 2, c: 3}, ["a", "b"])', "{c: 3}");
testEvalToBe('Dict.omit({a: 1, b: 2, c: 3}, ["a", "d"])', "{b: 2, c: 3}");
testEvalToBe(
'Dict.omit({a: 1, b: 2, c: 3}, ["d", "e"])',
"{a: 1,b: 2,c: 3}"
"{a: 1, b: 2, c: 3}"
);
testEvalToBe("Dict.omit({a: 1,b: 2,c: 3}, [])", "{a: 1,b: 2,c: 3}");
testEvalToBe("Dict.omit({a: 1, b: 2, c: 3}, [])", "{a: 1, b: 2, c: 3}");
});
});
6 changes: 3 additions & 3 deletions packages/squiggle-lang/__tests__/library/list_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ describe("List functions", () => {
);
testEvalToBe(
"arr=[{a: 1, b: 2}, {a: 1, b: 3}, {a:2, b:5}]; List.uniqBy(arr, {|e| e.a})",
"[{a: 1,b: 2},{a: 2,b: 5}]"
"[{a: 1, b: 2},{a: 2, b: 5}]"
);
testEvalToBe(
"arr=[{a: normal(5,2), b: 2}, {a: 1, b: 3}, {a:2, b:5}]; List.uniqBy(arr, {|e| e.a})",
"[{a: Sample Set Distribution,b: 2},{a: 1,b: 3},{a: 2,b: 5}]"
"[{a: Sample Set Distribution, b: 2},{a: 1, b: 3},{a: 2, b: 5}]"
);
testEvalToBe(
"arr=[{a: normal(5,2), b: 2}, {a: 1, b: 3}, {a:2, b:3}]; List.uniqBy(arr, {|e| e.b})",
"[{a: Sample Set Distribution,b: 2},{a: 1,b: 3}]"
"[{a: Sample Set Distribution, b: 2},{a: 1, b: 3}]"
);
});

Expand Down
7 changes: 4 additions & 3 deletions packages/squiggle-lang/__tests__/library/tag_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { testEvalToBe } from "../helpers/reducerHelpers.js";

describe("Tags", () => {
testEvalToBe("123 -> Tag.name('')", "123");
describe("name", () => {
testEvalToBe("123 -> Tag.name('myNumber') -> Tag.getName", '"myNumber"');
});
Expand All @@ -12,7 +13,7 @@ describe("Tags", () => {
describe("all", () => {
testEvalToBe(
"123 -> Tag.name('myName') -> Tag.doc('myDoc') -> Tag.getAll",
'{name: "myName",doc: "myDoc"}'
'{name: "myName", doc: "myDoc"}'
);
});

Expand Down Expand Up @@ -59,7 +60,7 @@ x = 5

x
`,
'5, with params name: "five"'
'5, with tags {name: "five"}'
);

testEvalToBe(
Expand All @@ -70,7 +71,7 @@ x = 5

x
`,
'5, with params name: "five", doc: "This is five"'
'5, with tags {name: "five", doc: "This is five"}'
);

testEvalToBe(
Expand Down