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

Commit

Permalink
rebased to main
Browse files Browse the repository at this point in the history
  • Loading branch information
jessekelly881 committed Dec 9, 2023
1 parent b8d6e0e commit 27de7e5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-spies-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

added toString for schema classes
54 changes: 32 additions & 22 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import * as InternalSchema from "./internal/schema.js"
import * as InternalSerializable from "./internal/serializable.js"
import * as Parser from "./Parser.js"
import * as ParseResult from "./ParseResult.js"
import type { Pretty } from "./Pretty.js"
import * as Pretty from "./Pretty.js"
import type * as Serializable from "./Serializable.js"

// ---------------------------------------------
Expand Down Expand Up @@ -1498,7 +1498,7 @@ export interface FilterAnnotations<A> extends DocAnnotations<A> {
readonly typeId?: AST.TypeAnnotation | { id: AST.TypeAnnotation; params: unknown }
readonly jsonSchema?: AST.JSONSchemaAnnotation
readonly arbitrary?: (...args: ReadonlyArray<Arbitrary<any>>) => Arbitrary<any>
readonly pretty?: (...args: ReadonlyArray<Pretty<any>>) => Pretty<any>
readonly pretty?: (...args: ReadonlyArray<Pretty.Pretty<any>>) => Pretty.Pretty<any>
readonly equivalence?: () => Equivalence.Equivalence<A>
}

