Skip to content

Commit

Permalink
update Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Sep 12, 2024
1 parent 95b51be commit d716e18
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
1 change: 0 additions & 1 deletion packages/effect/src/Cause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ export interface Die extends Cause.Variance<never>, Equal.Equal, Pipeable, Inspe
export interface Interrupt extends Cause.Variance<never>, Equal.Equal, Pipeable, Inspectable {
readonly _tag: "Interrupt"
readonly fiberId: FiberId.FiberId
readonly originSpan: Span | undefined
}

/**
Expand Down
15 changes: 8 additions & 7 deletions packages/rpc/test/Router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { RpcResolver, RpcResolverNoStream, RpcRouter } from "@effect/rpc"
import * as Rpc from "@effect/rpc/Rpc"
import { Schema } from "@effect/schema"
import * as S from "@effect/schema/Schema"
import { it } from "@effect/vitest"
import * as Array from "effect/Array"
import * as Chunk from "effect/Chunk"
import * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import { flow, pipe } from "effect/Function"
import * as Stream from "effect/Stream"
import { assert, describe, expect, it, test } from "vitest"
import { assert, describe, expect, test } from "vitest"

interface Name {
readonly _: unique symbol
Expand Down Expand Up @@ -198,10 +199,10 @@ describe("Router", () => {
value: "Hello, John!"
}, {
_tag: "Failure",
cause: { _tag: "Fail", error: { _tag: "SomeError", message: "fail" } }
cause: { _tag: "Annotated", cause: { _tag: "Fail", error: { _tag: "SomeError", message: "fail" } } }
}, {
_tag: "Failure",
cause: { _tag: "Fail", error: { _tag: "SomeError", message: "fail" } }
cause: { _tag: "Annotated", cause: { _tag: "Fail", error: { _tag: "SomeError", message: "fail" } } }
}, {
_tag: "Success",
value: date.toISOString()
Expand Down Expand Up @@ -247,10 +248,10 @@ describe("Router", () => {
value: "Hello, John!"
}, {
_tag: "Failure",
cause: { _tag: "Fail", error: { _tag: "SomeError", message: "fail" } }
cause: { _tag: "Annotated", cause: { _tag: "Fail", error: { _tag: "SomeError", message: "fail" } } }
}, {
_tag: "Failure",
cause: { _tag: "Fail", error: { _tag: "SomeError", message: "fail" } }
cause: { _tag: "Annotated", cause: { _tag: "Fail", error: { _tag: "SomeError", message: "fail" } } }
}, {
_tag: "Success",
value: date.toISOString()
Expand Down Expand Up @@ -380,7 +381,7 @@ describe.each([{
])
}).pipe(Effect.runPromise))

test("stream fail", () =>
it.effect("stream fail", () =>
Effect.gen(function*(_) {
let n = 0
const result = yield* _(
Expand All @@ -395,5 +396,5 @@ describe.each([{
)
assert.strictEqual(n, 2)
assert.deepStrictEqual(result, new SomeError({ message: "fail" }))
}).pipe(Effect.runPromise))
}))
})
40 changes: 33 additions & 7 deletions packages/schema/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as cause_ from "effect/Cause"
import * as chunk_ from "effect/Chunk"
import * as config_ from "effect/Config"
import * as configError_ from "effect/ConfigError"
import * as context_ from "effect/Context"
import * as data_ from "effect/Data"
import * as dateTime from "effect/DateTime"
import * as duration_ from "effect/Duration"
Expand Down Expand Up @@ -8310,6 +8311,10 @@ export type CauseEncoded<E, D> =
readonly left: CauseEncoded<E, D>
readonly right: CauseEncoded<E, D>
}
| {
readonly _tag: "Annotated"
readonly cause: CauseEncoded<E, D>
}

const causeDieEncoded = <D, DI, R>(defect: Schema<D, DI, R>) =>
Struct({
Expand Down Expand Up @@ -8346,6 +8351,12 @@ const causeSequentialEncoded = <E, EI, D, DI, R>(causeEncoded: Schema<CauseEncod
right: causeEncoded
})

const causeAnnotatedEncoded = <E, EI, D, DI, R>(causeEncoded: Schema<CauseEncoded<E, D>, CauseEncoded<EI, DI>, R>) =>
Struct({
_tag: Literal("Annotated"),
cause: causeEncoded
})

const causeEncoded = <E, EI, D, DI, R1, R2>(
error: Schema<E, EI, R1>,
defect: Schema<D, DI, R2>
Expand All @@ -8357,15 +8368,16 @@ const causeEncoded = <E, EI, D, DI, R1, R2>(
causeDieEncoded(defect),
CauseInterruptEncoded,
causeSequentialEncoded(recur),
causeParallelEncoded(recur)
causeParallelEncoded(recur),
causeAnnotatedEncoded(recur)
).annotations({ title: `CauseEncoded<${format(error)}>` })
return out
}

const causeArbitrary = <E>(
const causeEncodedArbitrary = <E>(
error: LazyArbitrary<E>,
defect: LazyArbitrary<unknown>
): LazyArbitrary<cause_.Cause<E>> =>
): LazyArbitrary<CauseEncoded<E, unknown>> =>
(fc) =>
fc.letrec((tie) => ({
Empty: fc.record({ _tag: fc.constant("Empty" as const) }),
Expand All @@ -8374,15 +8386,23 @@ const causeArbitrary = <E>(
Interrupt: fc.record({ _tag: fc.constant("Interrupt" as const), fiberId: fiberIdArbitrary(fc) }),
Sequential: fc.record({ _tag: fc.constant("Sequential" as const), left: tie("Cause"), right: tie("Cause") }),
Parallel: fc.record({ _tag: fc.constant("Parallel" as const), left: tie("Cause"), right: tie("Cause") }),
Annotated: fc.record({ _tag: fc.constant("Annotated" as const), cause: tie("Cause") }),
Cause: fc.oneof(
tie("Empty"),
tie("Fail"),
tie("Die"),
tie("Interrupt"),
tie("Sequential"),
tie("Parallel")
) as any as fastCheck_.Arbitrary<cause_.Cause<E>>
})).Cause.map(causeDecode)
tie("Parallel"),
tie("Annotated")
) as any as fastCheck_.Arbitrary<CauseEncoded<E, unknown>>
})).Cause

const causeArbitrary = <E>(
error: LazyArbitrary<E>,
defect: LazyArbitrary<unknown>
): LazyArbitrary<cause_.Cause<E>> =>
(fc) => causeEncodedArbitrary(error, defect)(fc).map(causeDecode)

const causePretty = <E>(error: pretty_.Pretty<E>): pretty_.Pretty<cause_.Cause<E>> => (cause) => {
const f = (cause: cause_.Cause<E>): string => {
Expand All @@ -8399,6 +8419,8 @@ const causePretty = <E>(error: pretty_.Pretty<E>): pretty_.Pretty<cause_.Cause<E
return `Cause.sequential(${f(cause.left)}, ${f(cause.right)})`
case "Parallel":
return `Cause.parallel(${f(cause.left)}, ${f(cause.right)})`
case "Annotated":
return `Cause.annotated(${cause_.pretty(cause.cause)})`
}
}
return f(cause)
Expand Down Expand Up @@ -8461,6 +8483,8 @@ function causeDecode<E>(cause: CauseEncoded<E, unknown>): cause_.Cause<E> {
return cause_.sequential(causeDecode(cause.left), causeDecode(cause.right))
case "Parallel":
return cause_.parallel(causeDecode(cause.left), causeDecode(cause.right))
case "Annotated":
return cause_.annotated(causeDecode(cause.cause), context_.empty())
}
}

Expand All @@ -8486,6 +8510,8 @@ function causeEncode<E>(cause: cause_.Cause<E>): CauseEncoded<E, unknown> {
left: causeEncode(cause.left),
right: causeEncode(cause.right)
}
case "Annotated":
return causeEncode(cause.cause)
}
}

Expand Down Expand Up @@ -8626,7 +8652,7 @@ const exitArbitrary = <A, E>(
): LazyArbitrary<exit_.Exit<A, E>> =>
(fc) =>
fc.oneof(
fc.record({ _tag: fc.constant("Failure" as const), cause: causeArbitrary(error, defect)(fc) }),
fc.record({ _tag: fc.constant("Failure" as const), cause: causeEncodedArbitrary(error, defect)(fc) }),
fc.record({ _tag: fc.constant("Success" as const), value: value(fc) })
).map(exitDecode)

Expand Down
2 changes: 1 addition & 1 deletion packages/schema/test/Schema/Cause/Cause.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("Cause", () => {
`(CauseEncoded<NumberFromString> <-> Cause<number>)
└─ Encoded side transformation failure
└─ CauseEncoded<NumberFromString>
└─ { readonly _tag: "Empty" | "Fail" | "Die" | "Interrupt" | "Sequential" | "Parallel" }
└─ { readonly _tag: "Empty" | "Fail" | "Die" | "Interrupt" | "Sequential" | "Parallel" | "Annotated" }
└─ ["_tag"]
└─ is missing`
)
Expand Down

0 comments on commit d716e18

Please sign in to comment.