Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Remove Ability to Customise the System Model(s) #164

Closed
wants to merge 9 commits into from
18 changes: 3 additions & 15 deletions packages/concerto-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ require('yargs')
describe: 'sample JSON to validate',
type: 'string'
});
yargs.option('ctoSystem', {
describe: 'system model to be used',
type: 'string'
});
yargs.option('ctoFiles', {
describe: 'array of CTO files',
type: 'string',
Expand All @@ -45,7 +41,7 @@ require('yargs')

try {
argv = Commands.validateValidateArgs(argv);
return Commands.validate(argv.sample, argv.ctoSystem, argv.ctoFiles)
return Commands.validate(argv.sample, argv.ctoFiles)
.then((result) => {
Logger.info(result);
})
Expand All @@ -59,10 +55,6 @@ require('yargs')
})
.command('compile', 'generate code for a target platform', (yargs) => {
yargs.demandOption(['ctoFiles'], 'Please provide at least the CTO files');
yargs.option('ctoSystem', {
describe: 'system model to be used',
type: 'string'
});
yargs.option('ctoFiles', {
describe: 'array of CTO files',
type: 'string',
Expand All @@ -83,7 +75,7 @@ require('yargs')
Logger.info(`generate code for target ${argv.target} from models ${argv.ctoFiles} into directory: ${argv.output}`);
}

return Commands.compile(argv.target, argv.ctoSystem, argv.ctoFiles, argv.output)
return Commands.compile(argv.target, argv.ctoFiles, argv.output)
.then((result) => {
Logger.info(result);
})
Expand All @@ -98,10 +90,6 @@ require('yargs')
type: 'string',
array: true
});
yargs.option('ctoSystem', {
describe: 'system model to be used',
type: 'string'
});
yargs.option('output', {
describe: 'output directory path',
type: 'string',
Expand All @@ -112,7 +100,7 @@ require('yargs')
Logger.info(`saving external models from ${argv.ctoFiles} into directory: ${argv.output}`);
}

return Commands.get(argv.ctoSystem, argv.ctoFiles, argv.output)
return Commands.get(argv.ctoFiles, argv.output)
.then((result) => {
Logger.info(result);
})
Expand Down
15 changes: 6 additions & 9 deletions packages/concerto-cli/lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,13 @@ class Commands {
* Validate a sample JSON against the model
*
* @param {string} sample - the sample to validate
* @param {string} ctoSystemFile - the system model file
* @param {string[]} ctoFiles - the CTO files to convert to code
* @returns {string} serialized form of the validated JSON
*/
static async validate(sample, ctoSystemFile, ctoFiles) {
static async validate(sample, ctoFiles) {
const json = JSON.parse(fs.readFileSync(sample, 'utf8'));

const modelManager = await ModelLoader.loadModelManager(ctoSystemFile, ctoFiles);
const modelManager = await ModelLoader.loadModelManager(ctoFiles);
const factory = new Factory(modelManager);
const serializer = new Serializer(factory, modelManager);

Expand All @@ -116,12 +115,11 @@ class Commands {
* Compile the model for a given target
*
* @param {string} target - the target of the code to compile
* @param {string} ctoSystemFile - the system model file
* @param {string[]} ctoFiles - the CTO files to convert to code
* @param {string} output the output directory
*/
static async compile(target, ctoSystemFile, ctoFiles, output) {
const modelManager = await ModelLoader.loadModelManager(ctoSystemFile, ctoFiles);
static async compile(target, ctoFiles, output) {
const modelManager = await ModelLoader.loadModelManager(ctoFiles);

let visitor = null;

Expand Down Expand Up @@ -161,12 +159,11 @@ class Commands {
* Fetches all external for a set of models dependencies and
* saves all the models to a target directory
*
* @param {string} ctoSystemFile the system model
* @param {string[]} ctoFiles the CTO files (can be local file paths or URLs)
* @param {string} output the output directory
*/
static async get(ctoSystemFile, ctoFiles, output) {
const modelManager = await ModelLoader.loadModelManager(ctoSystemFile, ctoFiles);
static async get(ctoFiles, output) {
const modelManager = await ModelLoader.loadModelManager(ctoFiles);
mkdirp.sync(output);
modelManager.writeModelsToFileSystem(output);
return `Loaded external models in '${output}'.`;
Expand Down
32 changes: 11 additions & 21 deletions packages/concerto-cli/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const Commands = require('../lib/commands');

describe('cicero-cli', () => {
const models = [path.resolve(__dirname, 'models/dom.cto'),path.resolve(__dirname, 'models/money.cto')];
const hlModel = path.resolve(__dirname, 'models/org.hyperledger.composer.system.cto');
const sample1 = path.resolve(__dirname, 'data/sample1.json');
const sample2 = path.resolve(__dirname, 'data/sample2.json');
const sampleText1 = fs.readFileSync(sample1, 'utf8');
Expand All @@ -54,13 +53,13 @@ describe('cicero-cli', () => {

describe('#validate', () => {
it('should validate against a model', async () => {
const result = await Commands.validate(sample1, null, models);
const result = await Commands.validate(sample1, models);
JSON.parse(result).should.deep.equal(JSON.parse(sampleText1));
});

it('should fail to validate against a model', async () => {
try {
const result = await Commands.validate(sample2, null, models);
const result = await Commands.validate(sample2, models);
JSON.parse(result).should.deep.equal(JSON.parse(sampleText2));
} catch (err) {
err.message.should.equal('Instance undefined invalid enum value true for field CurrencyCode');
Expand Down Expand Up @@ -96,43 +95,43 @@ describe('cicero-cli', () => {

it('should compile to a Go model', async () => {
const dir = await tmp.dir({ unsafeCleanup: true});
await Commands.compile('Go', null, models, dir.path);
await Commands.compile('Go', models, dir.path);
fs.readdirSync(dir.path).length.should.be.above(0);
dir.cleanup();
});
it('should compile to a PlantUML model', async () => {
const dir = await tmp.dir({ unsafeCleanup: true});
await Commands.compile('PlantUML', null, models, dir.path);
await Commands.compile('PlantUML', models, dir.path);
fs.readdirSync(dir.path).length.should.be.above(0);
dir.cleanup();
});
it('should compile to a Typescript model', async () => {
const dir = await tmp.dir({ unsafeCleanup: true});
await Commands.compile('Typescript', null, models, dir.path);
await Commands.compile('Typescript', models, dir.path);
fs.readdirSync(dir.path).length.should.be.above(0);
dir.cleanup();
});
it('should compile to a Java model', async () => {
const dir = await tmp.dir({ unsafeCleanup: true});
await Commands.compile('Java', null, models, dir.path);
await Commands.compile('Java', models, dir.path);
fs.readdirSync(dir.path).length.should.be.above(0);
dir.cleanup();
});
it('should compile to a JSONSchema model', async () => {
const dir = await tmp.dir({ unsafeCleanup: true});
await Commands.compile('JSONSchema', null, models, dir.path);
await Commands.compile('JSONSchema', models, dir.path);
fs.readdirSync(dir.path).length.should.be.above(0);
dir.cleanup();
});
it('should compile to a XMLSchema model', async () => {
const dir = await tmp.dir({ unsafeCleanup: true});
await Commands.compile('XMLSchema', null, models, dir.path);
await Commands.compile('XMLSchema', models, dir.path);
fs.readdirSync(dir.path).length.should.be.above(0);
dir.cleanup();
});
it('should not compile to an unknown model', async () => {
const dir = await tmp.dir({ unsafeCleanup: true});
await Commands.compile('BLAH', null, models, dir.path);
await Commands.compile('BLAH', models, dir.path);
fs.readdirSync(dir.path).length.should.be.equal(0);
dir.cleanup();
});
Expand All @@ -141,7 +140,7 @@ describe('cicero-cli', () => {
describe('#get', () => {
it('should save external dependencies', async () => {
const dir = await tmp.dir({ unsafeCleanup: true});
await Commands.get(null, models, dir.path);
await Commands.get(models, dir.path);
fs.readdirSync(dir.path).should.eql([
'@models.accordproject.org.cicero.contract.cto',
'dom.cto',
Expand All @@ -152,7 +151,7 @@ describe('cicero-cli', () => {

it('should save external dependencies for an external model', async () => {
const dir = await tmp.dir({ unsafeCleanup: true});
await Commands.get(null,['https://models.accordproject.org/patents/patent.cto'], dir.path);
await Commands.get(['https://models.accordproject.org/patents/patent.cto'], dir.path);
fs.readdirSync(dir.path).should.eql([
'@models.accordproject.org.address.cto',
'@models.accordproject.org.geo.cto',
Expand All @@ -166,14 +165,5 @@ describe('cicero-cli', () => {
]);
dir.cleanup();
});

it('should fail saving external dependencies for an external model but with the wrong system model', async () => {
const dir = await tmp.dir({ unsafeCleanup: true});
try {
await Commands.get(hlModel,['https://models.accordproject.org/patents/patent.cto'], dir.path);
} catch (err) {
err.message.should.contain('Relationship transactionInvoked must be to an asset or participant, but is to org.hyperledger.composer.system.Transaction');
}
});
});
});
11 changes: 3 additions & 8 deletions packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Factory {
}
class AssetDeclaration extends ClassDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ boolean isRelationshipTarget()
+ string getSystemType()
+ boolean hasInstance(object)
}
Expand All @@ -30,9 +29,6 @@ class ClassDeclaration extends Decorated {
+ boolean isEnum()
+ boolean isConcept()
+ boolean isEvent()
+ boolean isRelationshipTarget()
+ boolean isSystemRelationshipTarget()
+ boolean isSystemType()
+ boolean isSystemCoreType()
+ string getName()
+ String getNamespace()
Expand Down Expand Up @@ -112,7 +108,6 @@ class ModelFile {
}
class ParticipantDeclaration extends ClassDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ boolean isRelationshipTarget()
+ string getSystemType()
+ boolean hasInstance(object)
}
Expand Down Expand Up @@ -181,15 +176,15 @@ class ValidatedResource extends Resource {
+ void validate() throws Error
}
class ModelLoader {
+ object loadModelManager(string,string[])
+ object loadModelManager(string[])
}
class ModelManager {
+ void constructor()
+ void validateModelFile(string,string) throws IllegalModelException
+ Object addModelFile(string,string,boolean,boolean) throws IllegalModelException
+ Object addModelFile(string,string,boolean) throws IllegalModelException
+ Object updateModelFile(string,string,boolean) throws IllegalModelException
+ void deleteModelFile(string)
+ Object[] addModelFiles(string[],undefined,boolean,boolean)
+ Object[] addModelFiles(string[],undefined,boolean)
+ void validateModelFiles()
+ Promise updateExternalModels(Object,ModelFileDownloader) throws IllegalModelException
+ void writeModelsToFileSystem(String,Object,boolean,boolean)
Expand Down
4 changes: 4 additions & 0 deletions packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 0.82.6 {f3b64bf831a39a8165bd95121da685a3} 2020-02-02
- Remove notion of system table
- Remove user ability to change system model

Version 0.82.5 {d4d49fabba1c6ce465c446e2dccad488} 2019-11-10
- Add instance of alternatives to Factory and Serializer

Expand Down
12 changes: 1 addition & 11 deletions packages/concerto-core/lib/introspect/assetdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,13 @@ class AssetDeclaration extends ClassDeclaration {
this._isAssetDeclaration = true;
}

/**
* Returns true if this class can be pointed to by a relationship
*
* @return {boolean} true if the class may be pointed to by a relationship
*/
isRelationshipTarget() {
return true;
}

/**
* Returns the base system type for Assets from the system namespace
*
* @return {string} the short name of the base system type
*/
getSystemType() {
let systemType = this.modelFile.getModelManager().getSystemModelTable().get('Asset');
return systemType !== undefined ? systemType : null;
return 'Asset';
}

/**
Expand Down
31 changes: 1 addition & 30 deletions packages/concerto-core/lib/introspect/classdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,43 +308,14 @@ class ClassDeclaration extends Decorated {
return false;
}

/**
* Returns true if this class can be pointed to by a relationship
*
* @return {boolean} true if the class may be pointed to by a relationship
*/
isRelationshipTarget() {
return false;
}

/**
* Returns true if this class can be pointed to by a relationship in a
* system model
*
* @return {boolean} true if the class may be pointed to by a relationship
*/
isSystemRelationshipTarget() {
return this.isRelationshipTarget();
}

/**
* Returns true is this type is in the system namespace
*
* @return {boolean} true if the class may be pointed to by a relationship
*/
isSystemType() {
return this.modelFile.isSystemModelFile();
}

/**
* Returns true if this class is a system core type - both in the system
* namespace, and also one of the system core types (Asset, Participant, etc).
*
* @return {boolean} true if the class may be pointed to by a relationship
*/
isSystemCoreType() {
return this.isSystemType() &&
this.getSystemType() === this.getName();
return this.getSystemType() === this.getName();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/concerto-core/lib/introspect/eventdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class EventDeclaration extends ClassDeclaration {
* @return {string} the short name of the base system type
*/
getSystemType() {
let systemType = this.modelFile.getModelManager().getSystemModelTable().get('Event');
return systemType !== undefined ? systemType : null;
return 'Event';
}

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/concerto-core/lib/introspect/modelfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ class ModelFile {
* @private
*/
validate() {

// Validate all of the imports to check that they reference
// namespaces or types that actually exist.
this.imports.forEach((importName) => {
Expand Down Expand Up @@ -580,7 +579,7 @@ class ModelFile {
let result = [];
for(let n=0; n < this.declarations.length; n++) {
let classDeclaration = this.declarations[n];
if(classDeclaration instanceof type && (includeSystemType || !classDeclaration.isSystemType())) {
if(classDeclaration instanceof type && (includeSystemType || !classDeclaration.isSystemCoreType())) {
result.push(classDeclaration);
}
}
Expand Down
12 changes: 1 addition & 11 deletions packages/concerto-core/lib/introspect/participantdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,13 @@ class ParticipantDeclaration extends ClassDeclaration {
this._isParticipantDeclaration = true;
}

/**
* Returns true if this class can be pointed to by a relationship
*
* @return {boolean} true if the class may be pointed to by a relationship
*/
isRelationshipTarget() {
return true;
}

/**
* Returns the base system type for Participants from the system namespace
*
* @return {string} the short name of the base system type
*/
getSystemType() {
let systemType = this.modelFile.getModelManager().getSystemModelTable().get('Participant');
return systemType !== undefined ? systemType : null;
return 'Participant';
}

/**
Expand Down
Loading