-
Notifications
You must be signed in to change notification settings - Fork 13
fix: loading of examples #92
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule examples
updated
46 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { LoadExamples } from "./examples"; | ||
|
|
||
| describe("LoadExamples", () => { | ||
| it("should correctly parse and return examples and skip those that are missing files", () => { | ||
| const examples = LoadExamples(); | ||
|
|
||
| expect(examples).toHaveLength(6); | ||
|
|
||
| examples.forEach(function (example) { | ||
| expect(example.id).not.toBeNull(); | ||
| expect(example.title).not.toBeNull(); | ||
| expect(example.subtitle).not.toBeNull(); | ||
| expect(example.data).not.toBeNull(); | ||
| expect(example.data.schema).not.toBeNull(); | ||
| expect(example.data.relationships).not.toBeNull(); | ||
| expect(example.data.assertions).not.toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,62 +1,71 @@ | ||
| import yaml from "yaml"; | ||
| import { sortBy, zip } from "es-toolkit"; | ||
| import { ParsedValidation } from "./validationfileformat"; | ||
| export interface Example { | ||
| id: string; | ||
| title: string; | ||
| documentation: string; | ||
| subtitle: string; | ||
| data: ParsedValidation; | ||
| } | ||
|
|
||
| const readmesImports = import.meta.glob("/examples/schemas/*/README.md", { | ||
| // Get just the raw text | ||
| query: "?raw", | ||
| eager: true, | ||
| }); | ||
| const schemasImports = import.meta.glob( | ||
| "/examples/schemas/*/schema-and-data.yaml", | ||
| { | ||
| // Get just the raw text | ||
| query: "?raw", | ||
| eager: true, | ||
| }, | ||
| ); | ||
|
|
||
| const isTextImport = (input: unknown): input is { default: string } => { | ||
| return typeof input === "object" && input !== null && "default" in input; | ||
| }; | ||
|
|
||
| const sortAndRealizeImports = async ( | ||
| imports: Record<string, () => Promise<unknown>>, | ||
| ) => { | ||
| const realizedImports = await Promise.all( | ||
| sortBy(Object.entries(imports), [0]).map(([, importThunk]) => | ||
| importThunk(), | ||
| ), | ||
| ); | ||
| return realizedImports | ||
| .filter(isTextImport) | ||
| .map(({ default: defaultExport }) => defaultExport); | ||
| }; | ||
|
|
||
| /** | ||
| * LoadExamples loads the examples defined statically and compiled in. | ||
| * LoadExamples loads the examples defined statically and compiled in the /examples repo. | ||
| * If one example is lacking the schema file, we don't include it. | ||
| */ | ||
| export async function LoadExamples(): Promise<Example[]> { | ||
| const names = Object.entries(readmesImports).map(([path]) => path); | ||
| names.sort(); | ||
| const sortedReadmes = await sortAndRealizeImports(readmesImports); | ||
| const sortedSchemas = await sortAndRealizeImports(schemasImports); | ||
| const zippedImports = zip(names, sortedReadmes, sortedSchemas); | ||
| return zippedImports.map(([name, readme, schema]) => { | ||
| const lines = readme.split("\n"); | ||
| const title = lines[0].trim().substring(1).trim(); // Strip the # for the markdown header | ||
| const subtitle = lines[2].trim(); | ||
| return { | ||
| id: name, | ||
| title, | ||
| subtitle, | ||
| documentation: readme, | ||
| data: yaml.parse(schema), | ||
| }; | ||
| }); | ||
| export function LoadExamples(): Example[] { | ||
| return Object.keys(readmesImports) | ||
| .map((path) => { | ||
| // Grab the id | ||
| const match = path.match(/\/examples\/schemas\/(.*)\/README\.md/); | ||
| return match ? match[1] : ""; | ||
| }) | ||
| .filter((name): name is string => { | ||
| if (name === "") return false; | ||
| // Skip examples that don't have the schema file | ||
| const schemaPath = `/examples/schemas/${name}/schema-and-data.yaml`; | ||
| return schemaPath in schemasImports; | ||
| }) | ||
| .sort() | ||
| .map((name) => { | ||
| const readmeModule = | ||
| readmesImports[`/examples/schemas/${name}/README.md`]; | ||
| const schemaModule = | ||
| schemasImports[`/examples/schemas/${name}/schema-and-data.yaml`]; | ||
|
|
||
| const readme = isTextImport(readmeModule) ? readmeModule.default : ""; | ||
| const schema = isTextImport(schemaModule) ? schemaModule.default : ""; | ||
|
|
||
| const lines = readme.split("\n"); | ||
| const title = lines[0].trim().substring(1).trim(); // Strip the # for the markdown header | ||
| const subtitle = lines[2].trim(); | ||
| let schemaRes: ParsedValidation; | ||
| try { | ||
| schemaRes = yaml.parse(schema); | ||
| } catch (e) { | ||
| throw new Error("error parsing schema for example " + name + ": " + e); | ||
| } | ||
| return { | ||
| id: name, | ||
| title: title, | ||
| subtitle: subtitle, | ||
| data: schemaRes, | ||
| }; | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
WHAT! I didn't realize this was valid syntax
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 didn't know either (i don't know typescript), this was written by Claude 😆
https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates