Use AutoTitleAnnotationId to add automatic titles to avoid interfering with user-defined titles (#3918)

This commit is contained in:
Giulio Canti
2024-11-20 17:12:32 +01:00
committed by GitHub
parent 320557ab18
commit 4dca30cfcd
5 changed files with 30 additions and 40 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"effect": patch
---
Use a specific annotation (`AutoTitleAnnotationId`) to add automatic titles (added by `Struct` and `Class` APIs), instead of `TitleAnnotationId`, to avoid interfering with user-defined titles.
+5 -14
View File
@@ -2605,8 +2605,8 @@ const getDefaultTypeLiteralAST = <
})
}
return new AST.Transformation(
new AST.TypeLiteral(from, issFrom, { [AST.TitleAnnotationId]: "Struct (Encoded side)" }),
new AST.TypeLiteral(to, issTo, { [AST.TitleAnnotationId]: "Struct (Type side)" }),
new AST.TypeLiteral(from, issFrom, { [AST.AutoTitleAnnotationId]: "Struct (Encoded side)" }),
new AST.TypeLiteral(to, issTo, { [AST.AutoTitleAnnotationId]: "Struct (Type side)" }),
new AST.TypeLiteralTransformation(transformations)
)
}
@@ -8100,15 +8100,6 @@ const extendFields = (a: Struct.Fields, b: Struct.Fields): Struct.Fields => {
return out
}
// does not overwrite existing title annotation
const orElseTitleAnnotation = <A, I, R>(schema: Schema<A, I, R>, title: string): Schema<A, I, R> => {
const annotation = AST.getTitleAnnotation(schema.ast)
if (option_.isNone(annotation)) {
return schema.annotations({ title })
}
return schema
}
type MakeOptions = boolean | {
readonly disableValidation?: boolean
}
@@ -8128,9 +8119,9 @@ const makeClass = ({ Base, annotations, disableToString, fields, identifier, kin
disableToString?: boolean | undefined
}): any => {
const classSymbol = Symbol.for(`effect/Schema/${kind}/${identifier}`)
const validateSchema = orElseTitleAnnotation(schema, `${identifier} (Constructor)`)
const encodedSide: Schema.Any = orElseTitleAnnotation(schema, `${identifier} (Encoded side)`)
const typeSide = orElseTitleAnnotation(typeSchema(schema), `${identifier} (Type side)`)
const validateSchema = schema.annotations({ [AST.AutoTitleAnnotationId]: `${identifier} (Constructor)` })
const encodedSide: Schema.Any = schema.annotations({ [AST.AutoTitleAnnotationId]: `${identifier} (Encoded side)` })
const typeSide = typeSchema(schema).annotations({ [AST.AutoTitleAnnotationId]: `${identifier} (Type side)` })
const fallbackInstanceOf = (u: unknown) => Predicate.hasProperty(u, classSymbol) && ParseResult.is(typeSide)(u)
const klass = class extends Base {
constructor(
+8 -1
View File
@@ -126,6 +126,9 @@ export type TitleAnnotation = string
*/
export const TitleAnnotationId: unique symbol = Symbol.for("effect/annotation/Title")
/** @internal */
export const AutoTitleAnnotationId: unique symbol = Symbol.for("effect/annotation/AutoTitle")
/**
* @category annotations
* @since 3.10.0
@@ -346,6 +349,9 @@ export const getMissingMessageAnnotation = getAnnotation<MissingMessageAnnotatio
*/
export const getTitleAnnotation = getAnnotation<TitleAnnotation>(TitleAnnotationId)
/** @internal */
export const getAutoTitleAnnotation = getAnnotation<TitleAnnotation>(AutoTitleAnnotationId)
/**
* @category annotations
* @since 3.10.0
@@ -2814,6 +2820,7 @@ const formatKeyword = (ast: AST): string => Option.getOrElse(getExpected(ast), (
const getExpected = (ast: Annotated): Option.Option<string> => {
return getIdentifierAnnotation(ast).pipe(
Option.orElse(() => getTitleAnnotation(ast)),
Option.orElse(() => getDescriptionAnnotation(ast))
Option.orElse(() => getDescriptionAnnotation(ast)),
Option.orElse(() => getAutoTitleAnnotation(ast))
)
}
+6 -13
View File
@@ -1566,7 +1566,6 @@ schema (Suspend): <suspended schema>`
expectJSONSchema(Schema.typeSchema(A), {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "A (Type side)",
"required": [
"a"
],
@@ -1602,8 +1601,7 @@ schema (Suspend): <suspended schema>`
"type": "string"
}
},
"additionalProperties": false,
"title": "A (Encoded side)"
"additionalProperties": false
})
})
})
@@ -2057,8 +2055,7 @@ schema (Suspend): <suspended schema>`
"minLength": 1
}
},
"additionalProperties": false,
"title": "Struct (Encoded side)"
"additionalProperties": false
})
expectJSONSchema(Schema.typeSchema(schema), {
"$schema": "http://json-schema.org/draft-07/schema#",
@@ -2074,8 +2071,7 @@ schema (Suspend): <suspended schema>`
"minLength": 1
}
},
"additionalProperties": false,
"title": "Struct (Type side)"
"additionalProperties": false
})
expectJSONSchema(Schema.encodedSchema(schema), {
"$schema": "http://json-schema.org/draft-07/schema#",
@@ -2107,8 +2103,7 @@ schema (Suspend): <suspended schema>`
"minLength": 1
}
},
"additionalProperties": false,
"title": "Struct (Encoded side)"
"additionalProperties": false
}
)
})
@@ -2130,8 +2125,7 @@ schema (Suspend): <suspended schema>`
"minLength": 1
}
},
"additionalProperties": false,
"title": "Struct (Encoded side)"
"additionalProperties": false
}
)
})
@@ -2155,8 +2149,7 @@ schema (Suspend): <suspended schema>`
"minLength": 1
}
},
"additionalProperties": false,
"title": "Struct (Encoded side)"
"additionalProperties": false
}
)
})
@@ -1528,8 +1528,7 @@ schema (Suspend): <suspended schema>`
"type": "string"
}
},
"additionalProperties": false,
"title": "A (Encoded side)"
"additionalProperties": false
})
})
})
@@ -1949,8 +1948,7 @@ schema (Suspend): <suspended schema>`
"minLength": 1
}
},
"additionalProperties": false,
"title": "Struct (Encoded side)"
"additionalProperties": false
})
expectJSONSchema(Schema.typeSchema(schema), {
"type": "object",
@@ -1965,8 +1963,7 @@ schema (Suspend): <suspended schema>`
"minLength": 1
}
},
"additionalProperties": false,
"title": "Struct (Type side)"
"additionalProperties": false
})
expectJSONSchema(Schema.encodedSchema(schema), {
"type": "object",
@@ -1996,8 +1993,7 @@ schema (Suspend): <suspended schema>`
"minLength": 1
}
},
"additionalProperties": false,
"title": "Struct (Encoded side)"
"additionalProperties": false
}
)
})
@@ -2018,8 +2014,7 @@ schema (Suspend): <suspended schema>`
"minLength": 1
}
},
"additionalProperties": false,
"title": "Struct (Encoded side)"
"additionalProperties": false
}
)
})
@@ -2042,8 +2037,7 @@ schema (Suspend): <suspended schema>`
"minLength": 1
}
},
"additionalProperties": false,
"title": "Struct (Encoded side)"
"additionalProperties": false
}
)
})