Expand Down Expand Up @@ -2997,7 +2997,7 @@ export const DurationFromSelf: Schema<Duration.Duration> = declare(
: ParseResult.fail(ParseResult.type(ast, u)),
{
[AST.IdentifierAnnotationId]: "Duration",
[hooks.PrettyHookId]: (): Pretty<Duration.Duration> =>
[hooks.PrettyHookId]: (): Pretty.Pretty<Duration.Duration> =>
Duration.match({
onMillis: (_) => `Duration.millis(${_})`,
onNanos: (_) => `Duration.nanos(${_})`
Expand Down Expand Up @@ -3082,7 +3082,7 @@ export const Uint8ArrayFromSelf: Schema<Uint8Array> = declare(
: ParseResult.fail(ParseResult.type(ast, u)),
{
[AST.IdentifierAnnotationId]: "Uint8Array",
[hooks.PrettyHookId]: (): Pretty<Uint8Array> => (u8arr) =>
[hooks.PrettyHookId]: (): Pretty.Pretty<Uint8Array> => (u8arr) =>
`new Uint8Array(${JSON.stringify(Array.from(u8arr))})`,
[hooks.ArbitraryHookId]: (): Arbitrary<Uint8Array> => (fc) => fc.uint8Array(),
[hooks.EquivalenceHookId]: () => ReadonlyArray.getEquivalence(Equivalence.strict())
Expand Down Expand Up @@ -3155,7 +3155,7 @@ const makeEncodingTransform = <A extends string>(
{ strict: false }
).pipe(annotations({
[AST.IdentifierAnnotationId]: id,
[hooks.PrettyHookId]: (): Pretty<Uint8Array> => (u) => `${id}(${encode(u)})`,
[hooks.PrettyHookId]: (): Pretty.Pretty<Uint8Array> => (u) => `${id}(${encode(u)})`,
[hooks.ArbitraryHookId]: () => arbitrary
}))

Expand Down Expand Up @@ -3349,7 +3349,7 @@ export const validDate =

const dateArbitrary = (): Arbitrary<Date> => (fc) => fc.date({ noInvalidDate: false })

const datePretty = (): Pretty<Date> => (date) => `new Date(${JSON.stringify(date)})`
const datePretty = (): Pretty.Pretty<Date> => (date) => `new Date(${JSON.stringify(date)})`

/**
* Represents a schema for handling potentially **invalid** `Date` instances (e.g., `new Date("Invalid Date")` is not rejected).
Expand Down Expand Up @@ -3455,7 +3455,7 @@ const optionArbitrary = <A>(value: Arbitrary<A>): Arbitrary<Option.Option<A>> =>
return (fc) => arb(fc).map(optionDecode)
}

const optionPretty = <A>(value: Pretty<A>): Pretty<Option.Option<A>> =>
const optionPretty = <A>(value: Pretty.Pretty<A>): Pretty.Pretty<Option.Option<A>> =>
Option.match({
onNone: () => "none()",
onSome: (a) => `some(${value(a)})`
Expand Down Expand Up @@ -3559,7 +3559,10 @@ const eitherArbitrary = <E, A>(
return (fc) => arb(fc).map(eitherDecode)
}

const eitherPretty = <E, A>(left: Pretty<E>, right: Pretty<A>): Pretty<Either.Either<E, A>> =>
const eitherPretty = <E, A>(
left: Pretty.Pretty<E>,
right: Pretty.Pretty<A>
): Pretty.Pretty<Either.Either<E, A>> =>
Either.match({
onLeft: (e) => `left(${left(e)})`,
onRight: (a) => `right(${right(a)})`
Expand Down Expand Up @@ -3626,9 +3629,9 @@ const readonlyMapArbitrary = <K, V>(
(fc) => fc.array(fc.tuple(key(fc), value(fc))).map((as) => new Map(as))

const readonlyMapPretty = <K, V>(
key: Pretty<K>,
value: Pretty<V>
): Pretty<ReadonlyMap<K, V>> =>
key: Pretty.Pretty<K>,
value: Pretty.Pretty<V>
): Pretty.Pretty<ReadonlyMap<K, V>> =>
(map) =>
`new Map([${
Array.from(map.entries())
Expand Down Expand Up @@ -3701,7 +3704,7 @@ const isSet = (u: unknown): u is Set<unknown> => u instanceof Set
const readonlySetArbitrary = <A>(item: Arbitrary<A>): Arbitrary<ReadonlySet<A>> => (fc) =>
fc.array(item(fc)).map((as) => new Set(as))

const readonlySetPretty = <A>(item: Pretty<A>): Pretty<ReadonlySet<A>> => (set) =>
const readonlySetPretty = <A>(item: Pretty.Pretty<A>): Pretty.Pretty<ReadonlySet<A>> => (set) =>
`new Set([${Array.from(set.values()).map((a) => item(a)).join(", ")}])`

const readonlySetEquivalence = <A>(
Expand Down Expand Up @@ -3758,7 +3761,7 @@ export const readonlySet = <I, A>(item: Schema<I, A>): Schema<ReadonlyArray<I>,
// BigDecimal transformations
// ---------------------------------------------

const bigDecimalPretty = (): Pretty<BigDecimal.BigDecimal> => (val) =>
const bigDecimalPretty = (): Pretty.Pretty<BigDecimal.BigDecimal> => (val) =>
`BigDecimal(${BigDecimal.format(BigDecimal.normalize(val))})`

const bigDecimalArbitrary = (): Arbitrary<BigDecimal.BigDecimal> => (fc) =>
Expand Down Expand Up @@ -4102,7 +4105,7 @@ export const negateBigDecimal = <I, A extends BigDecimal.BigDecimal>(
const chunkArbitrary = <A>(item: Arbitrary<A>): Arbitrary<Chunk.Chunk<A>> => (fc) =>
fc.array(item(fc)).map(Chunk.fromIterable)

const chunkPretty = <A>(item: Pretty<A>): Pretty<Chunk.Chunk<A>> => (c) =>
const chunkPretty = <A>(item: Pretty.Pretty<A>): Pretty.Pretty<Chunk.Chunk<A>> => (c) =>
`Chunk(${Chunk.toReadonlyArray(c).map(item).join(", ")})`

/**
Expand Down Expand Up @@ -4161,8 +4164,8 @@ const dataArbitrary = <A extends Readonly<Record<string, any>> | ReadonlyArray<a
(fc) => item(fc).map(toData)

const dataPretty = <A extends Readonly<Record<string, any>> | ReadonlyArray<any>>(
item: Pretty<A>
): Pretty<Data.Data<A>> =>
item: Pretty.Pretty<A>
): Pretty.Pretty<Data.Data<A>> =>
(d) => `Data(${item(d)})`

/**
Expand Down Expand Up @@ -4417,6 +4420,7 @@ const makeClass = <I, A>(
additionalProps?: any
): any => {
const validator = Parser.validateSync(selfSchema)
const pretty = Pretty.to(selfSchema)
return class extends Base {
constructor(props: any = {}, disableValidation = false) {
Expand All @@ -4428,6 +4432,10 @@ const makeClass = <I, A>(
static [TypeId] = variance
toString() {
return pretty(this as any)
}
static pipe() {
return pipeArguments(this, arguments)
}
Expand Down Expand Up @@ -4549,7 +4557,7 @@ const fiberIdFromArbitrary = arbitrary.unsafe(FiberIdFrom)
const fiberIdArbitrary: Arbitrary<FiberId.FiberId> = (fc) =>
fiberIdFromArbitrary(fc).map(fiberIdDecode)
const fiberIdPretty: Pretty<FiberId.FiberId> = (fiberId) => {
const fiberIdPretty: Pretty.Pretty<FiberId.FiberId> = (fiberId) => {
switch (fiberId._tag) {
case "None":
return "FiberId.none"
Expand Down Expand Up @@ -4697,7 +4705,7 @@ const causeArbitrary = <E>(
return (fc) => arb(fc).map(causeDecode)
}
const causePretty = <E>(error: Pretty<E>): Pretty<Cause.Cause<E>> => (cause) => {
const causePretty = <E>(error: Pretty.Pretty<E>): Pretty.Pretty<Cause.Cause<E>> => (cause) => {
const f = (cause: Cause.Cause<E>): string => {
switch (cause._tag) {
case "Empty":
Expand Down Expand Up @@ -4869,10 +4877,12 @@ const exitArbitrary = <E, A>(
return (fc) => arb(fc).map(exitDecode)
}
const exitPretty = <E, A>(error: Pretty<E>, value: Pretty<A>): Pretty<Exit.Exit<E, A>> => (exit) =>
exit._tag === "Failure"
? `Exit.failCause(${causePretty(error)(exit.cause)})`
: `Exit.succeed(${value(exit.value)})`
const exitPretty =
<E, A>(error: Pretty.Pretty<E>, value: Pretty.Pretty<A>): Pretty.Pretty<Exit.Exit<E, A>> =>
(exit) =>
exit._tag === "Failure"
? `Exit.failCause(${causePretty(error)(exit.cause)})`
: `Exit.succeed(${value(exit.value)})`

/**
* @category Exit
Expand Down
8 changes: 8 additions & 0 deletions test/Schema/Class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ describe("Schema/Class", () => {
it("Data.Class", () => {
const person = new Person({ id: 1, name: "John" })
const personAge = new PersonWithAge({ id: 1, name: "John", age: 30 })

expect(String(person)).toEqual(`{ "id": 1, "name": "John" }`)
expect(person instanceof Data.Class).toEqual(true)
expect(personAge instanceof Data.Class).toEqual(true)

Expand Down Expand Up @@ -208,6 +210,8 @@ describe("Schema/Class", () => {

it("TaggedClass", () => {
let person = new TaggedPersonWithAge({ id: 1, name: "John", age: 30 })

expect(String(person)).toEqual(`{ "_tag": "TaggedPerson", "id": 1, "age": 30, "name": "John" }`)
expect(person._tag).toEqual("TaggedPerson")
expect(person.upperName).toEqual("JOHN")

Expand All @@ -232,6 +236,8 @@ describe("Schema/Class", () => {
}) {}

let err = new MyError({ id: 1 })

expect(String(err)).toEqual(`{ "_tag": "MyError", "id": 1 }`)
expect(err.stack).toContain("Class.test.ts:")
expect(err._tag).toEqual("MyError")
expect(err.id).toEqual(1)
Expand All @@ -251,6 +257,8 @@ describe("Schema/Class", () => {
}) {}

let req = new MyRequest({ id: 1 })

expect(String(req)).toEqual(`{ "_tag": "MyRequest", "id": 1 }`)
expect(req._tag).toEqual("MyRequest")
expect(req.id).toEqual(1)
expect(Request.isRequest(req)).toEqual(true)
Expand Down

0 comments on commit 27de7e5

Please sign in to comment.