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

add Serializable module #609

Merged
merged 39 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
8a7f73c
add Serializable module
tim-smart Nov 29, 2023
a91f269
docs
tim-smart Nov 29, 2023
ae554fe
allow From types in Serializable
tim-smart Nov 29, 2023
7d231b5
seperate WithResult
tim-smart Nov 29, 2023
7b47e92
add serializable / deserialize functions
tim-smart Nov 29, 2023
20c2cfa
update serialize signature
tim-smart Nov 29, 2023
5c2b03c
extend Request
Nov 29, 2023
98d4e9a
add Schema for Cause
tim-smart Nov 30, 2023
bbae836
add Exit schema
tim-smart Nov 30, 2023
4b91650
changesets
tim-smart Nov 30, 2023
305b8e8
replace SerializableWithResult with SerializableWithExit
tim-smart Nov 30, 2023
6017e7c
add Serializable tests
tim-smart Nov 30, 2023
9d568dd
update doc categories
tim-smart Nov 30, 2023
8ef8d4a
keep failure and success schemas separate
tim-smart Nov 30, 2023
b06e365
traverse Cause with ParseResult
Nov 30, 2023
6cdc60f
add generics to Serializable
Nov 30, 2023
887f784
toggle types
Nov 30, 2023
4991033
rename fiberId to fiberId, fiberIdFromSelf to FiberIdFromSelf (standard)
gcanti Nov 30, 2023
77d015b
move new test files
gcanti Nov 30, 2023
8130105
add basic FiberId test files
gcanti Nov 30, 2023
8fecf68
move tests files
gcanti Nov 30, 2023
6c3ae8a
chore
gcanti Nov 30, 2023
51c2724
remove some casts
gcanti Nov 30, 2023
6c73266
add OptionFrom, EitherFrom
gcanti Nov 30, 2023
4e5b0a5
move to calls to the right
gcanti Nov 30, 2023
27272b1
use transform for FiberId
tim-smart Nov 30, 2023
b590a5e
move Serializable generics right
tim-smart Dec 1, 2023
b648c07
simplify fiberIdArbitrary
gcanti Dec 3, 2023
e8be3c0
simplify causeArbitrary
gcanti Dec 3, 2023
7f1aa04
refactor exitArbitrary
gcanti Dec 3, 2023
cbeca3b
update docs
gcanti Dec 3, 2023
f1d79b6
more tests
gcanti Dec 3, 2023
78fc076
remove causeTraverse
gcanti Dec 3, 2023
7bd36db
fix Request error type
tim-smart Dec 3, 2023
3d9cbb5
docs
tim-smart Dec 3, 2023
f7ece0b
add optional defect schema to exit / cause
tim-smart Dec 4, 2023
0992baa
docs
tim-smart Dec 4, 2023
dcf703a
chore
gcanti Dec 4, 2023
a080797
typo
gcanti Dec 4, 2023
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
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 }
gcanti marked this conversation as resolved.
Show resolved Hide resolved
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
Loading