All checks were successful
continuous-integration/drone/push Build is passing
Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/traitify-ts/pulls/21
207 lines
6.4 KiB
TypeScript
207 lines
6.4 KiB
TypeScript
import { Schema as S } from "@effect/schema"
|
|
import { Simplify } from "type-fest"
|
|
import { Trait, TraitTuple } from "../Trait"
|
|
import { TraitExpression } from "../TraitExpression"
|
|
import { TraitExpressionBuilder } from "../TraitExpressionBuilder"
|
|
import { spreadSupertraits } from "../spreadSupertraits"
|
|
import { traitsUnique } from "../traitsUnique"
|
|
import { Extendable, StaticMembers } from "../util"
|
|
import { EffectSchemaTraitExpression } from "./EffectSchemaTraitExpression"
|
|
|
|
|
|
export class EffectSchemaInitialTraitExpressionBuilder {
|
|
class<
|
|
Fields extends S.Struct.Fields
|
|
>(
|
|
identifier: string,
|
|
fields: Fields,
|
|
annotations?: S.Annotations.Schema<unknown>,
|
|
) {
|
|
return new EffectSchemaTraitExpressionBuilder(
|
|
S.Class<unknown>(identifier)(fields, annotations),
|
|
{},
|
|
[],
|
|
"Immutable",
|
|
"Immutable",
|
|
)
|
|
}
|
|
|
|
taggedClass<
|
|
Tag extends string,
|
|
Fields extends S.Struct.Fields,
|
|
>(
|
|
tag: Tag,
|
|
fields: Fields,
|
|
annotations?: S.Annotations.Schema<unknown>,
|
|
) {
|
|
return new EffectSchemaTraitExpressionBuilder(
|
|
S.TaggedClass<unknown>()(tag, fields, annotations),
|
|
{},
|
|
[],
|
|
"Immutable",
|
|
"Immutable",
|
|
)
|
|
}
|
|
|
|
extends<
|
|
Super extends StaticMembers<S.Class<Self, Fields, I, R, C, Inherited, Proto>>,
|
|
Self extends object,
|
|
Fields extends S.Struct.Fields,
|
|
I, R, C,
|
|
Inherited extends object,
|
|
Proto,
|
|
|
|
NewFields extends S.Struct.Fields,
|
|
>(
|
|
superclass: Super | StaticMembers<S.Class<Self, Fields, I, R, C, Inherited, Proto>>,
|
|
identifier: string,
|
|
fields: NewFields,
|
|
annotations?: S.Annotations.Schema<unknown>,
|
|
) {
|
|
return new EffectSchemaTraitExpressionBuilder(
|
|
superclass.extend<unknown>(identifier)(fields, annotations),
|
|
|
|
{} as Simplify<
|
|
Omit<Super,
|
|
"prototype" | keyof S.Class<Self, Fields, I, R, C, Inherited, Proto>
|
|
>
|
|
>,
|
|
|
|
[],
|
|
"Immutable",
|
|
"Immutable",
|
|
)
|
|
}
|
|
}
|
|
|
|
|
|
export class EffectSchemaTraitExpressionBuilder<
|
|
Fields extends S.Struct.Fields,
|
|
I, R, C,
|
|
Inherited extends object,
|
|
Proto,
|
|
Static extends object,
|
|
|
|
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 expressionMutability: Mutability,
|
|
readonly expressionEncodedMutability: EncodedMutability,
|
|
) {}
|
|
|
|
|
|
mutable() {
|
|
return new EffectSchemaTraitExpressionBuilder(
|
|
this.expressionSuperclass,
|
|
this.expressionSuperclassStatic,
|
|
this.expressionTraits,
|
|
"Mutable",
|
|
this.expressionEncodedMutability,
|
|
)
|
|
}
|
|
|
|
immutable() {
|
|
return new EffectSchemaTraitExpressionBuilder(
|
|
this.expressionSuperclass,
|
|
this.expressionSuperclassStatic,
|
|
this.expressionTraits,
|
|
"Immutable",
|
|
this.expressionEncodedMutability,
|
|
)
|
|
}
|
|
|
|
mutableEncoded() {
|
|
return new EffectSchemaTraitExpressionBuilder(
|
|
this.expressionSuperclass,
|
|
this.expressionSuperclassStatic,
|
|
this.expressionTraits,
|
|
this.expressionMutability,
|
|
"Mutable",
|
|
)
|
|
}
|
|
|
|
immutableEncoded() {
|
|
return new EffectSchemaTraitExpressionBuilder(
|
|
this.expressionSuperclass,
|
|
this.expressionSuperclassStatic,
|
|
this.expressionTraits,
|
|
this.expressionMutability,
|
|
"Immutable",
|
|
)
|
|
}
|
|
|
|
|
|
expresses<
|
|
const T extends readonly Trait<
|
|
TraitExpression<
|
|
typeof TraitExpression.NullSuperclass,
|
|
readonly Trait<any, any, any, any>[]
|
|
>,
|
|
any,
|
|
any,
|
|
any
|
|
>[]
|
|
>(
|
|
...traits: T
|
|
) {
|
|
return new EffectSchemaTraitExpressionBuilder(
|
|
this.expressionSuperclass,
|
|
this.expressionSuperclassStatic,
|
|
|
|
traitsUnique([
|
|
...this.expressionTraits,
|
|
...spreadSupertraits(traits),
|
|
]) as TraitExpressionBuilder.ExpressesReturnTypeTraits<Traits, T>,
|
|
|
|
this.expressionMutability,
|
|
this.expressionEncodedMutability,
|
|
)
|
|
}
|
|
|
|
|
|
build(): (
|
|
Extendable<TraitTuple.MapAbstract<Traits>> extends false
|
|
? "Type conflict between the traits abstract definitions."
|
|
: Extendable<TraitTuple.MapStaticAbstract<Traits>> extends false
|
|
? "Type conflict between the traits static abstract definitions."
|
|
: Extendable<[
|
|
InstanceType<typeof this.expressionSuperclass>,
|
|
...TraitTuple.MapImplInstance<Traits>,
|
|
]> extends false
|
|
? "Type conflict between the traits implementation instance and/or the superclass instance."
|
|
: Extendable<[
|
|
Static,
|
|
...TraitTuple.MapImplStaticMembers<Traits>,
|
|
]> extends false
|
|
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
|
: EffectSchemaTraitExpression<
|
|
Fields,
|
|
I, R, C,
|
|
Inherited,
|
|
Proto,
|
|
Static,
|
|
|
|
Traits,
|
|
Mutability,
|
|
EncodedMutability
|
|
>
|
|
) {
|
|
return new EffectSchemaTraitExpression(
|
|
this.expressionSuperclass,
|
|
this.expressionSuperclassStatic,
|
|
this.expressionTraits,
|
|
this.expressionMutability,
|
|
this.expressionEncodedMutability,
|
|
) as any
|
|
}
|
|
}
|
|
|
|
|
|
export const effectSchemaExpression = new EffectSchemaInitialTraitExpressionBuilder()
|