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

S.compile #91

Merged
merged 2 commits into from
Sep 25, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions IDEAS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,32 @@ let trimContract: S.contract<string => string> = S.contract(s => {

## v9

- Add S.reverse
- Remove ...OrThrow from js/ts api
- Add mode/flags instead of operation
- Simplify caching by the mode/flag
- Add S./"~experimentalReverse"
- S.transform(s => {
s.reverse(input => input) // Or s.asyncReverse(input => Promise.resolve(input))
input => input
}) // or asyncTransform // Maybe format ?
- async serializing support
- Add S.unwrap
- Rename S.variant to something
- Rename serialize to convertReverse
- Add S.removeTypeValidation
- S.create / S.validate
- S.parseToJsonString
- Rename S.inline to S.toRescriptCode
- Add serializeToJsonString to js api
- Add S.bigint
- Fix reverse for object/tuple/variant/recursive

### Done

- Add S.compile

## v10

- Make S.serializeToJsonString super fast
- Add S.bigint
- Add S.promise

## v???
Expand Down
23 changes: 15 additions & 8 deletions packages/tests/src/benchmark/Benchmark.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,26 @@ S$RescriptSchema.$$Error.make({

console.timeEnd("S.Error.make");

run(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(new (Benchmark.default.Suite)(), "Parse string", (function () {
run(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(new (Benchmark.default.Suite)(), "Parse string", (function () {
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith("Hello world!", S$RescriptSchema.string);
};
})), "Serialize string", (function () {
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith("Hello world!", S$RescriptSchema.string);
return S$RescriptSchema.serializeOrRaiseWith("Hello world!", S$RescriptSchema.string);
};
})), "Serialize string", (function () {
return function () {
return S$RescriptSchema.serializeOrRaiseWith("Hello world!", S$RescriptSchema.string);
};
})).add("Advanced object schema factory", makeAdvancedObjectSchema), "Parse advanced object", (function () {
})).add("Advanced object schema factory", makeAdvancedObjectSchema), "Parse advanced object", (function () {
var schema = makeAdvancedObjectSchema();
var data = makeTestObject();
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
};
})), "Assert advanced object - compile", (function () {
var schema = makeAdvancedObjectSchema();
var data = makeTestObject();
var assertFn = S$RescriptSchema.compile(schema, "Any", "Assert", true);
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
assertFn(data);
};
})), "Assert advanced object", (function () {
var schema = makeAdvancedObjectSchema();
Expand Down
8 changes: 8 additions & 0 deletions packages/tests/src/benchmark/Benchmark.res
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ Suite.make()
data->S.parseAnyOrRaiseWith(schema)
}
})
->Suite.addWithPrepare("Assert advanced object - compile", () => {
let schema = makeAdvancedObjectSchema()
let data = makeTestObject()
let assertFn = schema->S.compile(~input=Any, ~output=Assert, ~typeValidation=true)
() => {
assertFn(data)
}
})
->Suite.addWithPrepare("Assert advanced object", () => {
let schema = makeAdvancedObjectSchema()
let data = makeTestObject()
Expand Down
17 changes: 6 additions & 11 deletions packages/tests/src/utils/U.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,30 @@ function unsafeAssertEqualSchemas(t, s1, s2, message) {
function assertCompiledCode(t, schema, op, code, message) {
var compiledCode;
if (op === "Assert") {
try {
S$RescriptSchema.assertOrRaiseWith(undefined, schema);
}
catch (exn){

}
compiledCode = (schema.assert.toString());
var fn = S$RescriptSchema.compile(schema, "Any", "Assert", true);
compiledCode = fn.toString();
} else if (op === "SerializeJson") {
try {
S$RescriptSchema.serializeOrRaiseWith(undefined, schema);
}
catch (exn$1){
catch (exn){

}
compiledCode = (schema.serializeToJsonOrThrow.toString());
} else if (op === "Serialize") {
try {
S$RescriptSchema.serializeToUnknownOrRaiseWith(undefined, schema);
}
catch (exn$2){
catch (exn$1){

}
compiledCode = (schema.serializeOrThrow.toString());
} else if (S$RescriptSchema.isAsyncParse(schema)) {
S$RescriptSchema.parseAsyncWith(undefined, schema);
compiledCode = (schema.a.toString());
} else {
S$RescriptSchema.parseAnyWith(undefined, schema);
compiledCode = (schema.parseOrThrow.toString());
var fn$1 = S$RescriptSchema.compile(schema, "Any", "Output", true);
compiledCode = fn$1.toString();
}
t.is(compiledCode, code, message !== undefined ? Caml_option.valFromOption(message) : undefined);
}
Expand Down
12 changes: 4 additions & 8 deletions packages/tests/src/utils/U.res
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,12 @@ let assertCompiledCode = (
let _ = %raw(`undefined`)->S.parseAsyncWith(schema)
%raw(`schema.a.toString()`)
} else {
let _ = %raw(`undefined`)->S.parseAnyWith(schema)
%raw(`schema.parseOrThrow.toString()`)
let fn = schema->S.compile(~input=Any, ~output=Output, ~typeValidation=true)
(fn->magic)["toString"]()
}
| #Assert =>
try {
let _ = %raw(`undefined`)->S.assertOrRaiseWith(schema)
} catch {
| _ => ()
}
%raw(`schema.assert.toString()`)
let fn = schema->S.compile(~input=Any, ~output=Assert, ~typeValidation=true)
(fn->magic)["toString"]()
| #Serialize => {
try {
let _ = %raw(`undefined`)->S.serializeToUnknownOrRaiseWith(schema)
Expand Down
3 changes: 3 additions & 0 deletions src/S.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ var refine = S_Core$RescriptSchema.refine;

var variant = S_Core$RescriptSchema.variant;

var compile = S_Core$RescriptSchema.compile;

var parseWith = S_Core$RescriptSchema.parseWith;

var parseAnyWith = S_Core$RescriptSchema.parseAnyWith;
Expand Down Expand Up @@ -210,6 +212,7 @@ export {
custom ,
refine ,
variant ,
compile ,
parseWith ,
parseAnyWith ,
parseJsonStringWith ,
Expand Down
20 changes: 20 additions & 0 deletions src/S.resi
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,26 @@ let refine: (t<'value>, s<'value> => 'value => unit) => t<'value>

let variant: (t<'value>, 'value => 'variant) => t<'variant>

type rec input<'input, 'computed> =
| Input: input<'input, 'input>
| Any: input<'input, 'any>
| Unknown: input<'input, unknown>
| Json: input<'input, Js.Json.t>
| JsonString: input<'input, string>
type rec output<'output, 'computed> =
| Output: output<'output, 'output>
| Unknown: output<'output, unknown>
| Assert: output<'output, unit>
| Json: output<'output, Js.Json.t>
| JsonString: output<'output, string>

let compile: (
t<'schemaOutput>,
~input: input<unknown, 'input>,
~output: output<'schemaOutput, 'output>,
~typeValidation: bool,
) => 'input => 'output

let parseWith: (Js.Json.t, t<'value>) => result<'value, error>

let parseAnyWith: ('any, t<'value>) => result<'value, error>
Expand Down
Loading
Loading