Skip to content
Closed
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
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,14 @@ jobs:
- name: Compiled ReScript matches its source
run: ../../scripts/assert-no-drift.sh '*.res.mjs'
working-directory: packages/e2e

# e2e holds every ppx schema to fixtures.json: what Serde reads and writes
# for the Rust type mirroring it. This keeps the file honest - it fails when a
# verdict moved, or when a @schema type in packages/e2e/src/ppx has no mirror.
serde-compat:
name: Serde mirror
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- run: cargo run --locked -- check
working-directory: packages/serde-compat
10 changes: 10 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@ that importing one message keeps none of its siblings in a Rollup 4 bundle.
A codec change a generated shape needs lands in `packages/sury/src` with its
spec first, like any other.

## Changing sury-ppx

Its output is held to Serde, not to itself. Every `@schema` type in
`packages/e2e/src/ppx` has a Rust mirror in `packages/serde-compat` - the type
Serde reads the same JSON into - and the e2e test file holds the schema to the
mirror's verdicts in `fixtures.json` with `assertSerdeCompatible`. A new type
gets a mirror, or a `skip` with its reason; `cargo run` there rewrites the
fixtures and `cargo run -- check` fails on a missing one. An input Sury reads
differently is a `.diverges` case with its reason, never a dropped one.

## Codecs

