From 5da3e23c839b500e84466271f02fe1161c5b7d30 Mon Sep 17 00:00:00 2001 From: Dan Selman Date: Mon, 29 Oct 2018 11:27:54 +0000 Subject: [PATCH] Add automatic timestamp field for transactions and events (#24) * refactor modelmanager to standadlone module Signed-off-by: Dan Selman * bump versions, fix linting Signed-off-by: Dan Selman * ignore Signed-off-by: Dan Selman * wip Signed-off-by: Dan Selman * wip Signed-off-by: Dan Selman * Update travis build scripts Signed-off-by: Matt Roberts * Add webpack build Signed-off-by: Matt Roberts * Update .travis.yml Signed-off-by: Matt Roberts * replaced logger with debug Signed-off-by: Dan Selman * removed wallet. Some codecov clean up Signed-off-by: Dan Selman * WIP Remove all references to static system model. Signed-off-by: Matt Roberts * fix changelog Signed-off-by: Dan Selman * Fix xmlschemavisitor. Increase test coverage Signed-off-by: Matt Roberts * Revert nyc output to text-summary Signed-off-by: Matt Roberts * Add pkgbump. Fix webpack. Fix missing moment dependency * Fix version number * Add system model table (#5) * WIP Signed-off-by: Matt Roberts * Client app has to add system models Signed-off-by: Matt Roberts * Add system model table Signed-off-by: Matt Roberts * Move composer models from lib to test. Add test for custom system model Signed-off-by: Matt Roberts * Remove commented code in assetdeclaration Signed-off-by: Matt Roberts * switch to yarn Signed-off-by: Dan Selman * travis build Signed-off-by: Dan Selman * remove system model gen Signed-off-by: Dan Selman * first published version Signed-off-by: Dan Selman * (doc) update description Signed-off-by: Dan Selman * (chore) update docs Signed-off-by: Dan Selman * (chore) cleaning Signed-off-by: Dan Selman * (chore) publish latest version Signed-off-by: Dan Selman * allow transactions to specify an identifying field #21 Signed-off-by: Dan Selman * bump version Signed-off-by: Dan Selman * Automatically add a timestamp field to transactions and events Signed-off-by: Dan Selman --- lib/introspect/classdeclaration.js | 17 ++++++++ lib/introspect/eventdeclaration.js | 42 +++++-------------- lib/introspect/transactiondeclaration.js | 38 +++++------------ package-lock.json | 2 +- package.json | 2 +- test/composer/models/system/event.md | 3 +- .../org.hyperledger.composer.system.cto | 4 -- test/composer/models/system/transaction.md | 3 +- test/composer/systemmodel.js | 4 -- test/data/model/customsystem-base.cto | 2 - test/introspect/eventdeclaration.js | 15 ------- 11 files changed, 43 insertions(+), 89 deletions(-) diff --git a/lib/introspect/classdeclaration.js b/lib/introspect/classdeclaration.js index e482984626..fa697db59b 100644 --- a/lib/introspect/classdeclaration.js +++ b/lib/introspect/classdeclaration.js @@ -104,6 +104,23 @@ class ClassDeclaration extends Decorated { } } + /** + * Adds a required field named 'timestamp' of type 'DateTime' if this class declaration does not have a super type. + * This method should only be called by system code. + * @private + */ + addTimestampField() { + // add a timestamp field + if(this.superType === null) { + const definition = {}; + definition.id = {}; + definition.id.name = 'timestamp'; + definition.propertyType = {}; + definition.propertyType.name = 'DateTime'; + this.properties.push(new Field(this, definition)); + } + } + /** * Resolve the super type on this class and store it as an internal property. * @return {ClassDeclaration} The super type, or null if non specified. diff --git a/lib/introspect/eventdeclaration.js b/lib/introspect/eventdeclaration.js index c37e7fc29c..ab839079e5 100644 --- a/lib/introspect/eventdeclaration.js +++ b/lib/introspect/eventdeclaration.js @@ -15,7 +15,6 @@ 'use strict'; const ClassDeclaration = require('./classdeclaration'); -const IllegalModelException = require('./illegalmodelexception'); /** Class representing the definition of an Event. * @extends ClassDeclaration @@ -35,43 +34,24 @@ class EventDeclaration extends ClassDeclaration { } /** - * Returns the base system type for Events from the system namespace + * Process the AST and build the model * - * @return {string} the short name of the base system type + * @throws {IllegalModelException} + * @private */ - getSystemType() { - let systemType = this.modelFile.getModelManager().getSystemModelTable().get('Event'); - return systemType !== undefined ? systemType : null; + process() { + super.process(); + this.addTimestampField(); } /** - * Semantic validation of the structure of this event. Subclasses should - * override this method to impose additional semantic constraints on the - * contents/relations of fields. + * Returns the base system type for Events from the system namespace * - * @throws {IllegalModelException} - * @private + * @return {string} the short name of the base system type */ - validate() { - let systemTypeDeclared = true; - - // If using models without importing system models - try { - this.getModelFile().getType(this.getSystemType()); - } catch (e) { - systemTypeDeclared = false; - } - if(systemTypeDeclared){ - const hasSystemType = this.getSystemType() !== null; - systemTypeDeclared = hasSystemType; - } - - if (!this.isSystemType() && this.idField && systemTypeDeclared) { - throw new IllegalModelException('Event should not specify an identifying field.', this.modelFile, this.ast.location); - } - - // do generic validation after specific validation - super.validate(); + getSystemType() { + let systemType = this.modelFile.getModelManager().getSystemModelTable().get('Event'); + return systemType !== undefined ? systemType : null; } /** diff --git a/lib/introspect/transactiondeclaration.js b/lib/introspect/transactiondeclaration.js index 08aeb10ae0..f375ef02ef 100644 --- a/lib/introspect/transactiondeclaration.js +++ b/lib/introspect/transactiondeclaration.js @@ -15,7 +15,6 @@ 'use strict'; const ClassDeclaration = require('./classdeclaration'); -const IllegalModelException = require('./illegalmodelexception'); /** Class representing the definition of an Transaction. * @extends ClassDeclaration @@ -36,39 +35,24 @@ class TransactionDeclaration extends ClassDeclaration { } /** - * Returns the base system type for Transactions from the system namespace + * Process the AST and build the model * - * @return {string} the short name of the base system type + * @throws {IllegalModelException} + * @private */ - getSystemType() { - let systemType = this.modelFile.getModelManager().getSystemModelTable().get('Transaction'); - return systemType !== undefined ? systemType : null; + process() { + super.process(); + this.addTimestampField(); } /** - * Semantic validation of the structure of this asset. Subclasses should - * override this method to impose additional semantic constraints on the - * contents/relations of fields. + * Returns the base system type for Transactions from the system namespace * - * @throws {IllegalModelException} - * @private + * @return {string} the short name of the base system type */ - validate() { - let systemTypeDeclared = true; - - // If using models without importing system models - try { - this.getModelFile().getType(this.getSystemType()); - } catch (e) { - systemTypeDeclared = false; - } - if(systemTypeDeclared){ - const hasSystemType = this.getSystemType() !== null; - systemTypeDeclared = hasSystemType; - } - - // perform general validation after specific validation. - super.validate(); + getSystemType() { + let systemType = this.modelFile.getModelManager().getSystemModelTable().get('Transaction'); + return systemType !== undefined ? systemType : null; } } diff --git a/package-lock.json b/package-lock.json index f0fbd65f9e..3d88cad71a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "composer-concerto", - "version": "0.31.3", + "version": "0.40.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7ab6947d4e..4f921fd0a9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "composer-concerto", - "version": "0.40.0", + "version": "0.50.0", "description": "Concerto", "engines": { "node": ">=8", diff --git a/test/composer/models/system/event.md b/test/composer/models/system/event.md index 5197d9d7ee..3df8538170 100644 --- a/test/composer/models/system/event.md +++ b/test/composer/models/system/event.md @@ -1,8 +1,7 @@ -The base *event* is implicitly extended by all other events. *Event* is an **abstract** meaning that no instances of it can be created, however, it does contain the *eventId* and *timestamp* properties, which are extended to all other events. +The base *event* is implicitly extended by all other events. *Event* is an **abstract** meaning that no instances of it can be created, however, it does contain the *eventId* property, which is extended to all other events. ``` abstract event Event identified by eventId { o String eventId - o DateTime timestamp } ``` diff --git a/test/composer/models/system/org.hyperledger.composer.system.cto b/test/composer/models/system/org.hyperledger.composer.system.cto index bbcf1379de..30ebb993f9 100644 --- a/test/composer/models/system/org.hyperledger.composer.system.cto +++ b/test/composer/models/system/org.hyperledger.composer.system.cto @@ -34,12 +34,10 @@ abstract participant Participant { } * * * @param {String} transactionId Identifier for this transaction - * @param {DateTime} timestamp Timestamp for this transaction */ @docs('transaction.md') abstract transaction Transaction identified by transactionId { o String transactionId - o DateTime timestamp } /** @@ -48,12 +46,10 @@ abstract transaction Transaction identified by transactionId { * Has two properties that are used set and are accessible in all transactions. * * @param {String} eventId Identifier for this event - * @param {DateTime} timestamp Timestamp for this event */ @docs('event.md') abstract event Event identified by eventId { o String eventId - o DateTime timestamp } /** diff --git a/test/composer/models/system/transaction.md b/test/composer/models/system/transaction.md index 7bd02f8a1b..2605e0f138 100644 --- a/test/composer/models/system/transaction.md +++ b/test/composer/models/system/transaction.md @@ -1,7 +1,6 @@ -The base *transaction* is implicitly extended by all other transactions. *Transaction* is an **abstract** meaning that no instances of it can be created, however, it does contain the *transactionId* and *timestamp* properties, which are extended to all other transactions. +The base *transaction* is implicitly extended by all other transactions. *Transaction* is an **abstract** meaning that no instances of it can be created, however, it does contain the *transactionId* property, which is extended to all other transactions. ``` abstract transaction Transaction identified by transactionId { o String transactionId - o DateTime timestamp ``` diff --git a/test/composer/systemmodel.js b/test/composer/systemmodel.js index 926955c0fa..0f386edb82 100644 --- a/test/composer/systemmodel.js +++ b/test/composer/systemmodel.js @@ -55,12 +55,10 @@ abstract participant Participant { } * * * @param {String} transactionId Identifier for this transaction - * @param {DateTime} timestamp Timestamp for this transaction */ @docs('transaction.md') abstract transaction Transaction identified by transactionId { o String transactionId - o DateTime timestamp } /** @@ -69,12 +67,10 @@ abstract transaction Transaction identified by transactionId { * Has two properties that are used set and are accessible in all transactions. * * @param {String} eventId Identifier for this event - * @param {DateTime} timestamp Timestamp for this event */ @docs('event.md') abstract event Event identified by eventId { o String eventId - o DateTime timestamp } /** diff --git a/test/data/model/customsystem-base.cto b/test/data/model/customsystem-base.cto index 649b7544d9..ab8d9ef140 100644 --- a/test/data/model/customsystem-base.cto +++ b/test/data/model/customsystem-base.cto @@ -10,10 +10,8 @@ participant MyParticipant identified by myParticipantId { transaction MyTransaction identified by myTransactionId { o String myTransactionId - o DateTime timestamp } event MyEvent identified by myEventId { o String myEventId - o DateTime timestamp } \ No newline at end of file diff --git a/test/introspect/eventdeclaration.js b/test/introspect/eventdeclaration.js index a6824611d6..5ab4b32f52 100644 --- a/test/introspect/eventdeclaration.js +++ b/test/introspect/eventdeclaration.js @@ -43,21 +43,6 @@ describe('EventDeclaration', () => { }); describe('#validate', () => { - it('Give an event an id field',()=>{ - let model = ` - namespace com.test - - event E identified by euid { - o String euid - }`; - const modelManager = new ModelManager(); - Util.addComposerSystemModels(modelManager); - (()=>{ - modelManager.addModelFile(model, 'awesome.cto' ); - }) - .should.throw(/Event should not specify an identifying field/); - - }); it('Check the system type',()=>{ let model = `