-
Notifications
You must be signed in to change notification settings - Fork 120
/
template.js
439 lines (393 loc) · 15.1 KB
/
template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const Metadata = require('./metadata');
const Logger = require('@accordproject/concerto-core').Logger;
const ParserManager = require('./parsermanager');
const crypto = require('crypto');
const stringify = require('json-stable-stringify');
const LogicManager = require('@accordproject/ergo-compiler').LogicManager;
const TemplateLoader = require('./templateloader');
const TemplateSaver = require('./templatesaver');
/**
* A template for a legal clause or contract. A Template has a template model, request/response transaction types,
* a template grammar (natural language for the template) as well as Ergo code for the business logic of the
* template.
* @class
* @public
* @abstract
*/
class Template {
/**
* Create the Template.
* Note: Only to be called by framework code. Applications should
* retrieve instances from {@link Template.fromArchive} or {@link Template.fromDirectory}.
* @param {object} packageJson - the JS object for package.json
* @param {String} readme - the readme in markdown for the template (optional)
* @param {object} samples - the sample text for the template in different locales
* @param {object} request - the JS object for the sample request
* @param {Object} options - e.g., { warnings: true }
*/
constructor(packageJson, readme, samples, request, options) {
this.metadata = new Metadata(packageJson, readme, samples, request);
this.logicManager = new LogicManager('cicero', null, options);
this.parserManager = new ParserManager(this);
}
/**
* Verifies that the template is well formed.
* Throws an exception with the details of any validation errors.
*/
validate() {
this.getModelManager().validateModelFiles();
this.getTemplateModel();
this.getLogicManager().compileLogicSync(true);
}
/**
* Returns the template model for the template
* @throws {Error} if no template model is found, or multiple template models are found
* @returns {ClassDeclaration} the template model for the template
*/
getTemplateModel() {
let modelType = 'org.accordproject.cicero.contract.AccordContract';
if(this.getMetadata().getTemplateType() !== 0) {
modelType = 'org.accordproject.cicero.contract.AccordClause';
}
const templateModels = this.getIntrospector().getClassDeclarations().filter((item) => {
return !item.isAbstract() && Template.instanceOf(item,modelType);
});
if (templateModels.length > 1) {
throw new Error(`Found multiple instances of ${modelType} in ${this.metadata.getName()}. The model for the template must contain a single asset that extends ${modelType}.`);
} else if (templateModels.length === 0) {
throw new Error(`Failed to find an asset that extends ${modelType} in ${this.metadata.getName()}. The model for the template must contain a single asset that extends ${modelType}.`);
} else {
return templateModels[0];
}
}
/**
* Returns the identifier for this template
* @return {String} the identifier of this template
*/
getIdentifier() {
return this.getMetadata().getIdentifier();
}
/**
* Returns the metadata for this template
* @return {Metadata} the metadata for this template
*/
getMetadata() {
return this.metadata;
}
/**
* Returns the name for this template
* @return {String} the name of this template
*/
getName() {
return this.getMetadata().getName();
}
/**
* Returns the display name for this template.
* @return {string} the display name of the template
*/
getDisplayName() {
return this.getMetadata().getDisplayName();
}
/**
* Returns the version for this template
* @return {String} the version of this template. Use semver module
* to parse.
*/
getVersion() {
return this.getMetadata().getVersion();
}
/**
* Returns the description for this template
* @return {String} the description of this template
*/
getDescription() {
return this.getMetadata().getDescription();
}
/**
* Gets a content based SHA-256 hash for this template. Hash
* is based on the metadata for the template plus the contents of
* all the models and all the script files.
* @return {string} the SHA-256 hash in hex format
*/
getHash() {
const content = {};
content.metadata = this.getMetadata();
if(this.parserManager.getTemplatizedGrammar()) {
content.templatizedGrammar = this.parserManager.getTemplatizedGrammar();
}
else {
// do not include the generated grammar because
// the contents is not deterministic
content.grammar = this.parserManager.getGrammar();
}
content.models = {};
content.scripts = {};
let modelFiles = this.getModelManager().getModels();
modelFiles.forEach(function (file) {
content.models[file.name] = file.content;
});
let scriptManager = this.getScriptManager();
let scriptFiles = scriptManager.getScripts();
scriptFiles.forEach(function (file) {
content.scripts[file.getIdentifier()] = file.contents;
});
const hasher = crypto.createHash('sha256');
hasher.update(stringify(content));
return hasher.digest('hex');
}
/**
* Persists this template to a Cicero Template Archive (cta) file.
* @param {string} [language] - target language for the archive (should be 'ergo')
* @param {Object} [options] - JSZip options
* @return {Promise<Buffer>} the zlib buffer
*/
async toArchive(language, options) {
return TemplateSaver.toArchive(this, language, options);
}
/**
* Builds a Template from the contents of a directory.
* The directory must include a package.json in the root (used to specify
* the name, version and description of the template).
*
* @param {String} path to a local directory
* @param {Object} [options] - an optional set of options to configure the instance.
* @return {Promise<Template>} a Promise to the instantiated template
*/
static async fromDirectory(path, options=null) {
return TemplateLoader.fromDirectory(Template, path, options);
}
/**
* Create a template from an archive.
* @param {Buffer} buffer - the buffer to a Cicero Template Archive (cta) file
* @param {Object} [options] - an optional set of options to configure the instance.
* @return {Promise<Template>} a Promise to the template
*/
static async fromArchive(buffer, options=null) {
return TemplateLoader.fromArchive(Template, buffer, options);
}
/**
* Create a template from an URL.
* @param {String} url - the URL to a Cicero Template Archive (cta) file
* @param {Object} [options] - an optional set of options to configure the instance.
* @return {Promise} a Promise to the template
*/
static async fromUrl(url, options=null) {
return TemplateLoader.fromUrl(Template, url, options);
}
/**
* Visitor design pattern
* @param {Object} visitor - the visitor
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
accept(visitor, parameters) {
return visitor.visit(this, parameters);
}
/**
* Provides access to the parser manager for this template.
* The parser manager can convert template data to and from
* natural language text.
* @return {ParserManager} the ParserManager for this template
*/
getParserManager() {
return this.parserManager;
}
/**
* Provides access to the template logic for this template.
* The template logic encapsulate the code necessary to
* execute the clause or contract.
* @return {LogicManager} the LogicManager for this template
*/
getLogicManager() {
return this.logicManager;
}
/**
* Provides access to the Introspector for this template. The Introspector
* is used to reflect on the types defined within this template.
* @return {Introspector} the Introspector for this template
*/
getIntrospector() {
return this.logicManager.getIntrospector();
}
/**
* Provides access to the Factory for this template. The Factory
* is used to create the types defined in this template.
* @return {Factory} the Factory for this template
*/
getFactory() {
return this.logicManager.getFactory();
}
/**
* Provides access to the Serializer for this template. The Serializer
* is used to serialize instances of the types defined within this template.
* @return {Serializer} the Serializer for this template
*/
getSerializer() {
return this.logicManager.getSerializer();
}
/**
* Provides access to the ScriptManager for this template. The ScriptManager
* manage access to the scripts that have been defined within this template.
* @return {ScriptManager} the ScriptManager for this template
* @private
*/
getScriptManager() {
return this.logicManager.getScriptManager();
}
/**
* Provides access to the ModelManager for this template. The ModelManager
* manage access to the models that have been defined within this template.
* @return {ModelManager} the ModelManager for this template
* @private
*/
getModelManager() {
return this.logicManager.getModelManager();
}
/**
* Set the samples within the Metadata
* @param {object} samples the samples for the tempalte
* @private
*/
setSamples(samples) {
this.metadata = new Metadata(this.metadata.getPackageJson(), this.metadata.getREADME(), samples, this.metadata.getRequest());
}
/**
* Set a locale-specified sample within the Metadata
* @param {object} sample the samples for the template
* @param {string} locale the IETF Language Tag (BCP 47) for the language
* @private
*/
setSample(sample, locale) {
const samples = this.metadata.getSamples();
samples[locale] = sample;
this.metadata = new Metadata(this.metadata.getPackageJson(), this.metadata.getREADME(), samples, this.metadata.getRequest());
}
/**
* Set the request within the Metadata
* @param {object} request the samples for the template
* @private
*/
setRequest(request) {
this.metadata = new Metadata(this.metadata.getPackageJson(), this.metadata.getREADME(), this.metadata.getSamples(), request);
}
/**
* Set the readme file within the Metadata
* @param {String} readme the readme in markdown for the template
* @private
*/
setReadme(readme) {
this.metadata = new Metadata(this.metadata.getPackageJson(), readme, this.metadata.getSamples(), this.metadata.getRequest());
}
/**
* Set the packageJson within the Metadata
* @param {object} packageJson the JS object for package.json
* @private
*/
setPackageJson(packageJson) {
this.metadata = new Metadata(packageJson, this.metadata.getREADME(), this.metadata.getSamples(), this.metadata.getRequest());
}
/**
* Provides a list of the input types that are accepted by this Template. Types use the fully-qualified form.
* @return {Array} a list of the request types
*/
getRequestTypes() {
return this.extractFuncDeclParameter(1);
}
/**
* Provides a list of the response types that are returned by this Template. Types use the fully-qualified form.
* @return {Array} a list of the response types
*/
getResponseTypes() {
return this.extractFuncDeclParameter(2);
}
/**
* Provides a list of the emit types that are emitted by this Template. Types use the fully-qualified form.
* @return {Array} a list of the emit types
*/
getEmitTypes() {
return this.extractFuncDeclParameter(3);
}
/**
* Provides a list of the state types that are expected by this Template. Types use the fully-qualified form.
* @return {Array} a list of the state types
*/
getStateTypes() {
return this.extractFuncDeclParameter(4);
}
/**
* Helper method to retrieve types from a function declarations
* @param {number} index the parameter index for the function declaration
* 1 - Request Types
* 2 - Return Types
* 3 - Emit Types
* 4 - State Types
* @returns {Array} a list of types
* @private
*/
extractFuncDeclParameter(index) {
const functionDeclarations = this.getScriptManager().allFunctionDeclarations();
let types = [];
functionDeclarations.forEach((ele, n) => {
const type = ele.getParameterTypes()[index];
if (type) {
types.push(type);
}
});
Logger.debug(types);
return types;
}
/**
* Returns true if the template has logic, i.e. has more than one script file.
* @return {boolean} true if the template has logic
*/
hasLogic() {
return this.getScriptManager().getAllScripts().length > 0;
}
/**
* Check to see if a ClassDeclaration is an instance of the specified fully qualified
* type name.
* @internal
* @param {ClassDeclaration} classDeclaration The class to test
* @param {String} fqt The fully qualified type name.
* @returns {boolean} True if classDeclaration an instance of the specified fully
* qualified type name, false otherwise.
*/
static instanceOf(classDeclaration, fqt) {
// Check to see if this is an exact instance of the specified type.
if (classDeclaration.getFullyQualifiedName() === fqt) {
return true;
}
// Now walk the class hierachy looking to see if it's an instance of the specified type.
let superTypeDeclaration = classDeclaration.getSuperTypeDeclaration();
while (superTypeDeclaration) {
if (superTypeDeclaration.getFullyQualifiedName() === fqt) {
return true;
}
superTypeDeclaration = superTypeDeclaration.getSuperTypeDeclaration();
}
return false;
}
/**
* Checks whether the template grammar has computer (Ergo) expressions
* @returns {boolean} True if the template grammar has Ergo expressions (`{{% ... %}}`)
*/
grammarHasErgoExpression() {
return this.getParserManager().ergoExpression;
}
}
module.exports = Template;