From 435f0c8ef035995001dde92f8e7a04fe35a3a41d Mon Sep 17 00:00:00 2001 From: Tyler Schicke Date: Tue, 28 May 2019 20:00:50 -0400 Subject: Added default values for Document Schemas --- src/new_fields/Schema.ts | 18 +++++++++++++++--- src/new_fields/Types.ts | 12 +++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) (limited to 'src/new_fields') diff --git a/src/new_fields/Schema.ts b/src/new_fields/Schema.ts index b821baec9..250f3c975 100644 --- a/src/new_fields/Schema.ts +++ b/src/new_fields/Schema.ts @@ -1,4 +1,4 @@ -import { Interface, ToInterface, Cast, ToConstructor, HasTail, Head, Tail, ListSpec, ToType } from "./Types"; +import { Interface, ToInterface, Cast, ToConstructor, HasTail, Head, Tail, ListSpec, ToType, DefaultFieldConstructor } from "./Types"; import { Doc, Field } from "./Doc"; type AllToInterface = { @@ -10,7 +10,7 @@ export const emptySchema = createSchema({}); export const Document = makeInterface(emptySchema); export type Document = makeInterface<[typeof emptySchema]>; -export type makeInterface = Partial> & Doc & { proto: Doc | undefined }; +export type makeInterface = AllToInterface & Doc & { proto: Doc | undefined }; // export function makeInterface(schemas: T): (doc: U) => All; // export function makeInterface(schema: T): (doc: U) => makeInterface; export function makeInterface(...schemas: T): (doc?: Doc) => makeInterface { @@ -24,7 +24,12 @@ export function makeInterface(...schemas: T): (doc?: Doc) get(target: any, prop, receiver) { const field = receiver.doc[prop]; if (prop in schema) { - return Cast(field, (schema as any)[prop]); + const desc = (schema as any)[prop]; + if (typeof desc === "object" && "defaultVal" in desc && "type" in desc) { + return Cast(field, desc.type, desc.defaultVal); + } else { + return Cast(field, (schema as any)[prop]); + } } return field; }, @@ -79,4 +84,11 @@ export function createSchema(schema: T): T & { proto: ToCon export function listSpec>(type: U): ListSpec> { return { List: type as any };//TODO Types +} + +export function defaultSpec>(type: T, defaultVal: ToType): DefaultFieldConstructor> { + return { + type: type as any, + defaultVal + }; } \ No newline at end of file diff --git a/src/new_fields/Types.ts b/src/new_fields/Types.ts index 4b4c58eb8..c04dd5e6d 100644 --- a/src/new_fields/Types.ts +++ b/src/new_fields/Types.ts @@ -2,12 +2,13 @@ import { Field, Opt, FieldResult, Doc } from "./Doc"; import { List } from "./List"; import { RefField } from "./RefField"; -export type ToType | ListSpec> = +export type ToType | ListSpec | DefaultFieldConstructor> = T extends "string" ? string : T extends "number" ? number : T extends "boolean" ? boolean : T extends ListSpec ? List : // T extends { new(...args: any[]): infer R } ? (R | Promise) : never; + T extends DefaultFieldConstructor ? never : T extends { new(...args: any[]): List } ? never : T extends { new(...args: any[]): infer R } ? R : never; @@ -19,12 +20,17 @@ export type ToConstructor = new (...args: any[]) => T; export type ToInterface = { - [P in Exclude]: FieldResult>; + [P in Exclude]: T[P] extends DefaultFieldConstructor ? Exclude, undefined> : FieldResult>; }; // type ListSpec = { List: ToContructor> | ListSpec> }; export type ListSpec = { List: ToConstructor }; +export type DefaultFieldConstructor = { + type: ToConstructor, + defaultVal: T +}; + // type ListType = { 0: List>>, 1: ToType> }[HasTail extends true ? 0 : 1]; export type Head = T extends [any, ...any[]] ? T[0] : never; @@ -34,7 +40,7 @@ export type HasTail = T extends ([] | [any]) ? false : true; //TODO Allow you to optionally specify default values for schemas, which should then make that field not be partial export interface Interface { - [key: string]: ToConstructor | ListSpec; + [key: string]: ToConstructor | ListSpec | DefaultFieldConstructor; // [key: string]: ToConstructor | ListSpec; } -- cgit v1.2.3-70-g09d2