skeleton for decorator-based schema validation

This commit is contained in:
Florian Dold 2016-01-05 18:37:21 +01:00
parent 91fc8d56f0
commit 9dc0d89118
8 changed files with 151 additions and 19 deletions

View File

@ -0,0 +1,87 @@
/*
This file is part of TALER
(C) 2016 GNUnet e.V.
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
*/
"use strict";
/**
* Decorators for type-checking JSON into
* an object.
* @module Checkable
* @author Florian Dold
*/
namespace Checkable {
let chkSym = Symbol("checkable");
function checkNumber(target, prop) {
return true;
}
function checkString(target, prop) {
return true;
}
export function Class(target) {
target.checked = (v) => {
let props = target.prototype[chkSym].props;
console.log("hello, world");
let remainingPropNames = new Set(Object.getOwnPropertyNames(v));
for (let prop of props) {
remainingPropNames.delete(prop);
console.log("prop", prop);
}
if (remainingPropNames.size != 0) {
throw Error("superfluous properties " + JSON.stringify(remainingPropNames.values()));
}
};
return target;
}
export function Value(type) {
function deco(target) {
}
return deco;
}
export function List(type) {
function deco(target) {
}
return deco;
}
export function Number(target: Object, propertyKey: string | symbol): void {
let chk = target[chkSym];
if (!chk) {
chk = {props: []};
target[chkSym] = chk;
}
chk.props.push({propertyKey: propertyKey, checker: checkNumber});
}
export function String(target: Object, propertyKey: string | symbol): void {
let chk = target[chkSym];
if (!chk) {
chk = {props: []};
target[chkSym] = chk;
}
chk.props.push({propertyKey: propertyKey, checker: checkString});
}
}

View File

