Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
runs-on: "depot-ubuntu-24.04-small"
steps:
- uses: "actions/checkout@v4"
with:
submodules: true
- uses: "authzed/action-spicedb@v1"
with:
version: "latest"
Expand Down
2 changes: 1 addition & 1 deletion examples
Submodule examples updated 46 files
+5 −4 .github/workflows/lint.yaml
+6 −0 .pre-commit-config.yaml
+3 −1 README.md
+31 −0 code-examples/go/expire/go.mod
+179 −0 code-examples/go/expire/go.sum
+57 −0 code-examples/go/expire/main.go
+9 −0 code-examples/go/expire/schema
+31 −0 code-examples/go/export-bulk/go.mod
+179 −0 code-examples/go/export-bulk/go.sum
+93 −0 code-examples/go/export-bulk/main.go
+4 −0 code-examples/java/materialize-client/.gitignore
+39 −0 code-examples/java/materialize-client/pom.xml
+75 −0 code-examples/java/materialize-client/src/main/java/com/authzed/LookupPermissionSetsStreamObserver.java
+151 −0 code-examples/java/materialize-client/src/main/java/com/authzed/MaterializeClient.java
+52 −0 code-examples/java/materialize-client/src/main/java/com/authzed/WatchPermissionSetsStreamObserver.java
+62 −0 data/retry/main.py
+44 −0 data/temporal_example/README.md
+22 −0 data/temporal_example/activities.py
+25 −0 data/temporal_example/compose/docker-compose.yml
+31 −0 data/temporal_example/data/postgres.py
+57 −0 data/temporal_example/data/spicedb.py
+10 −0 data/temporal_example/run_migrations.py
+24 −0 data/temporal_example/run_worker.py
+26 −0 data/temporal_example/run_workflow.py
+6 −0 data/temporal_example/shared.py
+40 −0 data/temporal_example/workflows.py
+553 −0 notebooks/rag/rag.ipynb
+86 −0 notebooks/rag/requirements.txt
+1 −0 observability/simple-datadog/.gitignore
+60 −0 observability/simple-datadog/README.md
+788 −0 observability/simple-datadog/all_metrics.txt
+25 −0 observability/simple-datadog/conf.d/openmetrics.d/conf.yaml
+5 −0 observability/simple-datadog/datadog.yaml
+86 −0 observability/simple-datadog/docker-compose.yml
+1 −0 observability/simple-datadog/placeholder.env
+1 −0 observability/simple-datadog/spicedb-dashboard.json
+12 −3 schemas/caveats/schema-and-data.yaml
+0 −1 schemas/google-iam/schema-and-data.yaml
+18 −0 schemas/multiple-validation-files/README.md
+200 −0 schemas/multiple-validation-files/schema.zed
+54 −0 schemas/multiple-validation-files/validations/admin-role.yaml
+32 −0 schemas/multiple-validation-files/validations/reader-role.yaml
+172 −117 spicedb-as-library/go.mod
+1,464 −264 spicedb-as-library/go.sum
+24 −17 spicedb-as-library/main.go
+5 −5 tracing/README.md
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"clsx": "^2.1.1",
"d3-scale-chromatic": "^2.0.0",
"dequal": "^2.0.2",
"es-toolkit": "^1.36.0",
"file-saver": "^2.0.5",
"file-select-dialog": "^1.5.4",
"line-column": "^1.0.2",
Expand Down
20 changes: 20 additions & 0 deletions src/spicedb-common/examples.test.ts
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();
});
});
});
77 changes: 43 additions & 34 deletions src/spicedb-common/examples.ts
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 => {
Copy link
Contributor

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

Copy link
Contributor Author

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

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,
};
});
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4833,11 +4833,6 @@ es-to-primitive@^1.3.0:
is-date-object "^1.0.5"
is-symbol "^1.0.4"

es-toolkit@^1.36.0:
version "1.36.0"
resolved "https://registry.yarnpkg.com/es-toolkit/-/es-toolkit-1.36.0.tgz#98a9ddba346cde1aeca099f9bc27c05838c68b9a"
integrity sha512-5lpkRpDELuTSeAL//Rcg5urg+K/yOD1BobJSiNeCc89snMqgrhckmj8jdljqraDbpREiXTNW311RN518eVHBng==

esbuild-android-64@0.14.47:
version "0.14.47"
resolved "https://registry.yarnpkg.com/esbuild-android-64/-/esbuild-android-64-0.14.47.tgz#ef95b42c67bcf4268c869153fa3ad1466c4cea6b"
Expand Down
Loading