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 | ;
|
16 |
|
17 | const Property = require('./property');
|
18 | const NumberValidator = require('./numbervalidator');
|
19 | const StringValidator = require('./stringvalidator');
|
20 |
|
21 | /**
|
22 | * Class representing the definition of a Field. A Field is owned
|
23 | * by a ClassDeclaration and has a name, type and additional metadata
|
24 | * (see below).
|
25 | * @private
|
26 | * @extends Property
|
27 | * @see See {@link Property}
|
28 | * @class
|
29 | * @memberof module:concerto-core
|
30 | */
|
31 | class Field extends Property {
|
32 |
|
33 | /**
|
34 | * Create an Field.
|
35 | * @param {ClassDeclaration} parent - The owner of this property
|
36 | * @param {Object} ast - The AST created by the parser
|
37 | * @throws {IllegalModelException}
|
38 | */
|
39 | constructor(parent, ast) {
|
40 | super(parent, ast);
|
41 | this._isField = true;
|
42 | }
|
43 |
|
44 | /**
|
45 | * Process the AST and build the model
|
46 | * @throws {IllegalModelException}
|
47 | * @private
|
48 | */
|
49 | process() {
|
50 | super.process();
|
51 |
|
52 | this.validator = null;
|
53 |
|
54 | switch(this.getType()) {
|
55 | case 'Integer':
|
56 | case 'Double':
|
57 | case 'Long':
|
58 | if(this.ast.range) {
|
59 | this.validator = new NumberValidator(this, this.ast.range);
|
60 | }
|
61 | break;
|
62 | case 'String':
|
63 | if(this.ast.regex) {
|
64 | this.validator = new StringValidator(this, this.ast.regex);
|
65 | }
|
66 | break;
|
67 | }
|
68 |
|
69 | if(this.ast.default) {
|
70 | this.defaultValue = this.ast.default;
|
71 | } else {
|
72 | this.defaultValue = null;
|
73 | }
|
74 | }
|
75 |
|
76 | /**
|
77 | * Returns the validator string for this field
|
78 | * @return {string} the validator for the field or null
|
79 | */
|
80 | getValidator() {
|
81 | return this.validator;
|
82 | }
|
83 |
|
84 | /**
|
85 | * Returns the default value for the field or null
|
86 | * @return {string} the default value for the field or null
|
87 | */
|
88 | getDefaultValue() {
|
89 | if(this.defaultValue) {
|
90 | return this.defaultValue;
|
91 | }
|
92 | else {
|
93 | return null;
|
94 | }
|
95 | }
|
96 |
|
97 | /**
|
98 | * Returns a string representation of this property§
|
99 | * @return {String} the string version of the property.
|
100 | */
|
101 | toString() {
|
102 | return 'Field {name=' + this.name + ', type=' + this.getFullyQualifiedTypeName() + ', array=' + this.array + ', optional=' + this.optional +'}';
|
103 | }
|
104 |
|
105 | /**
|
106 | * Alternative instanceof that is reliable across different module instances
|
107 | * @see https://github.com/hyperledger/composer-concerto/issues/47
|
108 | *
|
109 | * @param {object} object - The object to test against
|
110 | * @returns {boolean} - True, if the object is an instance of a Class Declaration
|
111 | */
|
112 | static [Symbol.hasInstance](object){
|
113 | return typeof object !== 'undefined' && object !== null && Boolean(object._isField);
|
114 | }
|
115 | }
|
116 |
|
117 | module.exports = Field;
|