import { Schema } from 'effect'; import type { DeltaSchema, ESchemaDescriptor, StructFieldsSchema, } from './schema-model.js'; export function schemaDescriptor(schema: Schema.Top): ESchemaDescriptor { const { schema: root, definitions } = Schema.toJsonSchemaDocument(schema); const value = { ...root } as ESchemaDescriptor; if (Object.keys(definitions).length > 0) value.$defs = definitions; return value; } export function struct( fields: S, ): Schema.Codec< Schema.Schema.Type>, Schema.Codec.Encoded> > { return Schema.Struct(fields) as unknown as Schema.Codec< Schema.Schema.Type>, Schema.Codec.Encoded> >; } export const INITIAL_VERSION = 'v1' as const; export const metaSchema = Schema.Struct({ _v: Schema.String, }); export const id = (identifier: string) => Schema.String.annotate({ identifier }); export const fromType = () => Schema.Unknown as unknown as Schema.Codec; export function mergeDelta( base: StructFieldsSchema, delta: DeltaSchema, ): StructFieldsSchema { const merged: StructFieldsSchema = { ...base }; for (const [key, value] of Object.entries(delta)) { if (value === null) { delete merged[key]; } else { merged[key] = value; } } return merged; }