Skip to content

Commit ef22ae9

Browse files
authored
Merge pull request #115 from dselman/contract-as-composer-object
(fix) contract data should be a Composer object
2 parents 86b53bc + be28047 commit ef22ae9

File tree

9 files changed

+33
-14
lines changed

9 files changed

+33
-14
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cicero-cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"@accordproject/cicero-core": "0.3.14",
5050
"@accordproject/cicero-engine": "0.3.14",
5151
"@accordproject/ergo-compiler": "0.0.48",
52-
"composer-common": "0.19.5",
52+
"composer-common": "0.19.6",
5353
"yargs": "9.0.1"
5454
},
5555
"license-check-config": {

packages/cicero-common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"nyc": "11.7.2"
4343
},
4444
"dependencies": {
45-
"composer-common": "0.19.5"
45+
"composer-common": "0.19.6"
4646
},
4747
"license-check-config": {
4848
"src": [

packages/cicero-core/lib/templateinstance.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ class TemplateInstance {
4040
}
4141
this.template = template;
4242
this.data = null;
43+
this.composerData = null;
4344
}
4445

4546
/**
4647
* Set the data for the clause
4748
* @param {object} data - the data for the clause, must be an instance of the
48-
* template model for the clause's template
49+
* template model for the clause's template. This should be a plain JS object
50+
* and will be deserialized and validated into the Composer object before assignment.
4951
*/
5052
setData(data) {
5153
// verify that data is an instance of the template model
@@ -57,21 +59,34 @@ class TemplateInstance {
5759

5860
// downloadExternalDependencies the data using the template model
5961
logger.debug('Setting clause data: ' + JSON.stringify(data));
60-
const resource = this.template.getSerializer().fromJSON(data);
62+
const resource = this.getTemplate().getSerializer().fromJSON(data);
6163
resource.validate();
6264

63-
// passed validation!
65+
// save the data
6466
this.data = data;
67+
68+
// save the composer data
69+
this.composerData = resource;
6570
}
6671

6772
/**
68-
* Get the data for the clause
73+
* Get the data for the clause. This is a plain JS object. To retrieve the Composer
74+
* object call getComposerData().
6975
* @return {object} - the data for the clause, or null if it has not been set
7076
*/
7177
getData() {
7278
return this.data;
7379
}
7480

81+
/**
82+
* Get the data for the clause. This is a Composer object. To retrieve the
83+
* plain JS object suitable for serialization call toJSON() and retrieve the `data` property.
84+
* @return {object} - the data for the clause, or null if it has not been set
85+
*/
86+
getDataAsComposerObject() {
87+
return this.composerData;
88+
}
89+
7590
/**
7691
* Set the data for the clause by parsing natural language text.
7792
* @param {string} text - the data for the clause
@@ -107,7 +122,7 @@ class TemplateInstance {
107122
let hash = '';
108123

109124
if (this.data) {
110-
const textToHash = JSON.stringify(this.data);
125+
const textToHash = JSON.stringify(this.getData());
111126
const hasher = crypto.createHash('sha256');
112127
hasher.update(textToHash);
113128
hash = '-' + hasher.digest('hex');

packages/cicero-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"dependencies": {
5151
"@accordproject/cicero-common": "0.3.14",
5252
"@accordproject/ergo-compiler": "0.0.48",
53-
"composer-common": "0.19.5",
53+
"composer-common": "0.19.6",
5454
"debug": "2.6.2",
5555
"glob": "7.1.2",
5656
"ietf-language-tag-regex": "0.0.5",

packages/cicero-core/test/clause.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ describe('Clause', () => {
7171
};
7272
clause.setData(data);
7373
clause.getData().should.eql(data);
74+
75+
// check that the composer data is really a Composer object
76+
clause.getDataAsComposerObject().getFullyQualifiedType().should.be.equal('io.clause.latedeliveryandpenalty.TemplateModel');
7477
});
7578
it('should throw error for bad $class', async function() {
7679
const template = await Template.fromDirectory('./test/data/latedeliveryandpenalty');

packages/cicero-engine/lib/engine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class Engine {
153153
this.compileJsClause(clause);
154154
script = this.scripts[clause.getIdentifier()];
155155

156-
const validContract = clause.getData();
156+
const validContract = clause.getDataAsComposerObject();
157157
const factory = template.getFactory();
158158
const vm = new VM({
159159
timeout: 1000,

packages/cicero-engine/lib/logger.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class Logger {
4747
keys.forEach(function(key) {
4848
let element = obj[key];
4949
if(element.getType) {
50-
printable[key] = this.serializer.toJSON(element, {validate: false, permitResourcesForRelationships: true});
50+
// TODO (DCS) call toJSON once that works for concepts
51+
printable[key] = element.getFullyQualifiedType();
5152
}
5253
else {
5354
printable[key] = element;

packages/cicero-engine/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
},
4545
"dependencies": {
4646
"@accordproject/cicero-core": "0.3.14",
47-
"composer-common": "0.19.5",
47+
"composer-common": "0.19.6",
4848
"moment": "2.20.1",
4949
"vm2": "3.5.0"
5050
},

0 commit comments

Comments
 (0)