@ -55,7 +55,7 @@ function exportDb(db) {
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let tx = db.transaction(db.objectStoreNames); let tx = db.transaction(db.objectStoreNames);
tx.addEventListener('complete', (e) => { tx.addEventListener("complete", (e) => {
resolve(dump); resolve(dump);
}); });
for (let i = 0; i < db.objectStoreNames.length; i++) { for (let i = 0; i < db.objectStoreNames.length; i++) {
@ -64,7 +64,7 @@ function exportDb(db) {
dump.stores[name] = storeDump; dump.stores[name] = storeDump;
let store = tx.objectStore(name) let store = tx.objectStore(name)
.openCursor() .openCursor()
.addEventListener('success', (e) => { .addEventListener("success", (e) => {
let cursor = e.target.result; let cursor = e.target.result;
if (cursor) { if (cursor) {
storeDump[cursor.key] = cursor.value; storeDump[cursor.key] = cursor.value;

View File

@ -41,10 +41,10 @@ namespace Db {
} }
export interface Denomination { export interface Denomination {
value: AmountJson; value: AmountJson_interface;
denom_pub: string; denom_pub: string;
fee_withdraw: AmountJson; fee_withdraw: AmountJson_interface;
fee_deposit: AmountJson; fee_deposit: AmountJson_interface;
} }
export interface PreCoin { export interface PreCoin {
@ -56,7 +56,7 @@ namespace Db {
withdrawSig: string; withdrawSig: string;
coinEv: string; coinEv: string;
mintBaseUrl: string; mintBaseUrl: string;
coinValue: AmountJson; coinValue: AmountJson_interface;
} }
export interface Coin { export interface Coin {
@ -64,7 +64,7 @@ namespace Db {
coinPriv: string; coinPriv: string;
denomPub: string; denomPub: string;
denomSig: string; denomSig: string;
currentAmount: AmountJson; currentAmount: AmountJson_interface;
mintBaseUrl: string; mintBaseUrl: string;
} }
@ -119,7 +119,7 @@ function exportDb(db): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let tx = db.transaction(db.objectStoreNames); let tx = db.transaction(db.objectStoreNames);
tx.addEventListener('complete', (e) => { tx.addEventListener("complete", (e) => {
resolve(dump); resolve(dump);
}); });
for (let i = 0; i < db.objectStoreNames.length; i++) { for (let i = 0; i < db.objectStoreNames.length; i++) {
@ -128,7 +128,7 @@ function exportDb(db): Promise<any> {
dump.stores[name] = storeDump; dump.stores[name] = storeDump;
let store = tx.objectStore(name) let store = tx.objectStore(name)
.openCursor() .openCursor()
.addEventListener('success', (e) => { .addEventListener("success", (e) => {
let cursor = e.target.result; let cursor = e.target.result;
if (cursor) { if (cursor) {
storeDump[cursor.key] = cursor.value; storeDump[cursor.key] = cursor.value;

View File

@ -291,7 +291,7 @@ arenaStack.push(new SyncArena());
class Amount extends ArenaObject { class Amount extends ArenaObject {
constructor(args?: AmountJson, arena?: Arena) { constructor(args?: AmountJson_interface, arena?: Arena) {
super(arena); super(arena);
if (args) { if (args) {
this.nativePtr = emscAlloc.get_amount(args.value, this.nativePtr = emscAlloc.get_amount(args.value,

View File

@ -21,7 +21,29 @@
*/ */
/// <reference path="../decl/urijs/URIjs.d.ts" /> /// <reference path="../decl/urijs/URIjs.d.ts" />
/// <reference path="../decl/chrome/chrome.d.ts" /> /// <reference path="../decl/chrome/chrome.d.ts" />
'use strict'; "use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
class MyClass {
}
let AmountJson = class {
};
__decorate([
Checkable.Number
], AmountJson.prototype, "value", void 0);
__decorate([
Checkable.Number
], AmountJson.prototype, "fraction", void 0);
__decorate([
Checkable.String
], AmountJson.prototype, "currency", void 0);
AmountJson = __decorate([
Checkable.Class
], AmountJson);
/** /**
* See http://api.taler.net/wallet.html#general * See http://api.taler.net/wallet.html#general
*/ */

View File

@ -24,10 +24,31 @@
/// <reference path="../decl/urijs/URIjs.d.ts" /> /// <reference path="../decl/urijs/URIjs.d.ts" />
/// <reference path="../decl/chrome/chrome.d.ts" /> /// <reference path="../decl/chrome/chrome.d.ts" />
'use strict'; "use strict";
interface AmountJson { class MyClass {
value: number;
fraction: number;
currency: string;
}
@Checkable.Class
class AmountJson {
@Checkable.Number
value: number;
@Checkable.Number
fraction: number;
@Checkable.String
currency: string;
static check: (v: any) => AmountJson;
}
interface AmountJson_interface {
value: number; value: number;
fraction: number fraction: number
currency: string; currency: string;
@ -58,11 +79,11 @@ interface Offer {
interface Contract { interface Contract {
H_wire: string; H_wire: string;
amount: AmountJson; amount: AmountJson_interface;
auditors: string[]; auditors: string[];
expiry: string, expiry: string,
locations: string[]; locations: string[];
max_fee: AmountJson; max_fee: AmountJson_interface;
merchant: any; merchant: any;
merchant_pub: string; merchant_pub: string;
mints: MintInfo[]; mints: MintInfo[];
@ -77,7 +98,7 @@ interface CoinPaySig {
coin_pub: string; coin_pub: string;
ub_sig: string; ub_sig: string;
denom_pub: string; denom_pub: string;
f: AmountJson; f: AmountJson_interface;
} }
@ -181,8 +202,8 @@ function signDeposit(db: IDBDatabase,
* @param allowedMints * @param allowedMints
*/ */
function getPossibleMintCoins(db: IDBDatabase, function getPossibleMintCoins(db: IDBDatabase,
paymentAmount: AmountJson, paymentAmount: AmountJson_interface,
depositFeeLimit: AmountJson, depositFeeLimit: AmountJson_interface,
allowedMints: MintInfo[]): Promise<MintCoins> { allowedMints: MintInfo[]): Promise<MintCoins> {
@ -617,7 +638,7 @@ function updateMintFromUrl(db, baseUrl) {
function getBalances(db): Promise<any> { function getBalances(db): Promise<any> {
function collectBalances(c: Db.Coin, byCurrency) { function collectBalances(c: Db.Coin, byCurrency) {
let acc: AmountJson = byCurrency[c.currentAmount.currency]; let acc: AmountJson_interface = byCurrency[c.currentAmount.currency];
if (!acc) { if (!acc) {
acc = Amount.getZero(c.currentAmount.currency).toJson(); acc = Amount.getZero(c.currentAmount.currency).toJson();
} }

View File

@ -48,6 +48,7 @@
"background/db.js", "background/db.js",
"background/query.js", "background/query.js",
"background/messaging.js", "background/messaging.js",
"background/checkable.js",
"background/http.js", "background/http.js",
"background/wallet.js" "background/wallet.js"
] ]

View File

@ -10,6 +10,7 @@
"background/db.ts", "background/db.ts",
"background/query.ts", "background/query.ts",
"background/http.ts", "background/http.ts",
"background/checkable.ts",
"background/messaging.ts", "background/messaging.ts",
"lib/util.ts", "lib/util.ts",
"lib/polyfill-react.ts", "lib/polyfill-react.ts",