Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
remove causeTraverse
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Dec 3, 2023
1 parent f1d79b6 commit 78fc076
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 44 deletions.
53 changes: 12 additions & 41 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3372,8 +3372,8 @@ const optionFrom = <I, A>(value: Schema<I, A>): Schema<OptionFrom<I>, OptionFrom
})
)

const optionDecode = <A>(o: OptionFrom<A>): Option.Option<A> =>
o._tag === "None" ? Option.none() : Option.some(o.value)
const optionDecode = <A>(input: OptionFrom<A>): Option.Option<A> =>
input._tag === "None" ? Option.none() : Option.some(input.value)

const optionArbitrary = <A>(value: Arbitrary<A>): Arbitrary<Option.Option<A>> => {
const placeholder = lazy<A>(() => any).pipe(annotations({
Expand Down Expand Up @@ -3476,8 +3476,8 @@ const eitherFrom = <IE, E, IA, A>(
})
)

const eitherDecode = <E, A>(e: EitherFrom<E, A>): Either.Either<E, A> =>
e._tag === "Left" ? Either.left(e.left) : Either.right(e.right)
const eitherDecode = <E, A>(input: EitherFrom<E, A>): Either.Either<E, A> =>
input._tag === "Left" ? Either.left(input.left) : Either.right(input.right)

const eitherArbitrary = <E, A>(
left: Arbitrary<E>,
Expand Down Expand Up @@ -4618,36 +4618,6 @@ const causeFrom = <EI, E>(error: Schema<EI, E>): Schema<CauseFrom<EI>, CauseFrom
})
)
const causeTraverse = <E, E2>(
cause: Cause.Cause<E>,
parseError: (e: E, options: ParseOptions) => ParseResult.ParseResult<E2>,
options: ParseOptions
): ParseResult.ParseResult<Cause.Cause<E2>> => {
if (cause._tag === "Fail") {
return ParseResult.map(parseError(cause.error, options), Cause.fail)
} else if (cause._tag === "Parallel") {
return ParseResult.flatMap(
causeTraverse(cause.left, parseError, options),
(left) =>
ParseResult.map(
causeTraverse(cause.right, parseError, options),
(right) => Cause.parallel(left, right)
)
)
} else if (cause._tag === "Sequential") {
return ParseResult.flatMap(
causeTraverse(cause.left, parseError, options),
(left) =>
ParseResult.map(
causeTraverse(cause.right, parseError, options),
(right) => Cause.sequential(left, right)
)
)
}
return ParseResult.succeed(cause)
}
const causeArbitrary = <E>(error: Arbitrary<E>): Arbitrary<Cause.Cause<E>> => {
const placeholder = lazy<E>(() => any).pipe(annotations({
[hooks.ArbitraryHookId]: () => error
Expand Down Expand Up @@ -4682,17 +4652,17 @@ const causePretty = <E>(error: Pretty<E>): Pretty<Cause.Cause<E>> => (cause) =>
*/
export const causeFromSelf = <IE, E>(
error: Schema<IE, E>
): Schema<Cause.Cause<IE>, Cause.Cause<E>> =>
declare(
): Schema<Cause.Cause<IE>, Cause.Cause<E>> => {
return declare(
[error],
causeFrom(error),
(isDecoding, error) => {
const parseError = isDecoding ? Parser.parse(error) : Parser.encode(error)
const parse = isDecoding ? Parser.parse(causeFrom(error)) : Parser.encode(causeFrom(error))
return (u, options, ast) => {
if (!Cause.isCause(u)) {
return ParseResult.fail(ParseResult.type(ast, u))
if (Cause.isCause(u)) {
return ParseResult.map(parse(causeEncode(u), options), causeDecode)
}
return causeTraverse(u, parseError, options)
return ParseResult.fail(ParseResult.type(ast, u))
}
},
{
Expand All @@ -4702,6 +4672,7 @@ export const causeFromSelf = <IE, E>(
[hooks.EquivalenceHookId]: () => Equal.equals
}
)
}
function causeDecode<E>(cause: CauseFrom<E>): Cause.Cause<E> {
switch (cause._tag) {
Expand All @@ -4725,7 +4696,7 @@ function causeEncode<E>(cause: Cause.Cause<E>): CauseFrom<E> {
case "Empty":
return { _tag: "Empty" }
case "Die":
return { _tag: "Die", defect: Cause.pretty(cause) }
return { _tag: "Die", defect: cause.defect }
case "Interrupt":
return { _tag: "Interrupt", fiberId: cause.fiberId }
case "Fail":
Expand Down
22 changes: 19 additions & 3 deletions test/Cause/cause.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ describe("Cause/cause", () => {
},
Cause.interrupt(FiberId.composite(FiberId.runtime(1, 1000), FiberId.none))
)

await Util.expectParseFailure(
schema,
null,
`Expected <anonymous type literal schema>, actual null`
)
await Util.expectParseFailure(
schema,
{},
`/_tag is missing`
)
await Util.expectParseFailure(
schema,
{ _tag: "Parallel", left: { _tag: "Fail" }, right: { _tag: "Interrupt" } },
`union member: /left union member: /error is missing`
)
})

it("encoding", async () => {
Expand All @@ -82,7 +98,7 @@ describe("Cause/cause", () => {
})
await Util.expectEncodeSuccess(schema, Cause.die("fail"), {
_tag: "Die",
defect: "Error: fail"
defect: "fail"
})
await Util.expectEncodeSuccess(
schema,
Expand All @@ -105,7 +121,7 @@ describe("Cause/cause", () => {

const failWithStack = S.encodeSync(schema)(Cause.die(new Error("fail")))
assert(failWithStack._tag === "Die")
assert.include(failWithStack.defect, "Error: fail")
assert.include(failWithStack.defect, "cause.test.ts")
assert.include(String(failWithStack.defect), "Error: fail")
assert.include(String((failWithStack.defect as any).stack), "cause.test.ts")
})
})
10 changes: 10 additions & 0 deletions test/Cause/causeFromSelf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ describe("Cause/causeFromSelf", () => {
await Util.expectParseSuccess(schema, Cause.fail("1"), Cause.fail(1))

await Util.expectParseFailure(schema, null, `Expected Cause, actual null`)
await Util.expectParseFailure(
schema,
Cause.fail("a"),
`union member: /error Expected string <-> number, actual "a"`
)
await Util.expectParseFailure(
schema,
Cause.parallel(Cause.die("error"), Cause.fail("a")),
`union member: /right union member: /error Expected string <-> number, actual "a"`
)
})

it("encoding", async () => {
Expand Down

0 comments on commit 78fc076

Please sign in to comment.