Skip to content

Commit

Permalink
Added in retrofit phase in model processing (hyperledger-archives#1170)
Browse files Browse the repository at this point in the history
* Added in retrofit phase in model processing

Signed-off-by: Matthew B White <[email protected]>

* first pass updates

* make timestamp a system property
  • Loading branch information
mbwhite authored Jun 6, 2017
1 parent b60d3b0 commit 23922bf
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/composer-common/lib/introspect/classdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ class ClassDeclaration {
}
return 'ClassDeclaration {id=' + this.getFullyQualifiedName() + superType + ' enum=' + this.isEnum() + ' abstract=' + this.isAbstract() + '}';
}

/** fixes up the model, post-process*/
retrofit(){}
}

module.exports = ClassDeclaration;
10 changes: 10 additions & 0 deletions packages/composer-common/lib/introspect/modelfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ class ModelFile {
return this.imports;
}

/** retrofit the model
*/
retrofit() {
// Validate all of the types in this model file.
for(let n=0; n < this.declarations.length; n++) {
let classDeclaration = this.declarations[n];
classDeclaration.retrofit();
}
}

/**
* Validates the ModelFile.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/composer-common/lib/log/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ class Logger {
// _root = new Node('root',false);
_tree = new Tree();

const regex = /(-?)concerto:(.*)?/;
const regex = /(-?)composer:(.*)?/;
// now we have an array of the elements that we might need to be enabled
//
for (let i=0; i< details.length;i++){
Expand Down
14 changes: 14 additions & 0 deletions packages/composer-common/lib/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ class ModelManager {
if (typeof modelFile === 'string') {
let m = new ModelFile(this, modelFile, fileName);
m.validate();
m.retrofit();
this.modelFiles[m.getNamespace()] = m;
return m;
} else {
modelFile.validate();
modelFile.retrofit();
this.modelFiles[modelFile.getNamespace()] = modelFile;
return modelFile;
}
Expand All @@ -124,13 +126,15 @@ class ModelManager {
throw new Error('model file does not exist');
}
m.validate();
m.retrofit();
this.modelFiles[m.getNamespace()] = m;
return m;
} else {
if (!this.modelFiles[modelFile.getNamespace()]) {
throw new Error('model file does not exist');
}
modelFile.validate();
modelFile.retrofit();
this.modelFiles[modelFile.getNamespace()] = modelFile;
return modelFile;
}
Expand Down Expand Up @@ -186,6 +190,16 @@ class ModelManager {
this.modelFiles[ns].validate();
}

// let's go and retrofit the model to make sure all is good
// make sure that models are all correctly/
// temp workaround until system models in place
for (let ns in this.modelFiles) {
if (! this.modelFiles[ns] === undefined) {
this.modelFiles[ns].retrofit();
}
}


// return the model files.
return newModelFiles;
}
Expand Down
14 changes: 14 additions & 0 deletions packages/composer-common/test/codegen/loopbackvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,15 @@ describe('LoopbackVisitor', () => {
}]);
});

// TODO: Added a timestamp here as that is now added most model parse...
// when System models are ready remove this.
// GITHUB: composer/issues/920
it('should generate a schema for a transaction with just an identifier', () => {
const modelFile = new ModelFile(modelManager, `
namespace org.acme
transaction MyTransaction identified by transactionId {
o String transactionId
}
`);
const schemas = modelFile.accept(visitor, { fileWriter: mockFileWriter });
Expand Down Expand Up @@ -560,14 +564,20 @@ describe('LoopbackVisitor', () => {
}]);
});


// TODO: Added a timestamp here as that is now added most model parse...
// when System models are ready remove this.
// GITHUB: composer/issues/920
it('should generate two schemas for a transaction that extends another transaction', () => {
const modelFile = new ModelFile(modelManager, `
namespace org.acme
transaction MyBaseTransaction identified by transactionId {
o String transactionId
}
transaction MyTransaction extends MyBaseTransaction {
o String theValue
}
`);
const schemas = modelFile.accept(visitor, { fileWriter: mockFileWriter });
Expand Down Expand Up @@ -656,11 +666,15 @@ describe('LoopbackVisitor', () => {
}]);
});

// TODO: Added a timestamp here as that is now added most model parse...
// when System models are ready remove this.
// GITHUB: composer/issues/920
it('should generate one schema for a transaction that extends an abstract transaction', () => {
const modelFile = new ModelFile(modelManager, `
namespace org.acme
abstract transaction MyBaseTransaction identified by transactionId {
o String transactionId
}
transaction MyTransaction extends MyBaseTransaction {
o String theValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ participant BaseMember identified by id {
transaction BaseTransaction identified by id {
o String id
--> BaseMember invoker
}
}
23 changes: 23 additions & 0 deletions packages/composer-common/test/introspect/modelfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ describe('ModelFile', () => {

});

// describe('#retrofit', () => {
//
// it('should complete succefully when having a transaction', () => {
// const model1 = `
// namespace org.acme.boilerplate
// import org.acme.core.basetx
// transaction tr1 extends basetx {
// }`;
// const model2 = `
// namespace org.acme.core
//
// abstract transaction basetx identified by id {
// o String id
// }`;
// let modelFile1 = new ModelFile(mockModelManager, model1);
// let modelFile2 = new ModelFile(mockModelManager, model2);
// modelFile2.retrofit();
// modelFile1.retrofit();
// });
//
//
// } );

describe('#validate', () => {

it('should throw if an import exists for an invalid namespace', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/composer-common/test/models/dependencies2.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('Dependencies2 Model', function() {

// const participant = factory.newResource('org.acme.base', 'ClientAdminMember', 'testadmin');
const transaction = factory.newTransaction('org.acme.core', 'BaseTransaction', 'testing');
console.log(transaction);
transaction.invoker = factory.newRelationship('org.acme.base', 'ClientAdminMember', 'testadmin');
transaction.validate();
const jsonObj = serializer.toJSON(transaction);
Expand Down

0 comments on commit 23922bf

Please sign in to comment.