Skip to content

Commit

Permalink
add missing makePropertySignature constructor (#2880)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored May 30, 2024
1 parent cc8ac50 commit 60fe3d5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-mails-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

add missing `makePropertySignature` constructor
42 changes: 28 additions & 14 deletions packages/schema/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1511,8 +1511,7 @@ export interface PropertySignature<
): PropertySignature<TypeToken, Type, Key, EncodedToken, Encoded, HasDefault, R>
}

/** @internal */
export class PropertySignatureImpl<
class PropertySignatureImpl<
TypeToken extends PropertySignature.Token,
Type,
Key extends PropertyKey,
Expand All @@ -1539,14 +1538,29 @@ export class PropertySignatureImpl<
annotations(
annotations: PropertySignature.Annotations<Type>
): PropertySignature<TypeToken, Type, Key, EncodedToken, Encoded, HasDefault, R> {
return new PropertySignatureImpl(propertySignatureAnnotations_(this.ast, toASTAnnotations(annotations)))
return makePropertySignature(propertySignatureAnnotations_(this.ast, toASTAnnotations(annotations)))
}

toString() {
return String(this.ast)
}
}

/**
* @category PropertySignature
* @since 1.0.0
*/
export const makePropertySignature = <
TypeToken extends PropertySignature.Token,
Type,
Key extends PropertyKey,
EncodedToken extends PropertySignature.Token,
Encoded,
HasDefault extends boolean = false,
R = never
>(ast: PropertySignature.AST) =>
new PropertySignatureImpl<TypeToken, Type, Key, EncodedToken, Encoded, HasDefault, R>(ast)

