Skip to content

Commit

Permalink
Remove excessive constraint from the pipeable overload of `attachProp… (
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Apr 22, 2024
1 parent 0983c8b commit dd41c6c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-mirrors-try.md
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

Remove excessive constraint from the pipeable overload of `attachPropertySignature`, closes #2593
11 changes: 10 additions & 1 deletion packages/schema/dtslint/Schema.ts
@@ -1,3 +1,4 @@
import type * as AST from "@effect/schema/AST"
import * as ParseResult from "@effect/schema/ParseResult"
import * as S from "@effect/schema/Schema"
import * as Brand from "effect/Brand"
Expand Down Expand Up @@ -1164,7 +1165,7 @@ const FooterLocaleIDs = S.Literal("footer_title", "footer_sendoff")
S.TemplateLiteral(S.Union(EmailLocaleIDs, FooterLocaleIDs), S.Literal("_id"))

// ---------------------------------------------
// AttachPropertySignature
// attachPropertySignature
// ---------------------------------------------

// $ExpectType Schema<{ readonly radius: number; readonly kind: "circle"; }, { readonly radius: number; }, never>
Expand All @@ -1173,6 +1174,14 @@ pipe(S.Struct({ radius: S.Number }), S.attachPropertySignature("kind", "circle")
// $ExpectType Schema<{ readonly radius: number; readonly kind: "circle"; }, { readonly radius: string; }, never>
pipe(S.Struct({ radius: S.NumberFromString }), S.attachPropertySignature("kind", "circle"))

const taggedStruct = <Name extends AST.LiteralValue | symbol, Fields extends S.Struct.Fields>(
name: Name,
fields: Fields
) => S.Struct(fields).pipe(S.attachPropertySignature("_tag", name))

// $ExpectType Schema<{ readonly a: string; readonly _tag: "A"; }, { readonly a: string; }, never>
taggedStruct("A", { a: S.String })

// ---------------------------------------------
// filter
// ---------------------------------------------
Expand Down
109 changes: 62 additions & 47 deletions packages/schema/src/Schema.ts
Expand Up @@ -2263,26 +2263,29 @@ export const pluck: {

const makeBrandSchema = <S extends Schema.AnyNoContext, B extends string | symbol>(
self: AST.AST,
annotations: Annotations.Schema<Schema.Type<S> & brand_.Brand<B>>,
brand: string | symbol
annotations: Annotations.Schema<Schema.Type<S> & brand_.Brand<B>>
): brand<S, B> => {
const ast = AST.annotations(self, toASTAnnotations(annotations))
const validateEither_ = validateEither(make(ast))
const schema = make(ast)
const validateEither_ = validateEither(schema)

const refined: any = brand_.refined((unbranded) =>
// v-- function
const out: any = brand_.refined((unbranded) =>
either_.match(validateEither_(unbranded), {
onLeft: (e) => option_.some(brand_.error(TreeFormatter.formatErrorSync(e), e)),
onRight: () => option_.none()
})
)
// make refined a BrandSchema...
refined.ast = ast
refined[TypeId] = variance
Object.setPrototypeOf(refined, SchemaImpl.prototype)
refined.annotations = (annotations: Annotations.Schema<Schema.Type<S> & brand_.Brand<B>>) => {
return makeBrandSchema(ast, annotations, brand)
}
return refined
// ----------------
// Schema interface
// ----------------
Object.setPrototypeOf(
Object.assign(out, schema, {
annotations: (a: typeof annotations) => makeBrandSchema(ast, a)
}),
Object.getPrototypeOf(schema)
)
return out
}

/**
Expand Down Expand Up @@ -2342,7 +2345,7 @@ export const brand = <S extends Schema.AnyNoContext, B extends string | symbol>(
title: String(self.ast) + ` & Brand<${util_.formatUnknown(brand)}>`,
...annotations,
[AST.BrandAnnotationId]: brandAnnotation
}, brand)
})
}

/**
Expand Down Expand Up @@ -2877,7 +2880,7 @@ export function transformLiterals<
* @since 1.0.0
*/
export const attachPropertySignature: {
<K extends PropertyKey, V extends AST.LiteralValue | symbol, A extends object>(
<K extends PropertyKey, V extends AST.LiteralValue | symbol, A>(
key: K,
value: V,
annotations?: Annotations.Schema<Types.Simplify<A & { readonly [k in K]: V }>>
Expand Down Expand Up @@ -6508,40 +6511,11 @@ const makeClass = ({ Base, annotations, fields, fromSchema, identifier, kind, ta
super(props, true)
}

static [TypeId] = variance

get [classSymbol]() {
return classSymbol
}
// ----------------
// Schema interface
// ----------------

static pipe() {
return pipeArguments(this, arguments)
}

static annotations(annotations: Annotations.Schema<any>) {
return make(this.ast).annotations(annotations)
}

static toString() {
return `(${String(from)} <-> ${identifier})`
}

toString() {
if (toStringOverride !== undefined) {
const out = toStringOverride(this)
if (out !== undefined) {
return out
}
}
return `${identifier}({ ${
util_.ownKeys(fields).map((p: any) => `${util_.formatPropertyKey(p)}: ${util_.formatUnknown(this[p])}`)
.join(", ")
} })`
}

static fields = { ...fields }

static identifier = identifier
static [TypeId] = variance

static get ast() {
const toSchema = typeSchema(schema)
Expand Down Expand Up @@ -6582,6 +6556,26 @@ const makeClass = ({ Base, annotations, fields, fromSchema, identifier, kind, ta
return transformation.ast
}

static pipe() {
return pipeArguments(this, arguments)
}

static annotations(annotations: Annotations.Schema<any>) {
return make(this.ast).annotations(annotations)
}

static toString() {
return `(${String(from)} <-> ${identifier})`
}

// ----------------
// Class interface
// ----------------

static fields = { ...fields }

static identifier = identifier

static extend<Extended>(identifier: string) {
return (newFields: Struct.Fields, annotations?: Annotations.Schema<Extended>) => {
const extendedFields = extendFields(fields, newFields)
Expand Down Expand Up @@ -6633,6 +6627,27 @@ const makeClass = ({ Base, annotations, fields, fromSchema, identifier, kind, ta
})
}
}

// ----------------
// other
// ----------------

get [classSymbol]() {
return classSymbol
}

toString() {
if (toStringOverride !== undefined) {
const out = toStringOverride(this)
if (out !== undefined) {
return out
}
}
return `${identifier}({ ${
util_.ownKeys(fields).map((p: any) => `${util_.formatPropertyKey(p)}: ${util_.formatUnknown(this[p])}`)
.join(", ")
} })`
}
}
}

Expand Down

0 comments on commit dd41c6c

Please sign in to comment.