1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | 'use strict';
|
16 |
|
17 | var util = require('util');
|
18 | var {
|
19 | NS_PREFIX_CommonMarkModel
|
20 | } = require('@accordproject/markdown-common').CommonMarkModel;
|
21 | var NS_PREFIX_TemplateMarkModel = require('./externalModels/TemplateMarkModel').NS_PREFIX_TemplateMarkModel;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | class ModelVisitor {
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
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 |
|
59 |
|
60 |
|
61 |
|
62 |
|
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 |
|
73 |
|
74 |
|
75 |
|
76 |
|
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 |
|
99 |
|
100 |
|
101 |
|
102 |
|
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 = ' ';
|
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 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | visitEnumValueDeclaration(enumValueDeclaration, parameters) {
|
141 | throw new Error('visitEnumValueDeclaration not handled');
|
142 | }
|
143 |
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | visitRelationshipDeclaration(relationship, parameters) {
|
151 | throw new Error('visitRelationshipDeclaration');
|
152 | }
|
153 | }
|
154 | module.exports = ModelVisitor; |
\ | No newline at end of file |