Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions .changeset/loud-otters-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tsplus/runtime": patch
"@tsplus/stdlib": patch
---

Removed Not Needed Lazy and Update TS+
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
"@effect-ts/build-utils": "0.40.3",
"@effect-ts/core": "^0.60.2",
"@repo-tooling/eslint-plugin-dprint": "^0.0.4",
"@tsplus/installer": "0.0.132",
"@types/node": "^18.6.4",
"@tsplus/installer": "0.0.133",
"@types/node": "^18.6.5",
"@types/rimraf": "^3.0.2",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.32.0",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"babel-plugin-annotate-pure-calls": "^0.4.0",
"babel-plugin-replace-import-extension": "^1.1.3",
"c8": "^7.12.0",
Expand All @@ -60,8 +60,8 @@
"rimraf": "^3.0.2",
"typescript": "^4.7.4",
"ultra-runner": "^3.10.5",
"vite": "^3.0.4",
"vitest": "0.20.3"
"vite": "^3.0.5",
"vitest": "0.21.1"
},
"resolutions": {
"eslint-plugin-codegen": "patch:eslint-plugin-codegen@npm:0.16.1#.yarn/patches/eslint-plugin-codegen-npm-0.16.1-87770191cd"
Expand Down
14 changes: 10 additions & 4 deletions packages/runtime/_src/Brand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export declare namespace Brand {

export type Brands<P> = P extends Valid<any, any> ? TypeLevel.UnionToIntersection<
{
[k in keyof P[Brand.valid]]: P extends P[Brand.valid][k] ? k extends string ? Valid<P[Brand.valid][k], k> : never
[k in keyof P[Brand.valid]]: P extends P[Brand.valid][k]
? k extends string ? Valid<P[Brand.valid][k], k> : never
: never
}[keyof P[Brand.valid]]
>
Expand Down Expand Up @@ -79,18 +80,23 @@ export declare namespace Brand {
readonly validate: (a: A) => a is A & Brand.Valid<A, K>
}

export type ValidatedWith<X extends Validation<any, any>> = X extends Validation<infer A, infer K> ? Validated<A, K>
export type ValidatedWith<X extends Validation<any, any>> = X extends Validation<infer A, infer K>
? Validated<A, K>
: never

/**
* @tsplus type Brand/Ops
*/
export interface Ops {
readonly validation: <A, K extends string>(predicate: (a: A) => boolean) => Brand.Validation<A, K>
readonly validation: <A, K extends string>(
predicate: (a: A) => boolean
) => Brand.Validation<A, K>
}
}

export function validation<A, K extends string>(predicate: (a: A) => boolean): Brand.Validation<A, K> {
export function validation<A, K extends string>(
predicate: (a: A) => boolean
): Brand.Validation<A, K> {
return {
validate: (value): value is Brand.Validated<A, K> => predicate(value)
}
Expand Down
98 changes: 74 additions & 24 deletions packages/runtime/_src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export const Decoder: DecoderOps = {}
/**
* @tsplus static Decoder/Ops __call
*/
export function make<A>(decodeResult: (u: unknown) => Result<Decoder.Error, Decoder.Error, A>): Decoder<A> {
export function make<A>(
decodeResult: (u: unknown) => Result<Decoder.Error, Decoder.Error, A>
): Decoder<A> {
return {
decodeResult
}
Expand Down Expand Up @@ -82,7 +84,10 @@ export class DecoderErrorPrimitive implements Decoder.Error {
readonly expectedType: string
) {}
render = () => {
return Tree(`Expected a value of type "${this.expectedType}" but received one of type "${typeof this.value}"`)
return Tree(
`Expected a value of type "${this.expectedType}" but received one of type "${typeof this
.value}"`
)
}
}

Expand All @@ -100,7 +105,10 @@ export class DecoderErrorIsoDateMalformed implements Decoder.Error {
readonly value: unknown
) {}
render = () => {
return Tree(`Expected a Date represented as an iso string instead received one of type "${typeof this.value}"`)
return Tree(
`Expected a Date represented as an iso string instead received one of type "${typeof this
.value}"`
)
}
}

Expand All @@ -116,7 +124,9 @@ export class DecoderErrorLiteral implements Decoder.Error {
` of type "${typeof this.expected}"` :
""
} instead received ${
typeof this.value === typeof this.expected ? `"${this.value}"` : `one of type "${typeof this.value}"`
typeof this.value === typeof this.expected ?
`"${this.value}"` :
`one of type "${typeof this.value}"`
}`
)
}
Expand All @@ -138,18 +148,24 @@ export class DecoderErrorStruct implements Decoder.Error {
constructor(
readonly fields: Chunk<DecoderErrorStructFieldError>
) {}
render = () => Tree(`Encountered while parsing an object structure`, this.fields.map((d) => d.render()))
render = () =>
Tree(`Encountered while parsing an object structure`, this.fields.map((d) => d.render()))
}

export class DecoderErrorTaggedMalformed implements Decoder.Error {
constructor(readonly keys: string[]) {}
render = () =>
Tree(`Expected a tagged object of the form "{ _tag: ${this.keys.sort().map(k => `"${k}"`).join(" | ")} }"`)
Tree(
`Expected a tagged object of the form "{ _tag: ${
this.keys.sort().map(k => `"${k}"`).join(" | ")
} }"`
)
}

