deno.land / x / pg_mem@2.8.1 / datatypes / t-equivalent.ts

t-equivalent.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { Evaluator } from '../evaluator.ts';import { TypeBase } from './datatype-base.ts';import { CastError, DataType, IEquivalentType, IType, nil, QueryError, typeDefToStr } from '../interfaces.ts';import { _ISchema, _IType } from '../interfaces-private.ts';import { Types } from './datatypes.ts';
export class EquivalentType extends TypeBase<string> {
private equiv: IType;
constructor(private def: IEquivalentType) { super(); if (typeof def.equivalentTo === 'string') { let eq = (Types as any)[def.equivalentTo]; if (typeof eq === 'function') { eq = eq(); } this.equiv = eq; } else { this.equiv = def.equivalentTo; }
if (!this.equiv) { throw new Error(`Invalid equilvalent type`); } }
get primary(): DataType { return this.def.name as any; }
doCanCast(to: _IType) { return to.primary === this.equiv.primary; }
doCast(value: Evaluator<string>, to: _IType<string>): Evaluator<any> | nil { return value; }
prefer(type: _IType<any>): _IType | nil { return this; }
doCanBuildFrom(from: _IType): boolean | nil { return from.primary === this.equiv.primary; }
doBuildFrom(value: Evaluator<string>, from: _IType<string>): Evaluator<string> | nil { return value .setConversion(x => { if (!this.def.isValid(x)) { throw new QueryError(`invalid input syntax for type ${typeDefToStr(this)}: ${x}`); } return x; }, val => ({ val, to: this.equiv.primary })); }}
pg_mem

Version Info

Tagged at
4 months ago