`CODEC_SPEC.md` is the normative statement of what conversions are legal,
Expand Down
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ npm run test:res
npm run test -- --watch
```

5. Hold it to Serde

The ppx reads and writes JSON the way Serde does. A `@schema` type added to
`packages/e2e/src/ppx` needs a Rust mirror in `packages/serde-compat`, and its
test file lists the schema in `assertSerdeCompatible`:

```
cd packages/serde-compat
cargo run # rewrites fixtures.json
cargo run -- check # what CI runs
```

## Make comparison

`pnpm benchmarks --write` regenerates `docs/benchmarks/*.md`, which is where the
Expand Down
44 changes: 43 additions & 1 deletion packages/e2e/src/ppx/Ppx_Example_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,35 @@ test("Main example", t => {
]),
),
deprecatedAgeRestriction: s.matches(
S.option(S.int)->S.meta({description: "Use rating instead", deprecated: true}),
S.union([S.literal(Null.null)->S.to(S.literal(None)), S.option(S.int)])->S.meta({
description: "Use rating instead",
deprecated: true,
}),
),
}),
)
t->Assert.deepEqual(
%raw(`{"Id": 1, "Title": "My first film", "Rating": "R", "Age": null}`)->S.parseOrThrow(
~to=filmSchema,
),
{
id: 1.,
title: "My first film",
tags: [],
rating: Restricted,
deprecatedAgeRestriction: None,
},
)
t->Assert.deepEqual(
{
id: 2.,
title: "Sad & sed",
tags: ["Loved"],
rating: ParentalStronglyCautioned,
deprecatedAgeRestriction: None,
}->S.convertOrThrow(~from=filmSchema, ~to=S.unknown),
%raw(`{"Id": 2, "Title": "Sad & sed", "Tags": ["Loved"], "Rating": "PG13", "Age": null}`),
)
})

let myCustomSchema = S.string->S.maxLength(5)
Expand Down Expand Up @@ -118,3 +143,20 @@ test("@s.description", t => {
S.string->S.meta({description: "A useful bit of text, if you know what to do with it."}),
)
})

assertSerdeCompatible(
"Example",
[
("rating", ratingSchema->S.castToUnknown),
("film", filmSchema->S.castToUnknown),
("matches", matchesSchema->S.castToUnknown),
("default", defaultSchema->S.castToUnknown),
("defaultWith", defaultWithSchema->S.castToUnknown),
("null", nullSchema->S.castToUnknown),
("nullWithDefault", nullWithDefaultSchema->S.castToUnknown),
("nullable", nullableSchema->S.castToUnknown),
("nullableWithDefault", nullableWithDefaultSchema->S.castToUnknown),
("deprecated", deprecatedSchema->S.castToUnknown),
("describe", describeSchema->S.castToUnknown),
],
)
125 changes: 98 additions & 27 deletions packages/e2e/src/ppx/Ppx_Example_test.res.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as S from "sury/src/S.res.mjs";
import * as U from "../utils/U.res.mjs";
import * as Sury from "sury";
import * as Vitest from "vitest";
import * as Vitest from "../utils/Vitest.res.mjs";
import * as Vitest$1 from "vitest";

let ratingSchema = Sury.union([
Sury.literal("G"),
Expand All @@ -16,66 +18,88 @@ let filmSchema = Sury.$schema(s => ({
Title: s.m(Sury.string),
Tags: s.m(Sury.$Option_getOr(Sury.$option(Sury.array(Sury.string)), [])),
Rating: s.m(ratingSchema),
Age: s.m(Sury.meta(Sury.$option(Sury.int32), {
Age: s.m(Sury.meta(Sury.union([
S.to(Sury.literal(null), Sury.literal(undefined), undefined),
Sury.$option(Sury.int32)
]), {
description: "Use rating instead",
deprecated: true
}))
}));

Vitest.test("Main example", t => U.assertEqualSchemas(t, filmSchema, Sury.$schema(s => ({
Id: s.m(Sury.number),
Title: s.m(Sury.string),
Tags: s.m(Sury.$Option_getOr(Sury.$option(Sury.array(Sury.string)), [])),
Rating: s.m(Sury.union([
Sury.literal("G"),
Sury.literal("PG"),
Sury.literal("PG13"),
Sury.literal("R")
])),
Age: s.m(Sury.meta(Sury.$option(Sury.int32), {
description: "Use rating instead",
deprecated: true
}))
})), undefined));
Vitest$1.test("Main example", t => {
U.assertEqualSchemas(t, filmSchema, Sury.$schema(s => ({
Id: s.m(Sury.number),
Title: s.m(Sury.string),
Tags: s.m(Sury.$Option_getOr(Sury.$option(Sury.array(Sury.string)), [])),
Rating: s.m(Sury.union([
Sury.literal("G"),
Sury.literal("PG"),
Sury.literal("PG13"),
Sury.literal("R")
])),
Age: s.m(Sury.meta(Sury.union([
S.to(Sury.literal(null), Sury.literal(undefined), undefined),
Sury.$option(Sury.int32)
]), {
description: "Use rating instead",
deprecated: true
}))
})), undefined);
Vitest.Assert.deepEqual(t, Sury.parseOrThrow({"Id": 1, "Title": "My first film", "Rating": "R", "Age": null}, filmSchema), {
Id: 1,
Title: "My first film",
Tags: [],
Rating: "R",
Age: undefined
}, undefined);
Vitest.Assert.deepEqual(t, S.convertOrThrow({
Id: 2,
Title: "Sad & sed",
Tags: ["Loved"],
Rating: "PG13",
Age: undefined
}, filmSchema, undefined, Sury.unknown), {"Id": 2, "Title": "Sad & sed", "Tags": ["Loved"], "Rating": "PG13", "Age": null}, undefined);
});

let myCustomSchema = Sury.maxLength(Sury.string, 5);

Vitest.test("@s.matches", t => U.assertEqualSchemas(t, myCustomSchema, myCustomSchema, undefined));
Vitest$1.test("@s.matches", t => U.assertEqualSchemas(t, myCustomSchema, myCustomSchema, undefined));

let urlSchema = Sury.url;

Vitest.test("Format schema as a type", t => U.assertEqualSchemas(t, urlSchema, Sury.url, undefined));
Vitest$1.test("Format schema as a type", t => U.assertEqualSchemas(t, urlSchema, Sury.url, undefined));

let defaultSchema = Sury.$Option_getOr(Sury.$option(Sury.string), "Unknown");

Vitest.test("@s.default", t => U.assertEqualSchemas(t, defaultSchema, Sury.$Option_getOr(Sury.$option(Sury.string), "Unknown"), undefined));
Vitest$1.test("@s.default", t => U.assertEqualSchemas(t, defaultSchema, Sury.$Option_getOr(Sury.$option(Sury.string), "Unknown"), undefined));

let defaultWithSchema = Sury.$Option_getOrWith(Sury.$option(Sury.array(Sury.string)), () => []);

Vitest.test("@s.defaultWith", t => U.assertEqualSchemas(t, defaultWithSchema, Sury.$Option_getOrWith(Sury.$option(Sury.array(Sury.string)), () => []), undefined));
Vitest$1.test("@s.defaultWith", t => U.assertEqualSchemas(t, defaultWithSchema, Sury.$Option_getOrWith(Sury.$option(Sury.array(Sury.string)), () => []), undefined));

let nullSchema = Sury.$nullAsOption(Sury.string);

Vitest.test("@s.null", t => U.assertEqualSchemas(t, nullSchema, Sury.$nullAsOption(Sury.string), undefined));
Vitest$1.test("@s.null", t => U.assertEqualSchemas(t, nullSchema, Sury.$nullAsOption(Sury.string), undefined));

let nullWithDefaultSchema = Sury.$Option_getOr(Sury.$nullAsOption(Sury.string), "Unknown");

Vitest.test("@s.null with @s.default", t => U.assertEqualSchemas(t, nullWithDefaultSchema, Sury.$Option_getOr(Sury.$nullAsOption(Sury.string), "Unknown"), undefined));
Vitest$1.test("@s.null with @s.default", t => U.assertEqualSchemas(t, nullWithDefaultSchema, Sury.$Option_getOr(Sury.$nullAsOption(Sury.string), "Unknown"), undefined));

let nullableSchema = Sury.$nullableAsOption(Sury.string);

Vitest.test("@s.nullable", t => U.assertEqualSchemas(t, nullableSchema, Sury.$nullableAsOption(Sury.string), undefined));
Vitest$1.test("@s.nullable", t => U.assertEqualSchemas(t, nullableSchema, Sury.$nullableAsOption(Sury.string), undefined));

let nullableWithDefaultSchema = Sury.$Option_getOr(Sury.$nullableAsOption(Sury.string), "Unknown");

Vitest.test("@s.nullable with @s.default", t => U.assertEqualSchemas(t, nullableWithDefaultSchema, Sury.$Option_getOr(Sury.$nullableAsOption(Sury.string), "Unknown"), undefined));
Vitest$1.test("@s.nullable with @s.default", t => U.assertEqualSchemas(t, nullableWithDefaultSchema, Sury.$Option_getOr(Sury.$nullableAsOption(Sury.string), "Unknown"), undefined));

let deprecatedSchema = Sury.meta(Sury.string, {
description: "Will be removed in APIv2",
deprecated: true
});

Vitest.test("@s.deprecated", t => U.assertEqualSchemas(t, deprecatedSchema, Sury.meta(Sury.string, {
Vitest$1.test("@s.deprecated", t => U.assertEqualSchemas(t, deprecatedSchema, Sury.meta(Sury.string, {
description: "Will be removed in APIv2",
deprecated: true
}), undefined));
Expand All @@ -84,10 +108,57 @@ let describeSchema = Sury.meta(Sury.string, {
description: "A useful bit of text, if you know what to do with it."
});

Vitest.test("@s.description", t => U.assertEqualSchemas(t, describeSchema, Sury.meta(Sury.string, {
Vitest$1.test("@s.description", t => U.assertEqualSchemas(t, describeSchema, Sury.meta(Sury.string, {
description: "A useful bit of text, if you know what to do with it."
}), undefined));

U.assertSerdeCompatible("Example", [
[
"rating",
ratingSchema
],
[
"film",
filmSchema
],
[
"matches",
myCustomSchema
],
[
"default",
defaultSchema
],
[
"defaultWith",
defaultWithSchema
],
[
"null",
nullSchema
],
[
"nullWithDefault",
nullWithDefaultSchema
],
[
"nullable",
nullableSchema
],
[
"nullableWithDefault",
nullableWithDefaultSchema
],
[
"deprecated",
deprecatedSchema
],
[
"describe",
describeSchema
]
]);

let matchesSchema = myCustomSchema;

export {
Expand Down
23 changes: 20 additions & 3 deletions packages/e2e/src/ppx/Ppx_Format_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ test("Record of formats", t => {
id: s.matches(S.uuid),
email: s.matches(S.email),
tags: s.matches(S.array(S.string)->S.nonEmpty),
homepage: s.matches(S.option(S.uri)),
homepage: s.matches(S.union([S.literal(Null.null)->S.to(S.literal(None)), S.option(S.uri)])),
}),
)
t->Assert.deepEqual(
%raw(`{id: "123e4567-e89b-12d3-a456-426614174000", email: "a@b.com", tags: ["x"]}`)
->S.parseOrThrow(~to=userSchema),
%raw(`{id: "123e4567-e89b-12d3-a456-426614174000", email: "a@b.com", tags: ["x"]}`)->S.parseOrThrow(
~to=userSchema,
),
{
id: Uuid("123e4567-e89b-12d3-a456-426614174000"),
email: Email("a@b.com"),
Expand Down Expand Up @@ -170,3 +171,19 @@ test("Record of network formats", t => {
},
)
})

assertSerdeCompatible(
"Format",
[
("myEmail", myEmailSchema->S.castToUnknown),
("myPort", myPortSchema->S.castToUnknown),
("myInteger", myIntegerSchema->S.castToUnknown),
("myJsonString", myJsonStringSchema->S.castToUnknown),
("myEnv", myEnvSchema->S.castToUnknown),
("myQueryString", myQueryStringSchema->S.castToUnknown),
("myNonEmptyTags", myNonEmptyTagsSchema->S.castToUnknown),
("user", userSchema->S.castToUnknown),
("identifiers", identifiersSchema->S.castToUnknown),
("host", hostSchema->S.castToUnknown),
],
)
Loading