export class DecoderErrorTaggedInner implements Decoder.Error {
constructor(readonly tag: string, readonly error: Decoder.Error) {}
render = () => Tree(`Encountered while processing tagged object "${this.tag}"`, Chunk(this.error.render()))
render = () =>
Tree(`Encountered while processing tagged object "${this.tag}"`, Chunk(this.error.render()))
}

export class DecoderErrorUnionMember implements Decoder.Error {
Expand All @@ -171,7 +187,9 @@ export class DecoderErrorArray implements Decoder.Error {
render = () =>
Tree(
`Encountered while processing an Array of elements`,
this.errors.map(([n, err]) => Tree(`Encountered while processing element "${n}"`, Chunk(err.render())))
this.errors.map(([n, err]) =>
Tree(`Encountered while processing element "${n}"`, Chunk(err.render()))
)
)
}

Expand All @@ -191,35 +209,45 @@ export class DecoderErrorValidation implements Decoder.Error {
* @tsplus implicit
*/
export const _true: Decoder<true> = Decoder((u) =>
Derive<Guard<true>>().is(u) ? Result.success(u) : Result.fail(new DecoderErrorPrimitive(u, "true"))
Derive<Guard<true>>().is(u) ?
Result.success(u) :
Result.fail(new DecoderErrorPrimitive(u, "true"))
)

/**
* @tsplus implicit
*/
export const _false: Decoder<false> = Decoder((u) =>
Derive<Guard<false>>().is(u) ? Result.success(u) : Result.fail(new DecoderErrorPrimitive(u, "false"))
Derive<Guard<false>>().is(u) ?
Result.success(u) :
Result.fail(new DecoderErrorPrimitive(u, "false"))
)

/**
* @tsplus implicit
*/
export const boolean: Decoder<boolean> = Decoder((u) =>
Derive<Guard<boolean>>().is(u) ? Result.success(u) : Result.fail(new DecoderErrorPrimitive(u, "boolean"))
Derive<Guard<boolean>>().is(u) ?
Result.success(u) :
Result.fail(new DecoderErrorPrimitive(u, "boolean"))
)

/**
* @tsplus implicit
*/
export const string: Decoder<string> = Decoder((u) =>
Derive<Guard<string>>().is(u) ? Result.success(u) : Result.fail(new DecoderErrorPrimitive(u, "string"))
Derive<Guard<string>>().is(u) ?
Result.success(u) :
Result.fail(new DecoderErrorPrimitive(u, "string"))
)

/**
* @tsplus implicit
*/
export const number: Decoder<number> = Decoder((u) =>
Derive<Guard<number>>().is(u) ? Result.success(u) : Result.fail(new DecoderErrorPrimitive(u, "number"))
Derive<Guard<number>>().is(u) ?
Result.success(u) :
Result.fail(new DecoderErrorPrimitive(u, "number"))
)

/**
Expand Down Expand Up @@ -374,27 +402,37 @@ export function deriveArray<A extends Array<any>>(
if (hasFailed) {
return Result.fail(new DecoderErrorArray(errors))
}
return Result.success(out as A, errors.isEmpty ? Maybe.none : Maybe.some(new DecoderErrorArray(errors)))
return Result.success(
out as A,
errors.isEmpty ? Maybe.none : Maybe.some(new DecoderErrorArray(errors))
)
}
return Result.fail(new DecoderErrorPrimitive(u, "Array"))
})
}

type EitherStructural<E, A> = { _tag: "Left"; left: E } | { _tag: "Right"; right: A }

function deriveEitherInternal<E, A>(left: Decoder<E>, right: Decoder<A>): Decoder<EitherStructural<E, A>> {
function deriveEitherInternal<E, A>(
left: Decoder<E>,
right: Decoder<A>
): Decoder<EitherStructural<E, A>> {
return Derive()
}

/**
* @tsplus derive Decoder[Either]<_> 10
*/
export function deriveEither<A extends Either<any, any>>(
...[left, right]: [A] extends [Either<infer _E, infer _A>] ? [left: Decoder<_E>, right: Decoder<_A>] : never
...[left, right]: [A] extends [Either<infer _E, infer _A>]
? [left: Decoder<_E>, right: Decoder<_A>]
: never
): Decoder<A> {
const structural = deriveEitherInternal(left, right)
return Decoder((u) =>
structural.decodeResult(u).map((e) => e._tag === "Left" ? Either.left(e.left) as A : Either.right(e.right) as A)
structural.decodeResult(u).map((e) =>
e._tag === "Left" ? Either.left(e.left) as A : Either.right(e.right) as A
)
)
}

Expand All @@ -415,7 +453,9 @@ export function deriveMaybe<A extends Maybe<any>>(
): Decoder<A> {
const structural = deriveMaybeInternal(value)
return Decoder((u) =>
structural.decodeResult(u).map((e) => e._tag === "Some" ? Maybe.some(e.value) as A : Maybe.none as A)
structural.decodeResult(u).map((e) =>
e._tag === "Some" ? Maybe.some(e.value) as A : Maybe.none as A
)
)
}

Expand All @@ -424,14 +464,16 @@ export class DecoderErrorRecordValue implements Decoder.Error {
readonly key: string,
readonly error: Decoder.Error
) {}
render = () => Tree(`Encountered while parsing a record value at "${this.key}"`, Chunk(this.error.render()))
render = () =>
Tree(`Encountered while parsing a record value at "${this.key}"`, Chunk(this.error.render()))
}

export class DecoderErrorRecordFields implements Decoder.Error {
constructor(
readonly fields: Chunk<Decoder.Error>
) {}
render = () => Tree(`Encountered while parsing a record structure`, this.fields.map((d) => d.render()))
render = () =>
Tree(`Encountered while parsing a record structure`, this.fields.map((d) => d.render()))
}

export class DecoderErrorRecordMissingKeys implements Decoder.Error {
Expand All @@ -440,7 +482,9 @@ export class DecoderErrorRecordMissingKeys implements Decoder.Error {
) {}
render = () =>
Tree(
`Encountered while parsing a record structure, missing keys: ${this.missing.map((k) => `"${k}"`).join(", ")}`
`Encountered while parsing a record structure, missing keys: ${
this.missing.map((k) => `"${k}"`).join(", ")
}`
)
}

Expand All @@ -452,7 +496,9 @@ export function deriveEmptyRecord<A extends {}>(
): Decoder<A> {
const record = Derive<Guard<{}>>()
// @ts-expect-error
return Decoder((u) => record.is(u) ? Result.success(u) : Result.fail(new DecoderErrorPrimitive(u, "{}")))
return Decoder((u) =>
record.is(u) ? Result.success(u) : Result.fail(new DecoderErrorPrimitive(u, "{}"))
)
}

/**
Expand Down Expand Up @@ -553,9 +599,13 @@ export function deriveRecord<A extends Record<string, any>>(
* @tsplus derive Decoder<_> 20
*/
export function deriveLiteral<A extends string | number>(
...[value]: Check<Check.IsLiteral<A> & Check.Not<Check.IsUnion<A>>> extends Check.True ? [value: A] : never
...[value]: Check<Check.IsLiteral<A> & Check.Not<Check.IsUnion<A>>> extends Check.True
? [value: A]
: never
): Decoder<A> {
return Decoder((u) => u === value ? Result.success(u as A) : Result.fail(new DecoderErrorLiteral(value, u)))
return Decoder((u) =>
u === value ? Result.success(u as A) : Result.fail(new DecoderErrorLiteral(value, u))
)
}

/**
Expand Down
16 changes: 12 additions & 4 deletions packages/runtime/_src/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ export function deriveNamed<A extends Brand<any>>(
* @tsplus derive Encoder<_> 10
*/
export function deriveValidation<A extends Brand.Valid<any, any>>(
...[base]: Check<Brand.IsValidated<A>> extends Check.True ? [base: Encoder<Brand.Unbranded<A>>] : never
...[base]: Check<Brand.IsValidated<A>> extends Check.True ? [base: Encoder<Brand.Unbranded<A>>]
: never
): Encoder<A> {
return Encoder((a) => base.encode(a as Brand.Unbranded<A>))
}
Expand All @@ -112,15 +113,20 @@ export function deriveLazy<A>(fn: (_: Encoder<A>) => Encoder<A>): Encoder<A> {

type EitherStructural<E, A> = { _tag: "Left"; left: E } | { _tag: "Right"; right: A }

function deriveEitherInternal<E, A>(left: Encoder<E>, right: Encoder<A>): Encoder<EitherStructural<E, A>> {
function deriveEitherInternal<E, A>(
left: Encoder<E>,
right: Encoder<A>
): Encoder<EitherStructural<E, A>> {
return Derive()
}

/**
* @tsplus derive Encoder[Either]<_> 10
*/
export function deriveEither<A extends Either<any, any>>(
...[left, right]: [A] extends [Either<infer _E, infer _A>] ? [left: Encoder<_E>, right: Encoder<_A>] : never
...[left, right]: [A] extends [Either<infer _E, infer _A>]
? [left: Encoder<_E>, right: Encoder<_A>]
: never
): Encoder<A> {
const structural = deriveEitherInternal(left, right)
return Encoder((u) => structural.encode(u))
Expand Down Expand Up @@ -250,7 +256,9 @@ export function deriveRecord<A extends Record<string, any>>(
* @tsplus derive Encoder<_> 20
*/
export function deriveLiteral<A extends string | number>(
...[value]: Check<Check.IsLiteral<A> & Check.Not<Check.IsUnion<A>>> extends Check.True ? [value: A] : never
...[value]: Check<Check.IsLiteral<A> & Check.Not<Check.IsUnion<A>>> extends Check.True
? [value: A]
: never
): Encoder<A> {
return Encoder(() => value)
}
Expand Down
Loading