-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update TS definitions to be in sync with implementation (#99)
- Loading branch information
1 parent
e00fa8e
commit fbe5b0e
Showing
2 changed files
with
102 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,87 @@ | ||
export enum TypeId { | ||
BOOL = 16, | ||
BYTEA = 17, | ||
CHAR = 18, | ||
INT8 = 20, | ||
INT2 = 21, | ||
INT4 = 23, | ||
REGPROC = 24, | ||
TEXT = 25, | ||
OID = 26, | ||
TID = 27, | ||
XID = 28, | ||
CID = 29, | ||
JSON = 114, | ||
XML = 142, | ||
PG_NODE_TREE = 194, | ||
SMGR = 210, | ||
PATH = 602, | ||
POLYGON = 604, | ||
CIDR = 650, | ||
FLOAT4 = 700, | ||
FLOAT8 = 701, | ||
ABSTIME = 702, | ||
RELTIME = 703, | ||
TINTERVAL = 704, | ||
CIRCLE = 718, | ||
MACADDR8 = 774, | ||
MONEY = 790, | ||
MACADDR = 829, | ||
INET = 869, | ||
ACLITEM = 1033, | ||
BPCHAR = 1042, | ||
VARCHAR = 1043, | ||
DATE = 1082, | ||
TIME = 1083, | ||
TIMESTAMP = 1114, | ||
TIMESTAMPTZ = 1184, | ||
INTERVAL = 1186, | ||
TIMETZ = 1266, | ||
BIT = 1560, | ||
VARBIT = 1562, | ||
NUMERIC = 1700, | ||
REFCURSOR = 1790, | ||
REGPROCEDURE = 2202, | ||
REGOPER = 2203, | ||
REGOPERATOR = 2204, | ||
REGCLASS = 2205, | ||
REGTYPE = 2206, | ||
UUID = 2950, | ||
TXID_SNAPSHOT = 2970, | ||
PG_LSN = 3220, | ||
PG_NDISTINCT = 3361, | ||
PG_DEPENDENCIES = 3402, | ||
TSVECTOR = 3614, | ||
TSQUERY = 3615, | ||
GTSVECTOR = 3642, | ||
REGCONFIG = 3734, | ||
REGDICTIONARY = 3769, | ||
JSONB = 3802, | ||
REGNAMESPACE = 4089, | ||
REGROLE = 4096 | ||
BOOL = 16, | ||
BYTEA = 17, | ||
CHAR = 18, | ||
INT8 = 20, | ||
INT2 = 21, | ||
INT4 = 23, | ||
REGPROC = 24, | ||
TEXT = 25, | ||
OID = 26, | ||
TID = 27, | ||
XID = 28, | ||
CID = 29, | ||
JSON = 114, | ||
XML = 142, | ||
PG_NODE_TREE = 194, | ||
SMGR = 210, | ||
PATH = 602, | ||
POLYGON = 604, | ||
CIDR = 650, | ||
FLOAT4 = 700, | ||
FLOAT8 = 701, | ||
ABSTIME = 702, | ||
RELTIME = 703, | ||
TINTERVAL = 704, | ||
CIRCLE = 718, | ||
MACADDR8 = 774, | ||
MONEY = 790, | ||
MACADDR = 829, | ||
INET = 869, | ||
ACLITEM = 1033, | ||
BPCHAR = 1042, | ||
VARCHAR = 1043, | ||
DATE = 1082, | ||
TIME = 1083, | ||
TIMESTAMP = 1114, | ||
TIMESTAMPTZ = 1184, | ||
INTERVAL = 1186, | ||
TIMETZ = 1266, | ||
BIT = 1560, | ||
VARBIT = 1562, | ||
NUMERIC = 1700, | ||
REFCURSOR = 1790, | ||
REGPROCEDURE = 2202, | ||
REGOPER = 2203, | ||
REGOPERATOR = 2204, | ||
REGCLASS = 2205, | ||
REGTYPE = 2206, | ||
UUID = 2950, | ||
TXID_SNAPSHOT = 2970, | ||
PG_LSN = 3220, | ||
PG_NDISTINCT = 3361, | ||
PG_DEPENDENCIES = 3402, | ||
TSVECTOR = 3614, | ||
TSQUERY = 3615, | ||
GTSVECTOR = 3642, | ||
REGCONFIG = 3734, | ||
REGDICTIONARY = 3769, | ||
JSONB = 3802, | ||
REGNAMESPACE = 4089, | ||
REGROLE = 4096 | ||
} | ||
|
||
export type builtinsTypes = | ||
'BOOL' | | ||
'BYTEA' | | ||
'CHAR' | | ||
'INT8' | | ||
'INT2' | | ||
'INT4' | | ||
'REGPROC' | | ||
'TEXT' | | ||
'OID' | | ||
'TID' | | ||
'XID' | | ||
'CID' | | ||
'JSON' | | ||
'XML' | | ||
'PG_NODE_TREE' | | ||
'SMGR' | | ||
'PATH' | | ||
'POLYGON' | | ||
'CIDR' | | ||
'FLOAT4' | | ||
'FLOAT8' | | ||
'ABSTIME' | | ||
'RELTIME' | | ||
'TINTERVAL' | | ||
'CIRCLE' | | ||
'MACADDR8' | | ||
'MONEY' | | ||
'MACADDR' | | ||
'INET' | | ||
'ACLITEM' | | ||
'BPCHAR' | | ||
'VARCHAR' | | ||
'DATE' | | ||
'TIME' | | ||
'TIMESTAMP' | | ||
'TIMESTAMPTZ' | | ||
'INTERVAL' | | ||
'TIMETZ' | | ||
'BIT' | | ||
'VARBIT' | | ||
'NUMERIC' | | ||
'REFCURSOR' | | ||
'REGPROCEDURE' | | ||
'REGOPER' | | ||
'REGOPERATOR' | | ||
'REGCLASS' | | ||
'REGTYPE' | | ||
'UUID' | | ||
'TXID_SNAPSHOT' | | ||
'PG_LSN' | | ||
'PG_NDISTINCT' | | ||
'PG_DEPENDENCIES' | | ||
'TSVECTOR' | | ||
'TSQUERY' | | ||
'GTSVECTOR' | | ||
'REGCONFIG' | | ||
'REGDICTIONARY' | | ||
'JSONB' | | ||
'REGNAMESPACE' | | ||
'REGROLE'; | ||
export type TypesBuiltins = { | ||
readonly [key in keyof typeof TypeId]: TypeId; | ||
}; | ||
|
||
export type TypesBuiltins = {[key in builtinsTypes]: TypeId}; | ||
|
||
export type TypeFormat = 'text' | 'binary'; | ||
export type TypeParser<I extends (string | Buffer), T> = (value: I) => T; | ||
|
||
export const builtins: TypesBuiltins; | ||
|
||
export function setTypeParser (id: TypeId, parseFn: ((value: string) => any)): void; | ||
export function setTypeParser (id: TypeId, format: TypeFormat, parseFn: (value: string) => any): void; | ||
export function setTypeParser<T>(oid: number | TypeId, parseFn: TypeParser<string, T>): void; | ||
export function setTypeParser<T>(oid: number | TypeId, format: 'text', parseFn: TypeParser<string, T>): void; | ||
export function setTypeParser<T>(oid: number | TypeId, format: 'binary', parseFn: TypeParser<Buffer, T>): void; | ||
|
||
export function getTypeParser<T>(oid: number | TypeId): TypeParser<string, T | string>; | ||
export function getTypeParser<T>(oid: number | TypeId, format: 'text'): TypeParser<string, T | string>; | ||
export function getTypeParser<T>(oid: number | TypeId, format: 'binary'): TypeParser<Buffer, T | string>; | ||
|
||
export const getTypeParser: (id: TypeId, format?: TypeFormat) => any | ||
interface ArrayParser<T> { | ||
parse(): ReadonlyArray<T>; | ||
} | ||
|
||
export const arrayParser: (source: string, transform: (entry: any) => any) => any[]; | ||
export namespace arrayParser { | ||
function create<T>(source: string): ArrayParser<string>; | ||
function create<T>(source: string, transform: TypeParser<string, T>): ArrayParser<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
import * as types from '.'; | ||
import { expectType } from 'tsd'; | ||
import {expectType} from 'tsd'; | ||
|
||
// builtins | ||
expectType<types.TypesBuiltins>(types.builtins); | ||
|
||
// getTypeParser | ||
const noParse = types.getTypeParser(types.builtins.NUMERIC, 'text'); | ||
const numericParser = types.getTypeParser(types.builtins.NUMERIC, 'binary'); | ||
expectType<string>(noParse('noParse')); | ||
expectType<number>(numericParser([200, 1, 0, 15])); | ||
// getTypeParser - not existing parser | ||
const noParse1 = types.getTypeParser<string>(types.builtins.NUMERIC); | ||
expectType<string>(noParse1('noParse')); | ||
const noParse2 = types.getTypeParser<string>(types.builtins.NUMERIC, 'text'); | ||
expectType<string>(noParse2('noParse')); | ||
const noParse3 = types.getTypeParser<string>(types.builtins.BOOL, 'binary'); | ||
expectType<string>(noParse3(Buffer.from([]))); | ||
|
||
// getArrayParser | ||
const value = types.arrayParser('{1,2,3}', (num) => parseInt(num)); | ||
expectType<number[]>(value); | ||
// getTypeParser - existing parser | ||
const booleanParser1 = types.getTypeParser<boolean>(types.builtins.BOOL); | ||
expectType<boolean | string>(booleanParser1('t')); | ||
const booleanParser2 = types.getTypeParser<boolean>(types.builtins.BOOL, 'text'); | ||
expectType<boolean | string>(booleanParser2('f')); | ||
const numericParser = types.getTypeParser<number>(types.builtins.NUMERIC, 'binary'); | ||
expectType<number | string>(numericParser(Buffer.from([200, 1, 0, 15]))); | ||
|
||
//setTypeParser | ||
types.setTypeParser(types.builtins.INT8, parseInt); | ||
types.setTypeParser(types.builtins.FLOAT8, parseFloat); | ||
types.setTypeParser(types.builtins.FLOAT8, 'binary', (data) => data[0]); | ||
types.setTypeParser(types.builtins.FLOAT8, 'text', parseFloat); | ||
// arrayParser | ||
const value = types.arrayParser.create('{1,2,3}', (num: string): number => Number.parseInt(num)).parse(); | ||
expectType<ReadonlyArray<number>>(value); | ||
|
||
// setTypeParser | ||
types.setTypeParser(types.builtins.INT8, Number.parseInt); | ||
types.setTypeParser(types.builtins.FLOAT8, Number.parseFloat); | ||
types.setTypeParser(types.builtins.FLOAT8, 'binary', (data: Buffer): number => data[0]); | ||
types.setTypeParser(types.builtins.FLOAT8, 'text', Number.parseFloat); |