Skip to content

Commit

Permalink
Add automatic timestamp field for transactions and events (#24)
Browse files Browse the repository at this point in the history
* refactor modelmanager to standadlone module

Signed-off-by: Dan Selman <[email protected]>

* bump versions, fix linting

Signed-off-by: Dan Selman <[email protected]>

* ignore

Signed-off-by: Dan Selman <[email protected]>

* wip

Signed-off-by: Dan Selman <[email protected]>

* wip

Signed-off-by: Dan Selman <[email protected]>

* Update travis build scripts

Signed-off-by: Matt Roberts <[email protected]>

* Add webpack build
Signed-off-by: Matt Roberts <[email protected]>

* Update .travis.yml

Signed-off-by: Matt Roberts <[email protected]>

* replaced logger with debug

Signed-off-by: Dan Selman <[email protected]>

* removed wallet. Some codecov clean up

Signed-off-by: Dan Selman <[email protected]>

* WIP Remove all references to static system model.

Signed-off-by: Matt Roberts <[email protected]>

* fix changelog

Signed-off-by: Dan Selman <[email protected]>

* Fix xmlschemavisitor. Increase test coverage

Signed-off-by: Matt Roberts <[email protected]>

* Revert nyc output to text-summary

Signed-off-by: Matt Roberts <[email protected]>

* Add pkgbump. Fix webpack. Fix missing moment dependency

* Fix version number

* Add system model table (#5)

* WIP
Signed-off-by: Matt Roberts <[email protected]>

* Client app has to add system models
Signed-off-by: Matt Roberts <[email protected]>

* Add system model table

Signed-off-by: Matt Roberts <[email protected]>

* Move composer models from lib to test. Add test for custom system model

Signed-off-by: Matt Roberts <[email protected]>

* Remove commented code in assetdeclaration

Signed-off-by: Matt Roberts <[email protected]>

* switch to yarn

Signed-off-by: Dan Selman <[email protected]>

* travis build

Signed-off-by: Dan Selman <[email protected]>

* remove system model gen

Signed-off-by: Dan Selman <[email protected]>

* first published version

Signed-off-by: Dan Selman <[email protected]>

* (doc) update description

Signed-off-by: Dan Selman <[email protected]>

* (chore) update docs

Signed-off-by: Dan Selman <[email protected]>

* (chore) cleaning

Signed-off-by: Dan Selman <[email protected]>

* (chore) publish latest version

Signed-off-by: Dan Selman <[email protected]>

* allow transactions to specify an identifying field #21

Signed-off-by: Dan Selman <[email protected]>

* bump version

Signed-off-by: Dan Selman <[email protected]>

* Automatically add a timestamp field to transactions and events

Signed-off-by: Dan Selman <[email protected]>
  • Loading branch information
dselman authored Oct 29, 2018
1 parent fbfa47c commit 5da3e23
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 89 deletions.
17 changes: 17 additions & 0 deletions lib/introspect/classdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 11 additions & 31 deletions lib/introspect/eventdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
'use strict';

const ClassDeclaration = require('./classdeclaration');
const IllegalModelException = require('./illegalmodelexception');

/** Class representing the definition of an Event.
* @extends ClassDeclaration
Expand All @@ -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;
}

/**
Expand Down
38 changes: 11 additions & 27 deletions lib/introspect/transactiondeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
'use strict';

const ClassDeclaration = require('./classdeclaration');
const IllegalModelException = require('./illegalmodelexception');

/** Class representing the definition of an Transaction.
* @extends ClassDeclaration
Expand All @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "composer-concerto",
"version": "0.40.0",
"version": "0.50.0",
"description": "Concerto",
"engines": {
"node": ">=8",
Expand Down
3 changes: 1 addition & 2 deletions test/composer/models/system/event.md
Original file line number Diff line number Diff line change
@@ -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
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand All @@ -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
}

/**
Expand Down
3 changes: 1 addition & 2 deletions test/composer/models/system/transaction.md
Original file line number Diff line number Diff line change
@@ -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
```
4 changes: 0 additions & 4 deletions test/composer/systemmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
/**
Expand All @@ -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
}
/**
Expand Down
2 changes: 0 additions & 2 deletions test/data/model/customsystem-base.cto
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 0 additions & 15 deletions test/introspect/eventdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down

0 comments on commit 5da3e23

Please sign in to comment.