Skip to content

Commit

Permalink
Fix formatting for Class and brands AST (#2581)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Apr 20, 2024
1 parent d90e8c3 commit a58b7de
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-humans-argue.md
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

Fix formatting for Class and brands AST.
29 changes: 16 additions & 13 deletions packages/schema/src/Schema.ts
Expand Up @@ -2275,15 +2275,12 @@ const makeBrandSchema = <S extends Schema.AnyNoContext, B extends string | symbo
})
)
// make refined a BrandSchema...
refined[TypeId] = variance
refined.ast = ast
refined.pipe = function() {
return pipeArguments(this, arguments)
}
refined[TypeId] = variance
Object.setPrototypeOf(refined, SchemaImpl.prototype)
refined.annotations = (annotations: Annotations.Schema<Schema.Type<S> & brand_.Brand<B>>) => {
return makeBrandSchema(ast, annotations, brand)
}
refined.toString = () => `${ast} & Brand<${formatPropertyKey(brand)}>`
return refined
}

Expand Down Expand Up @@ -2340,6 +2337,8 @@ export const brand = <S extends Schema.AnyNoContext, B extends string | symbol>(
onSome: (brands) => [...brands, brand]
})
return makeBrandSchema(self.ast, {
// add a default title annotation containing the brand
title: String(self.ast) + ` & Brand<${util_.formatUnknown(brand)}>`,
...annotations,
[AST.BrandAnnotationId]: brandAnnotation
}, brand)
Expand Down Expand Up @@ -6487,6 +6486,10 @@ const makeClass = ({ Base, annotations, fields, fromSchema, identifier, kind, ta
const classSymbol = Symbol.for(`@effect/schema/${kind}/${identifier}`)
const schema = fromSchema ?? Struct(fields)
const validate = ParseResult.validateSync(schema)
const from = option_.match(AST.getTitleAnnotation(schema.ast), {
onNone: () => schema.annotations({ title: `${identifier} (Encoded side)` }),
onSome: () => schema
})

return class extends Base {
constructor(
Expand All @@ -6508,10 +6511,6 @@ const makeClass = ({ Base, annotations, fields, fromSchema, identifier, kind, ta
return classSymbol
}

toString() {
return toStringOverride !== undefined ? toStringOverride(this) : pretty_.make(this.constructor as any)(this)
}
static pipe() {
return pipeArguments(this, arguments)
}
Expand All @@ -6520,6 +6519,14 @@ const makeClass = ({ Base, annotations, fields, fromSchema, identifier, kind, ta
return make(this.ast).annotations(annotations)
}

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

toString() {
return toStringOverride !== undefined ? toStringOverride(this) : pretty_.make(this.constructor as any)(this)
}

static fields = { ...fields }

static identifier = identifier
Expand Down Expand Up @@ -6558,10 +6565,6 @@ const makeClass = ({ Base, annotations, fields, fromSchema, identifier, kind, ta
...annotations
}
)
const from = option_.match(AST.getTitleAnnotation(schema.ast), {
onNone: () => schema.annotations({ title: `${identifier} (Encoded side)` }),
onSome: () => schema
})
const transformation = transform(
from,
declaration,
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/test/Schema/Class/Class.test.ts
Expand Up @@ -129,6 +129,8 @@ describe("Class APIs", () => {
it("should be a Schema", () => {
class A extends S.Class<A>("A")({ a: S.String }) {}
expect(S.isSchema(A)).toEqual(true)
expect(String(A)).toBe("(A (Encoded side) <-> A)")
expect(S.format(A)).toBe("(A (Encoded side) <-> A)")
})

it("should expose the fields", () => {
Expand Down
14 changes: 10 additions & 4 deletions packages/schema/test/Schema/brand.test.ts
Expand Up @@ -13,6 +13,12 @@ const isBrandConstructor = (u: unknown): u is Brand.Brand.Constructor<any> =>

describe("brand", () => {
describe("annotations", () => {
it("toString / format", () => {
const schema = S.Number.pipe(S.brand("A"))
expect(String(schema)).toBe(`number & Brand<"A">`)
expect(S.format(schema)).toBe(`number & Brand<"A">`)
})

it("using .annotations() twice", () => {
const schema = S.Number.pipe(S.brand("A"))
const annotatedSchema = schema.annotations({
Expand Down Expand Up @@ -48,7 +54,7 @@ describe("brand", () => {
expect(schema.ast.annotations).toEqual({
[AST.TypeAnnotationId]: S.IntTypeId,
[AST.BrandAnnotationId]: ["A"],
[AST.TitleAnnotationId]: "integer",
[AST.TitleAnnotationId]: `integer & Brand<"A">`,
[AST.DescriptionAnnotationId]: "an A brand",
[AST.JSONSchemaAnnotationId]: { type: "integer" }
})
Expand All @@ -67,7 +73,7 @@ describe("brand", () => {
expect(schema.ast.annotations).toEqual({
[AST.TypeAnnotationId]: S.IntTypeId,
[AST.BrandAnnotationId]: ["A", "B"],
[AST.TitleAnnotationId]: "integer",
[AST.TitleAnnotationId]: `integer & Brand<"A"> & Brand<"B">`,
[AST.DescriptionAnnotationId]: "a B brand",
[AST.JSONSchemaAnnotationId]: { type: "integer" }
})
Expand All @@ -87,7 +93,7 @@ describe("brand", () => {
expect(schema.ast.annotations).toEqual({
[AST.TypeAnnotationId]: S.IntTypeId,
[AST.BrandAnnotationId]: [A, B],
[AST.TitleAnnotationId]: "integer",
[AST.TitleAnnotationId]: "integer & Brand<Symbol(A)> & Brand<Symbol(B)>",
[AST.DescriptionAnnotationId]: "a B brand",
[AST.JSONSchemaAnnotationId]: { type: "integer" }
})
Expand Down Expand Up @@ -131,7 +137,7 @@ describe("brand", () => {
it("either", () => {
const Int = S.NumberFromString.pipe(S.int(), S.brand("Int"))
expect(Int.either(1)).toEqual(Either.right(1))
expect(Either.mapLeft(Int.either(1.2), (errors) => errors[0].message)).toEqual(Either.left(`integer
expect(Either.mapLeft(Int.either(1.2), (errors) => errors[0].message)).toEqual(Either.left(`integer & Brand<"Int">
└─ Predicate refinement failure
└─ Expected an integer, actual 1.2`))
})
Expand Down

0 comments on commit a58b7de

Please sign in to comment.