Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.
8 changes: 4 additions & 4 deletions packages/composer-common/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class BusinessNetworkMetadata {
}
class Factory {
+ void constructor(ModelManager)
+ Resource newResource(string,string,string,Object,boolean,string) throws TypeNotFoundException
+ Resource newConcept(string,string,Object,boolean,string) throws TypeNotFoundException
+ Resource newResource(string,string,string,Object,boolean,string,boolean) throws TypeNotFoundException
+ Resource newConcept(string,string,Object,boolean,string,boolean) throws TypeNotFoundException
+ Relationship newRelationship(string,string,string) throws TypeNotFoundException
+ Resource newTransaction(string,string,string,Object,string)
+ Resource newEvent(string,string,string,Object,string)
+ Resource newTransaction(string,string,string,Object,string,boolean)
+ Resource newEvent(string,string,string,Object,string,boolean)
}
class FileWallet extends Wallet {
+ string getHomeDirectory()
Expand Down
2 changes: 2 additions & 0 deletions packages/composer-common/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#
Version 0.9.2 {60327250ee4f059647020f8aee5ed67b} 2017-07-06
- Added includeOptionalFields option to Factory.newXXX functions

Version 0.9.0 {d0b9ae5179276e20604874624fe9529d} 2017-06-23
- Removed useless toJSON methods everywhere
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class LoopbackVisitor {

// Add information from the class declaration into the composer section.
if (jsonSchema.options && jsonSchema.options.composer) {
jsonSchema.options.composer.namespace = classDeclaration.getModelFile().getNamespace();
jsonSchema.options.composer.namespace = classDeclaration.getNamespace();
jsonSchema.options.composer.name = classDeclaration.getName();
jsonSchema.options.composer.fqn = classDeclaration.getFullyQualifiedName();
}
Expand Down
94 changes: 58 additions & 36 deletions packages/composer-common/lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const EventDeclaration = require('./introspect/eventdeclaration');

const uuid = require('uuid');


/**
* Use the Factory to create instances of Resource: transactions, participants
* and assets.
Expand Down Expand Up @@ -68,6 +67,8 @@ class Factory {
* @param {string} [options.generate] - Pass one of: <dl>
* <dt>sample</dt><dd>return a resource instance with generated sample data.</dd>
* <dt>empty</dt><dd>return a resource instance with empty property values.</dd></dl>
* @param {boolean} [options.includeOptionalFields] - if <code>options.generate</code>
* is specified, whether optional fields should be generated.
* @return {Resource} the new instance
* @throws {TypeNotFoundException} if the type is not registered with the ModelManager
*/
Expand Down Expand Up @@ -112,29 +113,7 @@ class Factory {
newObj = new ValidatedResource(this.modelManager, ns, type, id, new ResourceValidator());
}
newObj.assignFieldDefaults();

if(options.generate) {
let generator;
let includeOptionalFields;
if ((/^empty$/i).test(options.generate)) {
generator = ValueGeneratorFactory.empty();
includeOptionalFields = false;
} else {
generator = ValueGeneratorFactory.sample();
includeOptionalFields = true;
}

const visitor = new InstanceGenerator();
const parameters = {
stack: new TypedStack(newObj),
modelManager: this.modelManager,
factory: this,
valueGenerator: generator,
includeOptionalFields: includeOptionalFields
};

classDecl.accept(visitor, parameters);
}
this.initializeNewObject(newObj, classDecl, options);

// if we have an identifier, we set it now
let idField = classDecl.getIdentifierFieldName();
Expand All @@ -153,6 +132,8 @@ class Factory {
* @param {string} [options.generate] - Pass one of: <dl>
* <dt>sample</dt><dd>return a resource instance with generated sample data.</dd>
* <dt>empty</dt><dd>return a resource instance with empty property values.</dd></dl>
* @param {boolean} [options.includeOptionalFields] - if <code>options.generate</code>
* is specified, whether optional fields should be generated.
* @return {Resource} the new instance
* @throws {TypeNotFoundException} if the type is not registered with the ModelManager
*/
Expand Down Expand Up @@ -181,18 +162,7 @@ class Factory {
newObj = new ValidatedConcept(this.modelManager,ns,type, new ResourceValidator());
}
newObj.assignFieldDefaults();

if(options.generate) {
const visitor = new InstanceGenerator();
const generator = (/^empty$/i).test(options.generate) ? ValueGeneratorFactory.empty() : ValueGeneratorFactory.sample();
const parameters = {
stack: new TypedStack(newObj),
modelManager: this.modelManager,
factory: this,
valueGenerator: generator
};
classDecl.accept(visitor, parameters);
}
this.initializeNewObject(newObj, classDecl, options);

debug('Factory.newResource created concept %s', classDecl.getFullyQualifiedName() );
return newObj;
Expand Down Expand Up @@ -230,6 +200,8 @@ class Factory {
* @param {string} [options.generate] - Pass one of: <dl>
* <dt>sample</dt><dd>return a resource instance with generated sample data.</dd>
* <dt>empty</dt><dd>return a resource instance with empty property values.</dd></dl>
* @param {boolean} [options.includeOptionalFields] - if <code>options.generate</code>
* is specified, whether optional fields should be generated.
* @return {Resource} A resource for the new transaction.
*/
newTransaction(ns, type, id, options) {
Expand Down Expand Up @@ -263,6 +235,8 @@ class Factory {
* @param {string} [options.generate] - Pass one of: <dl>
* <dt>sample</dt><dd>return a resource instance with generated sample data.</dd>
* <dt>empty</dt><dd>return a resource instance with empty property values.</dd></dl>
* @param {boolean} [options.includeOptionalFields] - if <code>options.generate</code>
* is specified, whether optional fields should be generated.
* @return {Resource} A resource for the new event.
*/
newEvent(ns, type, id, options) {
Expand All @@ -285,6 +259,54 @@ class Factory {
return event;
}

/**
* PRIVATE IMPLEMENTATION. DO NOT CALL FROM OUTSIDE THIS CLASS.
*
* Initialize the state of a newly created resource
* @private
* @param {Typed} newObject - resource to initialize.
* @param {ClassDeclaration} classDeclaration - class declaration for the resource.
* @param {Object} clientOptions - field generation options supplied by the caller.
*/
initializeNewObject(newObject, classDeclaration, clientOptions) {
const generateParams = this.parseGenerateOptions(clientOptions);
if (generateParams) {
generateParams.stack = new TypedStack(newObject);
const visitor = new InstanceGenerator();
classDeclaration.accept(visitor, generateParams);
}
}

/**
* PRIVATE IMPLEMENTATION. DO NOT CALL FROM OUTSIDE THIS CLASS.
*
* Parse the client-supplied field generation options and return a corresponding set of InstanceGenerator
* options that can be used to initialize a resource.
* @private
* @param {Object} clientOptions - field generation options supplied by the caller.
* @return {Object} InstanceGenerator options.
*/
parseGenerateOptions(clientOptions) {
if (!clientOptions.generate) {
return null;
}

const generateParams = { };
generateParams.modelManager = this.modelManager;
generateParams.factory = this;

if ((/^empty$/i).test(clientOptions.generate)) {
generateParams.valueGenerator = ValueGeneratorFactory.empty();
} else {
// Allow any other value for backwards compatibility with previous (truthy) behavior
generateParams.valueGenerator = ValueGeneratorFactory.sample();
}

generateParams.includeOptionalFields = clientOptions.includeOptionalFields ? true : false;

return generateParams;
}

}

module.exports = Factory;
14 changes: 11 additions & 3 deletions packages/composer-common/lib/introspect/classdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class ClassDeclaration {
// we now validate the field, however to ensure that
// imports are resolved correctly we validate in the context
// of the declared type of the field for non-primitives in a different namespace
if(field.isPrimitive() || this.isEnum() || field.getNamespace() === this.getModelFile().getNamespace() ) {
if(field.isPrimitive() || this.isEnum() || field.getNamespace() === this.getNamespace() ) {
field.validate(this);
}
else {
Expand Down Expand Up @@ -304,7 +304,7 @@ class ClassDeclaration {
* @return {boolean} true if the class may be pointed to by a relationship
*/
isSystemType() {
return ModelUtil.getSystemNamespace() === this.modelFile.getNamespace();
return ModelUtil.getSystemNamespace() === this.getNamespace();
}

/**
Expand All @@ -317,14 +317,22 @@ class ClassDeclaration {
return this.name;
}

/**
* Return the namespace of this class.
* @return {String} namespace - a namespace.
*/
getNamespace() {
return this.modelFile.getNamespace();
}

/**
* Returns the fully qualified name of this class.
* The name will include the namespace if present.
*
* @return {string} the fully-qualified name of this class
*/
getFullyQualifiedName() {
return this.modelFile.getNamespace() + '.' + this.name;
return this.getNamespace() + '.' + this.name;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/composer-common/lib/introspect/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ class Property {
* @return {string} the fully qualified name of this property
*/
getFullyQualifiedName() {
return this.getNamespace() + '.' + this.getParent().getName() + '.' + this.getName();
return this.getParent().getFullyQualifiedName() + '.' + this.getName();
}

/**
* Returns the namespace of the parent of this property
* @return {string} the namespace of the parent of this property
*/
getNamespace() {
return this.getParent().getModelFile().getNamespace();
return this.getParent().getNamespace();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class RelationshipDeclaration extends Property {
if(ModelUtil.isPrimitiveType(this.getType())) {
throw new IllegalModelException('Relationship ' + this.getName() + ' cannot be to the primitive type ' + this.getType(), classDecl.getModelFile(), this.ast.location );
} else {
let namespace = this.getParent().getModelFile().getNamespace();
let namespace = this.getParent().getNamespace();

// we first try to get the type from our own model file
// because during validate we have not yet been added to the model manager
Expand Down
3 changes: 2 additions & 1 deletion packages/composer-common/lib/model/typed.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'use strict';

const Field = require('../introspect/field');
const ModelUtil = require('../modelutil');

/**
* Object is an instance with a namespace and a type.
Expand Down Expand Up @@ -76,7 +77,7 @@ class Typed {
* @return {string} The fully-qualified type name of this object
*/
getFullyQualifiedType() {
return this.$namespace + '.' + this.$type;
return ModelUtil.getFullyQualifiedName(this.$namespace, this.$type);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/composer-common/lib/modelutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class ModelUtil {
*/
static getFullyQualifiedName(namespace, type) {
if (namespace) {
return namespace + '.' + type;
return `${namespace}.${type}`;
} else {
return type;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/composer-common/lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ class Serializer {
// create a new instance, using the identifier field name as the ID.
let resource;
if (classDeclaration instanceof TransactionDeclaration) {
resource = this.factory.newTransaction( classDeclaration.getModelFile().getNamespace(),
resource = this.factory.newTransaction( classDeclaration.getNamespace(),
classDeclaration.getName(),
jsonObject[classDeclaration.getIdentifierFieldName()] );
} else if (classDeclaration instanceof EventDeclaration) {
resource = this.factory.newEvent( classDeclaration.getModelFile().getNamespace(),
resource = this.factory.newEvent( classDeclaration.getNamespace(),
classDeclaration.getName(),
jsonObject[classDeclaration.getIdentifierFieldName()] );
} else {
resource = this.factory.newResource( classDeclaration.getModelFile().getNamespace(),
resource = this.factory.newResource( classDeclaration.getNamespace(),
classDeclaration.getName(),
jsonObject[classDeclaration.getIdentifierFieldName()] );
}
Expand Down
Loading