mirror of
https://github.com/anomalyco/opencode.git
synced 2026-04-23 22:34:53 +00:00
feat(schema): add Newtype helper for nominal scalar types
Newtype creates a class-based nominal wrapper where the class itself is a valid Effect Schema. Port QuestionID to use it, replacing the manual brand + withStatics pattern.
This commit is contained in:
@@ -2,16 +2,16 @@ import { Schema } from "effect"
|
||||
import z from "zod"
|
||||
|
||||
import { Identifier } from "@/id/id"
|
||||
import { withStatics } from "@/util/schema"
|
||||
import { Newtype } from "@/util/schema"
|
||||
|
||||
const questionIdSchema = Schema.String.pipe(Schema.brand("QuestionID"))
|
||||
export class QuestionID extends Newtype<QuestionID>()("QuestionID", Schema.String) {
|
||||
static make(id: string): QuestionID {
|
||||
return this.makeUnsafe(id)
|
||||
}
|
||||
|
||||
export type QuestionID = typeof questionIdSchema.Type
|
||||
static ascending(id?: string): QuestionID {
|
||||
return this.makeUnsafe(Identifier.ascending("question", id))
|
||||
}
|
||||
|
||||
export const QuestionID = questionIdSchema.pipe(
|
||||
withStatics((schema: typeof questionIdSchema) => ({
|
||||
make: (id: string) => schema.makeUnsafe(id),
|
||||
ascending: (id?: string) => schema.makeUnsafe(Identifier.ascending("question", id)),
|
||||
zod: Identifier.schema("question").pipe(z.custom<QuestionID>()),
|
||||
})),
|
||||
)
|
||||
static readonly zod = Identifier.schema("question") as unknown as z.ZodType<QuestionID>
|
||||
}
|
||||
|
||||
@@ -109,8 +109,8 @@ export class QuestionService extends ServiceMap.Service<QuestionService, Questio
|
||||
static readonly layer = Layer.effect(
|
||||
QuestionService,
|
||||
Effect.gen(function* () {
|
||||
const instanceState = yield* InstanceState.make<Map<QuestionID, PendingEntry>, QuestionServiceError>(
|
||||
() => Effect.succeed(new Map<QuestionID, PendingEntry>()),
|
||||
const instanceState = yield* InstanceState.make<Map<QuestionID, PendingEntry>, QuestionServiceError>(() =>
|
||||
Effect.succeed(new Map<QuestionID, PendingEntry>()),
|
||||
)
|
||||
|
||||
const getPending = InstanceState.get(instanceState)
|
||||
|
||||
@@ -15,3 +15,40 @@ export const withStatics =
|
||||
<S extends object, M extends Record<string, unknown>>(methods: (schema: S) => M) =>
|
||||
(schema: S): S & M =>
|
||||
Object.assign(schema, methods(schema))
|
||||
|
||||
declare const NewtypeBrand: unique symbol
|
||||
type NewtypeBrand<Tag extends string> = { readonly [NewtypeBrand]: Tag }
|
||||
|
||||
/**
|
||||
* Nominal wrapper for scalar types. The class itself is a valid schema —
|
||||
* pass it directly to `Schema.decode`, `Schema.decodeEffect`, etc.
|
||||
*
|
||||
* @example
|
||||
* class QuestionID extends Newtype<QuestionID>()("QuestionID", Schema.String) {
|
||||
* static make(id: string): QuestionID {
|
||||
* return this.makeUnsafe(id)
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* Schema.decodeEffect(QuestionID)(input)
|
||||
*/
|
||||
export function Newtype<Self>() {
|
||||
return <const Tag extends string, S extends Schema.Top>(tag: Tag, schema: S) => {
|
||||
type Branded = NewtypeBrand<Tag>
|
||||
|
||||
abstract class Base {
|
||||
declare readonly [NewtypeBrand]: Tag
|
||||
|
||||
static makeUnsafe(value: Schema.Schema.Type<S>): Self {
|
||||
return value as unknown as Self
|
||||
}
|
||||
}
|
||||
|
||||
Object.setPrototypeOf(Base, schema)
|
||||
|
||||
return Base as unknown as
|
||||
& (abstract new (_: never) => Branded)
|
||||
& { readonly makeUnsafe: (value: Schema.Schema.Type<S>) => Self }
|
||||
& Omit<Schema.Opaque<Self, S, {}>, "makeUnsafe">
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user