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