-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
modelutil.js
291 lines (256 loc) · 10.1 KB
/
modelutil.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
/*
* 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 { MetaModelUtil } = require('@accordproject/concerto-metamodel');
const semver = require('semver');
const Globalize = require('./globalize');
const ID_REGEX = /^(\p{Lu}|\p{Ll}|\p{Lt}|\p{Lm}|\p{Lo}|\p{Nl}|\$|_|\\u[0-9A-Fa-f]{4})(?:\p{Lu}|\p{Ll}|\p{Lt}|\p{Lm}|\p{Lo}|\p{Nl}|\$|_|\\u[0-9A-Fa-f]{4}|\p{Mn}|\p{Mc}|\p{Nd}|\p{Pc}|\u200C|\u200D)*$/u;
const privateReservedProperties = [
// Internal use only
'$classDeclaration', // Used to cache a reference to theClass Declaration instance
'$namespace', // Used to cache the namespace for a type
'$type', // Used to cache the type for a type
'$modelManager', // Used to cache a reference to the ModelManager instance
'$validator', // Used to cache a reference to the ResourceValidator instance
'$identifierFieldName', // Used for caching the identifier field name
'$imports', // Reserved for future use
'$superTypes', // Reserved for future use
// Included in serialization
'$id', // Used for URI identifier
];
const assignableReservedProperties = [
// Included in serialization
'$identifier', // Used for shadowing the identifier field, or where a system identifier is required
'$timestamp' // Used in Event and Transaction prototype classes
];
const reservedProperties = [
// Included in serialization
'$class', // Used for discriminating between instances of different classes
...assignableReservedProperties,
...privateReservedProperties
];
/**
* Internal Model Utility Class
* <p><a href="./diagrams-private/modelutil.svg"><img src="./diagrams-private/modelutil.svg" style="height:100%;"/></a></p>
* @private
* @class
* @memberof module:concerto-core
*/
class ModelUtil {
/**
* Returns everything after the last dot, if present, of the source string
* @param {string} fqn - the source string
* @return {string} - the string after the last dot
*/
static getShortName(fqn) {
let result = fqn;
let dotIndex = fqn.lastIndexOf('.');
if (dotIndex > -1) {
result = fqn.substr(dotIndex + 1);
}
return result;
}
/**
* Returns the namespace for the fully qualified name of a type
* @param {string} fqn - the fully qualified identifier of a type
* @return {string} - namespace of the type (everything before the last dot)
* or the empty string if there is no dot
*/
static getNamespace(fqn) {
if (!fqn) {
throw new Error(Globalize.formatMessage('modelutil-getnamespace-nofnq'));
}
let result = '';
let dotIndex = fqn.lastIndexOf('.');
if (dotIndex > -1) {
result = fqn.substr(0, dotIndex);
}
return result;
}
/**
* @typedef ParseNamespaceResult
* @property {string} name the name of the namespace
* @property {string} escapedNamespace the escaped namespace
* @property {string} version the version of the namespace
* @property {object} versionParsed the parsed semantic version of the namespace
*/
/**
* Parses a potentially versioned namespace into
* its name and version parts. The version of the namespace
* (if present) is parsed using semver.parse.
* @param {string} ns the namespace to parse
* @returns {ParseNamespaceResult} the result of parsing
*/
static parseNamespace(ns) {
if(!ns) {
throw new Error('Namespace is null or undefined.');
}
const parts = ns.split('@');
if(parts.length > 2) {
throw new Error(`Invalid namespace ${ns}`);
}
if(parts.length === 2) {
if(!semver.valid(parts[1])) {
throw new Error(`Invalid namespace ${ns}`);
}
}
return {
name: parts[0],
escapedNamespace: ns.replace('@', '_'),
version: parts.length > 1 ? parts[1] : null,
versionParsed: parts.length > 1 ? semver.parse(parts[1]) : null
};
}
/**
* Return the fully qualified name for an import
* @param {object} imp - the import
* @return {string[]} - the fully qualified names for that import
* @private
*/
static importFullyQualifiedNames(imp) {
return MetaModelUtil.importFullyQualifiedNames(imp);
}
/**
* Returns true if the type is a primitive type
* @param {string} typeName - the name of the type
* @return {boolean} - true if the type is a primitive
* @private
*/
static isPrimitiveType(typeName) {
const primitiveTypes = ['Boolean', 'String', 'DateTime', 'Double', 'Integer', 'Long'];
return (primitiveTypes.indexOf(typeName) >= 0);
}
/**
* Returns true if the type is assignable to the propertyType.
*
* @param {ModelFile} modelFile - the ModelFile that owns the Property
* @param {string} typeName - the FQN of the type we are trying to assign
* @param {Property} property - the property that we'd like to store the
* type in.
* @return {boolean} - true if the type can be assigned to the property
* @private
*/
static isAssignableTo(modelFile, typeName, property) {
const propertyTypeName = property.getFullyQualifiedTypeName();
const isDirectMatch = (typeName === propertyTypeName);
if (isDirectMatch || ModelUtil.isPrimitiveType(typeName) || ModelUtil.isPrimitiveType(propertyTypeName)) {
return isDirectMatch;
}
const typeDeclaration = modelFile.getType(typeName);
if (!typeDeclaration) {
throw new Error('Cannot find type ' + typeName);
}
return typeDeclaration.getAllSuperTypeDeclarations().
some(type => type.getFullyQualifiedName() === propertyTypeName);
}
/**
* Returns the passed string with the first character capitalized
* @param {string} string - the string
* @return {string} the string with the first letter capitalized
* @private
*/
static capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Returns true if the given field is an enumerated type
* @param {Field} field - the string
* @return {boolean} true if the field is declared as an enumeration
* @private
*/
static isEnum(field) {
const modelFile = field.getParent().getModelFile();
const typeDeclaration = modelFile.getType(field.getType());
return typeDeclaration?.isEnum();
}
/**
* Returns true if the given field is an map type
* @param {Field} field - the string
* @return {boolean} true if the field is declared as an map
* @private
*/
static isMap(field) {
const modelFile = field.getParent().getModelFile();
const typeDeclaration = modelFile.getType(field.getType());
return typeDeclaration?.isMapDeclaration?.();
}
/**
* Returns true if the given field is a Scalar type
* @param {Field} field - the Field to test
* @return {boolean} true if the field is declared as an scalar
* @private
*/
static isScalar(field) {
const modelFile = field.getParent().getModelFile();
const declaration = modelFile.getType(field.getType());
return declaration?.isScalarDeclaration?.();
}
/**
* Return true if the name is a valid Concerto identifier
* @param {string} name - the name of the identifier to test.
* @returns {boolean} true if the identifier is valid.
*/
static isValidIdentifier(name) {
return ID_REGEX.test(name);
}
/**
* Get the fully qualified name of a type.
* @param {string} namespace - namespace of the type.
* @param {string} type - short name of the type.
* @returns {string} the fully qualified type name.
*/
static getFullyQualifiedName(namespace, type) {
if (namespace) {
return `${namespace}.${type}`;
} else {
return type;
}
}
/**
* Converts a fully qualified type name to a FQN without a namespace version.
* If the FQN is a primitive type it is returned unchanged.
* @param {string} fqn fully qualified name of a type
* @returns {string} the fully qualified name minus the namespace version
*/
static removeNamespaceVersionFromFullyQualifiedName(fqn) {
if(ModelUtil.isPrimitiveType(fqn)) {
return fqn;
}
const ns = ModelUtil.getNamespace(fqn);
const { name: namespace } = ModelUtil.parseNamespace(ns);
const typeName = ModelUtil.getShortName(fqn);
return ModelUtil.getFullyQualifiedName(namespace, typeName);
}
/**
* Returns true if the property is a system property.
* System properties are not declared in the model.
* @param {String} propertyName - the name of the property
* @return {Boolean} true if the property is a system property
* @private
*/
static isSystemProperty(propertyName) {
return reservedProperties.includes(propertyName);
}
/**
* Returns true if the property is an system property that can be set in serialized JSON.
* System properties are not declared in the model.
* @param {String} propertyName - the name of the property
* @return {Boolean} true if the property is a system property
* @private
*/
static isPrivateSystemProperty(propertyName) {
return privateReservedProperties.includes(propertyName);
}
}
module.exports = ModelUtil;