UNPKG

5.42 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17var util = require('util');
18var {
19 NS_PREFIX_CommonMarkModel
20} = require('@accordproject/markdown-common').CommonMarkModel;
21var NS_PREFIX_TemplateMarkModel = require('./externalModels/TemplateMarkModel').NS_PREFIX_TemplateMarkModel;
22
23/**
24 * Converts concerto models to TemplateMark
25 *
26 * @private
27 * @class
28 */
29class ModelVisitor {
30 /**
31 * Visitor design pattern
32 * @param {Object} thing - the object being visited
33 * @param {Object} parameters - the parameter
34 * @return {Object} the result of visiting or null
35 * @private
36 */
37 visit(thing, parameters) {
38 var _thing$isEnum, _thing$isClassDeclara, _thing$isField, _thing$isRelationship, _thing$isEnumValue;
39 if ((_thing$isEnum = thing.isEnum) !== null && _thing$isEnum !== void 0 && _thing$isEnum.call(thing)) {
40 return this.visitEnumDeclaration(thing, parameters);
41 } else if ((_thing$isClassDeclara = thing.isClassDeclaration) !== null && _thing$isClassDeclara !== void 0 && _thing$isClassDeclara.call(thing)) {
42 return this.visitClassDeclaration(thing, parameters);
43 } else if ((_thing$isField = thing.isField) !== null && _thing$isField !== void 0 && _thing$isField.call(thing)) {
44 return this.visitField(thing, parameters);
45 } else if ((_thing$isRelationship = thing.isRelationship) !== null && _thing$isRelationship !== void 0 && _thing$isRelationship.call(thing)) {
46 return this.visitRelationship(thing, parameters);
47 } else if ((_thing$isEnumValue = thing.isEnumValue) !== null && _thing$isEnumValue !== void 0 && _thing$isEnumValue.call(thing)) {
48 return this.visitEnumValueDeclaration(thing, parameters);
49 } else {
50 throw new Error('Unrecognised type: ' + typeof thing + ', value: ' + util.inspect(thing, {
51 showHidden: true,
52 depth: 2
53 }));
54 }
55 }
56
57 /**
58 * Visitor design pattern
59 * @param {EnumDeclaration} enumDeclaration - the object being visited
60 * @param {Object} parameters - the parameter
61 * @return {Object} the result of visiting or null
62 * @private
63 */
64 visitEnumDeclaration(enumDeclaration, parameters) {
65 var result = {};
66 result.$class = NS_PREFIX_TemplateMarkModel + 'EnumVariableDefinition';
67 result.name = parameters.type;
68 return result;
69 }
70
71 /**
72 * Visitor design pattern
73 * @param {ClassDeclaration} classDeclaration - the object being visited
74 * @param {Object} parameters - the parameter
75 * @return {Object} the result of visiting or null
76 * @private
77 */
78 visitClassDeclaration(classDeclaration, parameters) {
79 var result = {};
80 result.$class = NS_PREFIX_TemplateMarkModel + 'WithDefinition';
81 result.name = parameters.name;
82 result.nodes = [];
83 var first = true;
84 classDeclaration.getProperties().forEach((property, index) => {
85 if (!first) {
86 var textNode = {};
87 textNode.$class = NS_PREFIX_CommonMarkModel + 'Text';
88 textNode.text = ' ';
89 result.nodes.push(textNode);
90 }
91 result.nodes.push(property.accept(this, parameters));
92 first = false;
93 });
94 return result;
95 }
96
97 /**
98 * Visitor design pattern
99 * @param {Field} field - the object being visited
100 * @param {Object} parameters - the parameter
101 * @return {Object} the result of visiting or null
102 * @private
103 */
104 visitField(field, parameters) {
105 var fieldName = field.getName();
106 var result = {};
107 result.$class = NS_PREFIX_TemplateMarkModel + 'VariableDefinition';
108 result.name = fieldName;
109 if (field.isArray()) {
110 if (field.isPrimitive()) {
111 result.name = 'this';
112 }
113 var arrayResult = {};
114 arrayResult.$class = NS_PREFIX_TemplateMarkModel + 'JoinDefinition';
115 arrayResult.separator = ' '; // XXX {{#join }}
116 arrayResult.name = fieldName;
117 arrayResult.nodes = [result];
118 result = arrayResult;
119 }
120 if (field.isOptional()) {
121 if (field.isPrimitive()) {
122 result.name = 'this';
123 }
124 var optionalResult = {};
125 optionalResult.$class = NS_PREFIX_TemplateMarkModel + 'OptionalDefinition';
126 optionalResult.name = fieldName;
127 optionalResult.whenSome = [result];
128 optionalResult.whenNone = [];
129 result = optionalResult;
130 }
131 return result;
132 }
133
134 /**
135 * Visitor design pattern
136 * @param {EnumValueDeclaration} enumValueDeclaration - the object being visited
137 * @param {Object} parameters - the parameter
138 * @private
139 */
140 visitEnumValueDeclaration(enumValueDeclaration, parameters) {
141 throw new Error('visitEnumValueDeclaration not handled');
142 }
143
144 /**
145 * Visitor design pattern
146 * @param {Relationship} relationship - the object being visited
147 * @param {Object} parameters - the parameter
148 * @private
149 */
150 visitRelationshipDeclaration(relationship, parameters) {
151 throw new Error('visitRelationshipDeclaration');
152 }
153}
154module.exports = ModelVisitor;
\No newline at end of file