Working effect schema mutability
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -6,28 +6,33 @@ import { Extend, StaticMembers } from "../util"
|
|||||||
|
|
||||||
|
|
||||||
export class EffectSchemaTraitExpression<
|
export class EffectSchemaTraitExpression<
|
||||||
Fields extends S.Struct.Fields,
|
Fields extends S.Struct.Fields,
|
||||||
I, R, C,
|
I, R, C,
|
||||||
Inherited extends object,
|
Inherited extends object,
|
||||||
Proto,
|
Proto,
|
||||||
Static extends object,
|
Static extends object,
|
||||||
|
|
||||||
const Traits extends readonly Trait<any, any, any, any>[],
|
const Traits extends readonly Trait<any, any, any, any>[],
|
||||||
|
|
||||||
|
const Mutability extends "Immutable" | "Mutable",
|
||||||
|
const EncodedMutability extends "Immutable" | "Mutable",
|
||||||
>
|
>
|
||||||
implements TraitExpressionLike<
|
implements TraitExpressionLike<
|
||||||
S.Class<unknown, Fields, I, R, C, Inherited, Proto>,
|
S.Class<unknown, Fields, I, R, C, Inherited, Proto>,
|
||||||
Traits
|
Traits
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly superclass: S.Class<unknown, Fields, I, R, C, Inherited, Proto>,
|
readonly superclass: S.Class<unknown, Fields, I, R, C, Inherited, Proto>,
|
||||||
readonly superclassStatic: Static,
|
readonly superclassStatic: Static,
|
||||||
readonly traits: Traits,
|
readonly traits: Traits,
|
||||||
|
readonly mutability: Mutability,
|
||||||
|
readonly encodedMutability: EncodedMutability,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
|
||||||
extends<Self>(): (
|
extends<Self>(): (
|
||||||
AbstractConstructor<
|
AbstractConstructor<
|
||||||
S.Struct.Type<Fields> &
|
ApplyMutability<S.Struct.Type<Fields>, Mutability> &
|
||||||
Omit<Inherited, keyof Fields> &
|
Omit<Inherited, keyof Fields> &
|
||||||
Proto,
|
Proto,
|
||||||
|
|
||||||
@@ -38,7 +43,8 @@ implements TraitExpressionLike<
|
|||||||
S.Class<
|
S.Class<
|
||||||
Self,
|
Self,
|
||||||
Fields,
|
Fields,
|
||||||
I, R, C,
|
ApplyMutability<I, EncodedMutability>,
|
||||||
|
R, C,
|
||||||
Simplify<
|
Simplify<
|
||||||
Extend<[
|
Extend<[
|
||||||
Inherited,
|
Inherited,
|
||||||
@@ -69,3 +75,10 @@ implements TraitExpressionLike<
|
|||||||
return (_target: StaticImplements<typeof this>) => {}
|
return (_target: StaticImplements<typeof this>) => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type ApplyMutability<T, Mutability extends "Immutable" | "Mutable"> = (
|
||||||
|
Mutability extends "Immutable"
|
||||||
|
? { +readonly [P in keyof T]: T[P] }
|
||||||
|
: { -readonly [P in keyof T]: T[P] }
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { Schema as S } from "@effect/schema"
|
import { Schema as S } from "@effect/schema"
|
||||||
import * as Types from "effect/Types"
|
|
||||||
import { Simplify } from "type-fest"
|
import { Simplify } from "type-fest"
|
||||||
import { Trait, TraitTuple } from "../Trait"
|
import { Trait, TraitTuple } from "../Trait"
|
||||||
import { TraitExpression } from "../TraitExpression"
|
import { TraitExpression } from "../TraitExpression"
|
||||||
@@ -22,6 +21,8 @@ export class EffectSchemaInitialTraitExpressionBuilder {
|
|||||||
S.Class<unknown>(identifier)(fields, annotations),
|
S.Class<unknown>(identifier)(fields, annotations),
|
||||||
{},
|
{},
|
||||||
[],
|
[],
|
||||||
|
"Immutable",
|
||||||
|
"Immutable",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,6 +38,8 @@ export class EffectSchemaInitialTraitExpressionBuilder {
|
|||||||
S.TaggedClass<unknown>()(tag, fields, annotations),
|
S.TaggedClass<unknown>()(tag, fields, annotations),
|
||||||
{},
|
{},
|
||||||
[],
|
[],
|
||||||
|
"Immutable",
|
||||||
|
"Immutable",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,56 +68,71 @@ export class EffectSchemaInitialTraitExpressionBuilder {
|
|||||||
>,
|
>,
|
||||||
|
|
||||||
[],
|
[],
|
||||||
|
"Immutable",
|
||||||
|
"Immutable",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export class EffectSchemaTraitExpressionBuilder<
|
export class EffectSchemaTraitExpressionBuilder<
|
||||||
Fields extends S.Struct.Fields,
|
Fields extends S.Struct.Fields,
|
||||||
I, R, C,
|
I, R, C,
|
||||||
Inherited extends object,
|
Inherited extends object,
|
||||||
Proto,
|
Proto,
|
||||||
Static extends object,
|
Static extends object,
|
||||||
|
|
||||||
const Traits extends readonly Trait<any, any, any, any>[],
|
const Traits extends readonly Trait<any, any, any, any>[],
|
||||||
|
|
||||||
|
const Mutability extends "Immutable" | "Mutable",
|
||||||
|
const EncodedMutability extends "Immutable" | "Mutable",
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly expressionSuperclass: S.Class<unknown, Fields, I, R, C, Inherited, Proto>,
|
readonly expressionSuperclass: S.Class<unknown, Fields, I, R, C, Inherited, Proto>,
|
||||||
readonly expressionSuperclassStatic: Static,
|
readonly expressionSuperclassStatic: Static,
|
||||||
readonly expressionTraits: Traits,
|
readonly expressionTraits: Traits,
|
||||||
|
readonly expressionMutability: Mutability,
|
||||||
|
readonly expressionEncodedMutability: EncodedMutability,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
|
||||||
// mutable() {
|
mutable() {
|
||||||
// return new EffectSchemaTraitExpressionBuilder(
|
return new EffectSchemaTraitExpressionBuilder(
|
||||||
// this.expressionSuperclass as S.Class<unknown, Fields, Types.Mutable<A>, I, R, C, Inherited, Proto>,
|
this.expressionSuperclass,
|
||||||
// this.expressionSuperclassStatic,
|
this.expressionSuperclassStatic,
|
||||||
// this.expressionTraits,
|
this.expressionTraits,
|
||||||
// )
|
"Mutable",
|
||||||
// }
|
this.expressionEncodedMutability,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// immutable() {
|
immutable() {
|
||||||
// return new EffectSchemaTraitExpressionBuilder(
|
return new EffectSchemaTraitExpressionBuilder(
|
||||||
// this.expressionSuperclass as S.Class<unknown, Fields, Readonly<A>, I, R, C, Inherited, Proto>,
|
this.expressionSuperclass,
|
||||||
// this.expressionSuperclassStatic,
|
this.expressionSuperclassStatic,
|
||||||
// this.expressionTraits,
|
this.expressionTraits,
|
||||||
// )
|
"Immutable",
|
||||||
// }
|
this.expressionEncodedMutability,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
mutableEncoded() {
|
mutableEncoded() {
|
||||||
return new EffectSchemaTraitExpressionBuilder(
|
return new EffectSchemaTraitExpressionBuilder(
|
||||||
this.expressionSuperclass as S.Class<unknown, Fields, Types.Mutable<I>, R, C, Inherited, Proto>,
|
this.expressionSuperclass,
|
||||||
this.expressionSuperclassStatic,
|
this.expressionSuperclassStatic,
|
||||||
this.expressionTraits,
|
this.expressionTraits,
|
||||||
|
this.expressionMutability,
|
||||||
|
"Mutable",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
immutableEncoded() {
|
immutableEncoded() {
|
||||||
return new EffectSchemaTraitExpressionBuilder(
|
return new EffectSchemaTraitExpressionBuilder(
|
||||||
this.expressionSuperclass as S.Class<unknown, Fields, Readonly<I>, R, C, Inherited, Proto>,
|
this.expressionSuperclass,
|
||||||
this.expressionSuperclassStatic,
|
this.expressionSuperclassStatic,
|
||||||
this.expressionTraits,
|
this.expressionTraits,
|
||||||
|
this.expressionMutability,
|
||||||
|
"Immutable",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,6 +158,9 @@ export class EffectSchemaTraitExpressionBuilder<
|
|||||||
...this.expressionTraits,
|
...this.expressionTraits,
|
||||||
...spreadSupertraits(traits),
|
...spreadSupertraits(traits),
|
||||||
]) as TraitExpressionBuilder.ExpressesReturnTypeTraits<Traits, T>,
|
]) as TraitExpressionBuilder.ExpressesReturnTypeTraits<Traits, T>,
|
||||||
|
|
||||||
|
this.expressionMutability,
|
||||||
|
this.expressionEncodedMutability,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,13 +187,17 @@ export class EffectSchemaTraitExpressionBuilder<
|
|||||||
Proto,
|
Proto,
|
||||||
Static,
|
Static,
|
||||||
|
|
||||||
Traits
|
Traits,
|
||||||
|
Mutability,
|
||||||
|
EncodedMutability
|
||||||
>
|
>
|
||||||
) {
|
) {
|
||||||
return new EffectSchemaTraitExpression(
|
return new EffectSchemaTraitExpression(
|
||||||
this.expressionSuperclass,
|
this.expressionSuperclass,
|
||||||
this.expressionSuperclassStatic,
|
this.expressionSuperclassStatic,
|
||||||
this.expressionTraits,
|
this.expressionTraits,
|
||||||
|
this.expressionMutability,
|
||||||
|
this.expressionEncodedMutability,
|
||||||
) as any
|
) as any
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,21 +8,6 @@ type RequiredKeys<T> = {
|
|||||||
[K in keyof T]-?: {} extends Pick<T, K> ? never : K
|
[K in keyof T]-?: {} extends Pick<T, K> ? never : K
|
||||||
}[keyof T]
|
}[keyof T]
|
||||||
|
|
||||||
type Extr<T> =
|
|
||||||
T extends EffectSchemaTraitExpression<
|
|
||||||
infer Fields,
|
|
||||||
infer I,
|
|
||||||
infer R,
|
|
||||||
infer C,
|
|
||||||
infer Inherited,
|
|
||||||
infer Proto,
|
|
||||||
infer Static,
|
|
||||||
|
|
||||||
infer Traits
|
|
||||||
>
|
|
||||||
? Static
|
|
||||||
: never
|
|
||||||
|
|
||||||
type InspectSchemaClass<T> = T extends S.Class<infer Self, infer Fields, infer I, infer R, infer C, infer Inherited, infer Proto>
|
type InspectSchemaClass<T> = T extends S.Class<infer Self, infer Fields, infer I, infer R, infer C, infer Inherited, infer Proto>
|
||||||
? Inherited
|
? Inherited
|
||||||
: never
|
: never
|
||||||
@@ -33,7 +18,7 @@ const userExp = effectSchemaExpression
|
|||||||
id: S.BigIntFromSelf,
|
id: S.BigIntFromSelf,
|
||||||
role: S.Union(S.Literal("User"), S.Literal("Admin")),
|
role: S.Union(S.Literal("User"), S.Literal("Admin")),
|
||||||
})
|
})
|
||||||
// .mutable()
|
.mutable()
|
||||||
.mutableEncoded()
|
.mutableEncoded()
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
@@ -46,7 +31,7 @@ export class User extends userExp.extends<User>() implements Implements<typeof u
|
|||||||
User.Encoded
|
User.Encoded
|
||||||
|
|
||||||
const user = new User({ id: 0n, role: "User" })
|
const user = new User({ id: 0n, role: "User" })
|
||||||
// user.id = 0n
|
user.id = 0n
|
||||||
|
|
||||||
|
|
||||||
const adminExp = effectSchemaExpression
|
const adminExp = effectSchemaExpression
|
||||||
|
|||||||
Reference in New Issue
Block a user