Skip to content

Commit

Permalink
Removed s.failWithError in favor of S.Error.raise
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed May 11, 2024
1 parent 08cebfd commit cad31f0
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_V7.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
- More flexible
- Improved tree-shaking
- Can get the info from the `tagged` type: `JSON` -> `JSON({validated: bool})`
- Removed `s.failWithError`. Use `S.Error.raise` instead
1 change: 0 additions & 1 deletion IDEAS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ let trimContract: S.contract<string => string> = S.contract(s => {
- stop reallocate objects without transformations
- Make S.serializeToJsonString super fast
- Make operations more treeshakable by starting passing the actual operation to the initialOperation function. Or add a condition (verify performance)
- Remove `s.failWithError` since there's `Error.raise` 🤔
- Turn `String.email` -> `email`, `String.min` -> `stringMin` for tree-shaking
- Rename `InvalidJsonStruct` error, since after `rescript-struct`->`rescript-schema` it became misleading
- Add S.bigint
Expand Down
12 changes: 3 additions & 9 deletions docs/rescript-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -892,24 +892,18 @@ You can also define your own custom schema factories that are specific to your a

```rescript
let nullableSchema = innerSchema => {
S.custom("Nullable", s => {
S.custom("Nullable", _ => {
parser: unknown => {
if unknown === %raw(`undefined`) || unknown === %raw(`null`) {
None
} else {
switch unknown->S.parseAnyWith(innerSchema) {
| Ok(value) => Some(value)
| Error(error) => s.failWithError(error)
}
Some(unknown->S.parseAnyOrRaiseWith(innerSchema))
}
},
serializer: value => {
switch value {
| Some(innerValue) =>
switch innerValue->S.serializeToUnknownWith(innerSchema) {
| Ok(value) => value
| Error(error) => s.failWithError(error)
}
innerValue->S.serializeToUnknownOrRaiseWith(innerSchema)
| None => %raw(`null`)
}
},
Expand Down
13 changes: 3 additions & 10 deletions packages/tests/src/core/S_custom_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@ open Ava
open RescriptCore

let nullableSchema = innerSchema => {
S.custom("Nullable", s => {
S.custom("Nullable", _ => {
parser: unknown => {
if unknown === %raw(`undefined`) || unknown === %raw(`null`) {
None
} else {
switch unknown->S.parseAnyWith(innerSchema) {
| Ok(value) => Some(value)
| Error(error) => s.failWithError(error)
}
Some(unknown->S.parseAnyOrRaiseWith(innerSchema))
}
},
serializer: value => {
switch value {
| Some(innerValue) =>
switch innerValue->S.serializeToUnknownWith(innerSchema) {
| Ok(value) => value
| Error(error) => s.failWithError(error)
}
| Some(innerValue) => innerValue->S.serializeToUnknownOrRaiseWith(innerSchema)
| None => %raw(`null`)
}
},
Expand Down
14 changes: 7 additions & 7 deletions packages/tests/src/core/S_failWithError_test.res
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
open Ava

test("FIXME: Should keep operation of the error passed to advanced fail", t => {
test("Keeps operation of the error passed to S.Error.raise", t => {
let schema = S.array(
S.string->S.transform(s => {
S.string->S.transform(_ => {
parser: _ =>
s.failWithError(
S.Error.raise(
U.error({
code: OperationFailed("User error"),
operation: Serializing,
Expand All @@ -18,7 +18,7 @@ test("FIXME: Should keep operation of the error passed to advanced fail", t => {
["Hello world!"]->S.parseAnyWith(schema),
{
code: OperationFailed("User error"),
operation: Parsing,
operation: Serializing,
path: S.Path.fromArray(["0", "a", "b"]),
},
)
Expand All @@ -30,11 +30,11 @@ test("Works with failing outside of the parser", t => {
"root",
S.array(
S.string->S.transform(
s =>
s.failWithError(
_ =>
S.Error.raise(
U.error({
code: OperationFailed("User error"),
operation: Serializing,
operation: Parsing,
path: S.Path.fromArray(["a", "b"]),
}),
),
Expand Down
14 changes: 7 additions & 7 deletions packages/tests/src/core/S_transform_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ test("Fails to parse when user raises error in a Transformed Primitive parser",
)
})

test("Uses the path from failWithError called in the transform parser", t => {
test("Uses the path from S.Error.raise called in the transform parser", t => {
let schema = S.array(
S.string->S.transform(s => {
S.string->S.transform(_ => {
parser: _ =>
s.failWithError(
S.Error.raise(
U.error({
code: OperationFailed("User error"),
operation: Parsing,
Expand All @@ -71,14 +71,14 @@ test("Uses the path from failWithError called in the transform parser", t => {
)
})

test("Uses the path from failWithError called in the transform serializer", t => {
test("Uses the path from S.Error.raise called in the transform serializer", t => {
let schema = S.array(
S.string->S.transform(s => {
S.string->S.transform(_ => {
serializer: _ =>
s.failWithError(
S.Error.raise(
U.error({
code: OperationFailed("User error"),
operation: Parsing,
operation: Serializing,
path: S.Path.fromArray(["a", "b"]),
}),
),
Expand Down
2 changes: 0 additions & 2 deletions src/S.resi
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ type exn += private Raised(error)
type s<'value> = {
schema: t<'value>,
fail: 'a. (string, ~path: Path.t=?) => 'a,
failWithError: 'a. error => 'a,
}

module Error: {
Expand Down Expand Up @@ -126,7 +125,6 @@ module Catch: {
@as("i") input: unknown,
@as("s") schema: t<'value>,
@as("f") fail: 'a. (string, ~path: Path.t=?) => 'a,
@as("w") failWithError: 'a. error => 'a,
}
}
let catch: (t<'value>, Catch.s<'value> => 'value) => t<'value>
Expand Down
6 changes: 0 additions & 6 deletions src/S_Core.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ function make(selfSchema, path, operation) {
TAG: "OperationFailed",
_0: message
}, operation, path + customPath);
}),
failWithError: (function (error) {
throw new RescriptSchemaError(error.code, operation, path + error.path);
})
};
}
Expand Down Expand Up @@ -2584,9 +2581,6 @@ function $$catch(schema, getFallbackValue) {
TAG: "OperationFailed",
_0: message
}, b.o, path + customPath);
}),
w: (function (error) {
throw new RescriptSchemaError(error.code, b.o, path + error.path);
})
});
}) - 1) + "](" + inputVar + "," + errorVar + ")";
Expand Down
12 changes: 0 additions & 12 deletions src/S_Core.res
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,11 @@ module InternalError = {
type s<'value> = {
schema: t<'value>,
fail: 'a. (string, ~path: Path.t=?) => 'a,
failWithError: 'a. error => 'a,
}

module EffectCtx = {
let make = (~selfSchema, ~path, ~operation) => {
schema: selfSchema->castUnknownSchemaToAnySchema,
failWithError: (error: error) => {
InternalError.raise(~path=path->Path.concat(error.path), ~code=error.code, ~operation)
},
fail: (message, ~path as customPath=Path.empty) => {
InternalError.raise(
~path=path->Path.concat(customPath),
Expand Down Expand Up @@ -3346,7 +3342,6 @@ module Catch = {
@as("i") input: unknown,
@as("s") schema: t<'value>,
@as("f") fail: 'a. (string, ~path: Path.t=?) => 'a,
@as("w") failWithError: 'a. error => 'a,
}
}
let catch = (schema, getFallbackValue) => {
Expand All @@ -3362,13 +3357,6 @@ let catch = (schema, getFallbackValue) => {
Catch.input,
error: internalError,
schema: selfSchema->castUnknownSchemaToAnySchema,
failWithError: (error: error) => {
InternalError.raise(
~path=path->Path.concat(error.path),
~code=error.code,
~operation=b.operation,
)
},
fail: (message, ~path as customPath=Path.empty) => {
InternalError.raise(
~path=path->Path.concat(customPath),
Expand Down
2 changes: 0 additions & 2 deletions src/S_Core.resi
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ type exn += private Raised(error)
type s<'value> = {
schema: t<'value>,
fail: 'a. (string, ~path: Path.t=?) => 'a,
failWithError: 'a. error => 'a,
}

module Error: {
Expand Down Expand Up @@ -126,7 +125,6 @@ module Catch: {
@as("i") input: unknown,
@as("s") schema: t<'value>,
@as("f") fail: 'a. (string, ~path: Path.t=?) => 'a,
@as("w") failWithError: 'a. error => 'a,
}
}
let catch: (t<'value>, Catch.s<'value> => 'value) => t<'value>
Expand Down

1 comment on commit cad31f0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: cad31f0 Previous: 9fb2e69 Ratio
Parse string 844706399 ops/sec (±0.48%) 819064824 ops/sec (±0.11%) 0.97
Serialize string 844915339 ops/sec (±0.46%) 819779932 ops/sec (±0.08%) 0.97
Advanced object schema factory 437135 ops/sec (±1.41%) 431262 ops/sec (±0.44%) 0.99
Parse advanced object 48587704 ops/sec (±0.53%) 46671133 ops/sec (±0.42%) 0.96
Create and parse advanced object 35160 ops/sec (±0.89%) 34129 ops/sec (±0.58%) 0.97
Parse advanced strict object 22686856 ops/sec (±0.78%) 22262984 ops/sec (±0.24%) 0.98
Serialize advanced object 834685614 ops/sec (±0.46%) 807737293 ops/sec (±0.20%) 0.97

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.