/**
* Lifts a `Schema` into a `PropertySignature`.
*
Expand All @@ -1556,7 +1570,7 @@ export class PropertySignatureImpl<
export const propertySignature = <A, I, R>(
self: Schema<A, I, R>
): PropertySignature<PropertySignature.GetToken<false>, A, never, PropertySignature.GetToken<false>, I, false, R> =>
new PropertySignatureImpl(new PropertySignatureDeclaration(self.ast, false, true, {}, undefined))
makePropertySignature(new PropertySignatureDeclaration(self.ast, false, true, {}, undefined))

/**
* Enhances a property signature with a default constructor value.
Expand Down Expand Up @@ -1599,11 +1613,11 @@ export const withConstructorDefault: {
const ast = self.ast
switch (ast._tag) {
case "PropertySignatureDeclaration":
return new PropertySignatureImpl(
return makePropertySignature(
new PropertySignatureDeclaration(ast.type, ast.isOptional, ast.isReadonly, ast.annotations, defaultValue)
)
case "PropertySignatureTransformation":
return new PropertySignatureImpl(
return makePropertySignature(
new PropertySignatureTransformation(
ast.from,
new ToPropertySignature(ast.to.type, ast.to.isOptional, ast.to.isReadonly, ast.to.annotations, defaultValue),
Expand Down Expand Up @@ -1657,7 +1671,7 @@ export const withDecodingDefault: {
const ast = self.ast
switch (ast._tag) {
case "PropertySignatureDeclaration":
return new PropertySignatureImpl(
return makePropertySignature(
new PropertySignatureTransformation(
ast,
new ToPropertySignature(AST.typeAST(ast.type), false, true, {}, undefined),
Expand All @@ -1666,7 +1680,7 @@ export const withDecodingDefault: {
)
)
case "PropertySignatureTransformation":
return new PropertySignatureImpl(
return makePropertySignature(
new PropertySignatureTransformation(
ast.from,
new ToPropertySignature(ast.to.type, false, ast.to.isReadonly, ast.to.annotations, ast.to.defaultValue),
Expand Down Expand Up @@ -1764,7 +1778,7 @@ export const fromKey: {
const ast = self.ast
switch (ast._tag) {
case "PropertySignatureDeclaration": {
return new PropertySignatureImpl(
return makePropertySignature(
new PropertySignatureTransformation(
new FromPropertySignature(
ast.type,
Expand All @@ -1780,7 +1794,7 @@ export const fromKey: {
)
}
case "PropertySignatureTransformation":
return new PropertySignatureImpl(
return makePropertySignature(
new PropertySignatureTransformation(
new FromPropertySignature(
ast.from.type,
Expand Down Expand Up @@ -1814,7 +1828,7 @@ export const optionalToRequired = <FA, FI, FR, TA, TI, TR>(
readonly encode: (ti: TI) => option_.Option<FA>
}
): PropertySignature<":", TA, never, "?:", FI, false, FR | TR> =>
new PropertySignatureImpl(
makePropertySignature(
new PropertySignatureTransformation(
new FromPropertySignature(from.ast, true, true, {}, undefined),
new ToPropertySignature(to.ast, false, true, {}, undefined),
Expand Down Expand Up @@ -1844,7 +1858,7 @@ export const optionalToOptional = <FA, FI, FR, TA, TI, TR>(
readonly encode: (o: option_.Option<TI>) => option_.Option<FA>
}
): PropertySignature<"?:", TA, never, "?:", FI, false, FR | TR> =>
new PropertySignatureImpl(
makePropertySignature(
new PropertySignatureTransformation(
new FromPropertySignature(from.ast, true, true, {}, undefined),
new ToPropertySignature(to.ast, true, true, {}, undefined),
Expand Down Expand Up @@ -2004,7 +2018,7 @@ export const optional: {
{ decode: option_.filter(Predicate.isNotNull<A | null>), encode: identity }
)
} else {
return new PropertySignatureImpl(new PropertySignatureDeclaration(schema.ast, true, true, {}, undefined))
return makePropertySignature(new PropertySignatureDeclaration(schema.ast, true, true, {}, undefined))
}
}
} else {
Expand Down Expand Up @@ -2062,7 +2076,7 @@ export const optional: {
{ decode: option_.filter(Predicate.isNotNull<A | null | undefined>), encode: identity }
)
} else {
return new PropertySignatureImpl(
return makePropertySignature(
new PropertySignatureDeclaration(UndefinedOr(schema).ast, true, true, {}, undefined)
)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/schema/test/Schema/PropertySignature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe("PropertySignature", () => {
})

it("add a decoding default to an optional field", async () => {
const ps: S.PropertySignature<":", number, never, "?:", string, never> = new S.PropertySignatureImpl(
const ps: S.PropertySignature<":", number, never, "?:", string, never> = S.makePropertySignature(
new S.PropertySignatureTransformation(
new S.FromPropertySignature(S.NumberFromString.ast, true, true, {}, undefined),
new S.ToPropertySignature(S.Number.ast, false, true, {}, undefined),
Expand Down Expand Up @@ -113,7 +113,7 @@ describe("PropertySignature", () => {
})

it("add a bidirectional (decoding/encoding) default to an optional field", async () => {
const ps: S.PropertySignature<":", number, never, "?:", string, never> = new S.PropertySignatureImpl(
const ps: S.PropertySignature<":", number, never, "?:", string, never> = S.makePropertySignature(
new S.PropertySignatureTransformation(
new S.FromPropertySignature(S.NumberFromString.ast, true, true, {}, undefined),
new S.ToPropertySignature(S.Number.ast, false, true, {}, undefined),
Expand Down Expand Up @@ -142,7 +142,7 @@ describe("PropertySignature", () => {
})

it("empty string as optional", async () => {
const ps: S.PropertySignature<"?:", string, never, ":", string, never> = new S.PropertySignatureImpl(
const ps: S.PropertySignature<"?:", string, never, ":", string, never> = S.makePropertySignature(
new S.PropertySignatureTransformation(
new S.FromPropertySignature(S.String.ast, false, true, {}, undefined),
new S.ToPropertySignature(S.String.ast, true, true, {}, undefined),
Expand All @@ -159,7 +159,7 @@ describe("PropertySignature", () => {
})

it("encoding default", async () => {
const ps: S.PropertySignature<"?:", number, never, ":", number, never> = new S.PropertySignatureImpl(
const ps: S.PropertySignature<"?:", number, never, ":", number, never> = S.makePropertySignature(
new S.PropertySignatureTransformation(
new S.FromPropertySignature(S.Number.ast, false, true, {}, undefined),
new S.ToPropertySignature(S.Number.ast, true, true, {}, undefined),
Expand Down

0 comments on commit 60fe3d5

Please sign in to comment.