@api-client/core 0.6.13 → 0.6.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/browser.d.ts +9 -2
- package/build/browser.js +12 -2
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +9 -2
- package/build/index.js +12 -2
- package/build/index.js.map +1 -1
- package/build/src/amf/AmfMixin.d.ts +395 -0
- package/build/src/amf/AmfMixin.js +1146 -0
- package/build/src/amf/AmfMixin.js.map +1 -0
- package/build/src/amf/AmfSerializer.d.ts +136 -0
- package/build/src/amf/AmfSerializer.js +1913 -0
- package/build/src/amf/AmfSerializer.js.map +1 -0
- package/build/src/amf/AmfShapeGenerator.d.ts +85 -0
- package/build/src/amf/AmfShapeGenerator.js +385 -0
- package/build/src/amf/AmfShapeGenerator.js.map +1 -0
- package/build/src/amf/AmfTypes.d.ts +24 -0
- package/build/src/amf/AmfTypes.js +122 -0
- package/build/src/amf/AmfTypes.js.map +1 -0
- package/build/src/amf/ApiExampleGenerator.d.ts +36 -0
- package/build/src/amf/ApiExampleGenerator.js +109 -0
- package/build/src/amf/ApiExampleGenerator.js.map +1 -0
- package/build/src/amf/ApiMonacoSchemaGenerator.d.ts +67 -0
- package/build/src/amf/ApiMonacoSchemaGenerator.js +243 -0
- package/build/src/amf/ApiMonacoSchemaGenerator.js.map +1 -0
- package/build/src/amf/ApiSchemaGenerator.d.ts +55 -0
- package/build/src/amf/ApiSchemaGenerator.js +94 -0
- package/build/src/amf/ApiSchemaGenerator.js.map +1 -0
- package/build/src/amf/ApiSchemaValues.d.ts +98 -0
- package/build/src/amf/ApiSchemaValues.js +382 -0
- package/build/src/amf/ApiSchemaValues.js.map +1 -0
- package/build/src/amf/Utils.d.ts +41 -0
- package/build/src/amf/Utils.js +176 -0
- package/build/src/amf/Utils.js.map +1 -0
- package/build/src/amf/data-node/DataNodeBase.d.ts +31 -0
- package/build/src/amf/data-node/DataNodeBase.js +77 -0
- package/build/src/amf/data-node/DataNodeBase.js.map +1 -0
- package/build/src/amf/data-node/JsonDataNodeGenerator.d.ts +15 -0
- package/build/src/amf/data-node/JsonDataNodeGenerator.js +24 -0
- package/build/src/amf/data-node/JsonDataNodeGenerator.js.map +1 -0
- package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.d.ts +13 -0
- package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.js +42 -0
- package/build/src/amf/data-node/UrlEncodedDataNodeGenerator.js.map +1 -0
- package/build/src/amf/data-node/XmlDataNodeGenerator.d.ts +21 -0
- package/build/src/amf/data-node/XmlDataNodeGenerator.js +30 -0
- package/build/src/amf/data-node/XmlDataNodeGenerator.js.map +1 -0
- package/build/src/amf/definitions/Amf.d.ts +362 -0
- package/build/src/amf/definitions/Amf.js +2 -0
- package/build/src/amf/definitions/Amf.js.map +1 -0
- package/build/src/amf/definitions/Api.d.ts +381 -0
- package/build/src/amf/definitions/Api.js +2 -0
- package/build/src/amf/definitions/Api.js.map +1 -0
- package/build/src/amf/definitions/Base.d.ts +11 -0
- package/build/src/amf/definitions/Base.js +2 -0
- package/build/src/amf/definitions/Base.js.map +1 -0
- package/build/src/amf/definitions/Namespace.d.ts +311 -0
- package/build/src/amf/definitions/Namespace.js +330 -0
- package/build/src/amf/definitions/Namespace.js.map +1 -0
- package/build/src/amf/definitions/Shapes.d.ts +309 -0
- package/build/src/amf/definitions/Shapes.js +87 -0
- package/build/src/amf/definitions/Shapes.js.map +1 -0
- package/build/src/amf/models/AmfDataNode.d.ts +68 -0
- package/build/src/amf/models/AmfDataNode.js +188 -0
- package/build/src/amf/models/AmfDataNode.js.map +1 -0
- package/build/src/amf/shape/ShapeBase.d.ts +75 -0
- package/build/src/amf/shape/ShapeBase.js +90 -0
- package/build/src/amf/shape/ShapeBase.js.map +1 -0
- package/build/src/amf/shape/ShapeJsonSchemaGenerator.d.ts +46 -0
- package/build/src/amf/shape/ShapeJsonSchemaGenerator.js +406 -0
- package/build/src/amf/shape/ShapeJsonSchemaGenerator.js.map +1 -0
- package/build/src/amf/shape/ShapeXmlSchemaGenerator.d.ts +84 -0
- package/build/src/amf/shape/ShapeXmlSchemaGenerator.js +820 -0
- package/build/src/amf/shape/ShapeXmlSchemaGenerator.js.map +1 -0
- package/build/src/models/Thing.d.ts +17 -0
- package/build/src/models/Thing.js +19 -1
- package/build/src/models/Thing.js.map +1 -1
- package/build/src/models/data/Bindings.d.ts +161 -0
- package/build/src/models/data/Bindings.js +2 -0
- package/build/src/models/data/Bindings.js.map +1 -0
- package/build/src/models/data/DataAssociation.d.ts +135 -14
- package/build/src/models/data/DataAssociation.js +154 -21
- package/build/src/models/data/DataAssociation.js.map +1 -1
- package/build/src/models/data/DataAssociationSchema.d.ts +36 -0
- package/build/src/models/data/DataEntity.d.ts +76 -4
- package/build/src/models/data/DataEntity.js +136 -9
- package/build/src/models/data/DataEntity.js.map +1 -1
- package/build/src/models/data/DataFile.d.ts +3 -0
- package/build/src/models/data/DataFile.js +3 -0
- package/build/src/models/data/DataFile.js.map +1 -1
- package/build/src/models/data/DataModel.d.ts +1 -1
- package/build/src/models/data/DataModel.js.map +1 -1
- package/build/src/models/data/DataNamespace.d.ts +6 -0
- package/build/src/models/data/DataNamespace.js +11 -1
- package/build/src/models/data/DataNamespace.js.map +1 -1
- package/build/src/models/data/DataProperty.d.ts +131 -36
- package/build/src/models/data/DataProperty.js +200 -17
- package/build/src/models/data/DataProperty.js.map +1 -1
- package/data/apis/oas-date/oas-date.yaml +28 -0
- package/data/apis/oas-types/oas-types.yaml +159 -0
- package/data/apis/oas-unions/oas-unions.yaml +75 -0
- package/data/apis/raml-date/raml-date.raml +28 -0
- package/data/apis/recursive/recursive.raml +14 -0
- package/data/apis/schema-api/examples/person.json +14 -0
- package/data/apis/schema-api/examples/person.raml +14 -0
- package/data/apis/schema-api/examples/person.url.encoded +1 -0
- package/data/apis/schema-api/examples/person.xml +14 -0
- package/data/apis/schema-api/library/demo-types.raml +43 -0
- package/data/apis/schema-api/schema-api.raml +644 -0
- package/data/apis/schema-api/schemas/person.json +104 -0
- package/data/apis/schema-api/schemas/person.xsd +26 -0
- package/data/apis/schema-api/types/DemoPerson.raml +67 -0
- package/data/model.js +106 -0
- package/data/models/oas-date.json +637 -0
- package/data/models/oas-types.json +5352 -0
- package/data/models/oas-unions.json +1881 -0
- package/data/models/raml-date.json +1096 -0
- package/data/models/recursive.json +610 -0
- package/data/models/schema-api.json +37319 -0
- package/package.json +9 -6
- package/src/amf/AmfMixin.ts +1623 -0
- package/src/amf/AmfSerializer.ts +2028 -0
- package/src/amf/AmfShapeGenerator.ts +400 -0
- package/src/amf/AmfTypes.ts +126 -0
- package/src/amf/ApiExampleGenerator.ts +112 -0
- package/src/amf/ApiMonacoSchemaGenerator.ts +296 -0
- package/src/amf/ApiSchemaGenerator.ts +108 -0
- package/src/amf/ApiSchemaValues.ts +411 -0
- package/src/amf/Utils.ts +182 -0
- package/src/amf/data-node/DataNodeBase.ts +81 -0
- package/src/amf/data-node/JsonDataNodeGenerator.ts +26 -0
- package/src/amf/data-node/README.md +3 -0
- package/src/amf/data-node/UrlEncodedDataNodeGenerator.ts +43 -0
- package/src/amf/data-node/XmlDataNodeGenerator.ts +38 -0
- package/src/amf/definitions/Amf.ts +443 -0
- package/src/amf/definitions/Api.ts +427 -0
- package/src/amf/definitions/Base.ts +13 -0
- package/src/amf/definitions/Namespace.ts +341 -0
- package/src/amf/definitions/Shapes.ts +414 -0
- package/src/amf/models/AmfDataNode.ts +200 -0
- package/src/amf/shape/README.md +4 -0
- package/src/amf/shape/ShapeBase.ts +160 -0
- package/src/amf/shape/ShapeJsonSchemaGenerator.ts +422 -0
- package/src/amf/shape/ShapeXmlSchemaGenerator.ts +876 -0
- package/src/models/Thing.ts +25 -1
- package/src/models/data/Bindings.ts +186 -0
- package/src/models/data/DataAssociation.ts +226 -29
- package/src/models/data/DataAssociationSchema.ts +38 -0
- package/src/models/data/DataEntity.ts +170 -13
- package/src/models/data/DataFile.ts +3 -0
- package/src/models/data/DataModel.ts +1 -1
- package/src/models/data/DataNamespace.ts +16 -1
- package/src/models/data/DataProperty.ts +250 -47
- package/build/src/models/data/DataPropertySchema.d.ts +0 -125
- package/build/src/models/data/DataPropertySchema.js +0 -33
- package/build/src/models/data/DataPropertySchema.js.map +0 -1
- package/src/models/data/DataPropertySchema.ts +0 -156
|
@@ -0,0 +1,876 @@
|
|
|
1
|
+
import { Core as JsonCore } from '@api-client/json';
|
|
2
|
+
import { AmfNamespace as ns } from "../definitions/Namespace.js";
|
|
3
|
+
import { ShapeBase } from './ShapeBase.js';
|
|
4
|
+
import { XmlDataNodeGenerator } from '../data-node/XmlDataNodeGenerator.js';
|
|
5
|
+
import { collectNodeProperties, formatXmlValue, getUnionMember } from '../Utils.js';
|
|
6
|
+
import { IAnyShape, IArrayShape, IDataExample, IDataNodeUnion, INodeShape, IPropertyShape, IScalarShape, IShapeUnion, IUnionShape } from "../definitions/Shapes.js";
|
|
7
|
+
|
|
8
|
+
interface IProcessNodeOptions {
|
|
9
|
+
forceName?: string;
|
|
10
|
+
indent?: number;
|
|
11
|
+
noWrap?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface ICollectExamplesOptions {
|
|
15
|
+
nodeName?: string;
|
|
16
|
+
indent?: number;
|
|
17
|
+
isWrapped?: boolean;
|
|
18
|
+
tagFill?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Normalizes given name to a value that can be accepted by `createElement`
|
|
23
|
+
* function on a document object.
|
|
24
|
+
* @param name A name to process
|
|
25
|
+
* @returns The normalized name
|
|
26
|
+
*/
|
|
27
|
+
export const normalizeXmlTagName = (name: string): string => name.replace(/[^a-zA-Z0-9-_.]/g, '');
|
|
28
|
+
const UNKNOWN_TYPE = 'unknown-type';
|
|
29
|
+
|
|
30
|
+
export function shapeToXmlTagName(shape: IAnyShape): string {
|
|
31
|
+
const { name, inherits=[], xmlSerialization } = shape;
|
|
32
|
+
let label: string | undefined = xmlSerialization && xmlSerialization.name ? xmlSerialization.name : name || UNKNOWN_TYPE;
|
|
33
|
+
if (label === 'schema' && inherits.length) {
|
|
34
|
+
const n = inherits.find(i => i.name && i.name !== 'schema');
|
|
35
|
+
if (n) {
|
|
36
|
+
label = n.name === 'type' ? n.displayName || n.name : n.name;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return normalizeXmlTagName(String(label));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class ShapeXmlSchemaGenerator extends ShapeBase {
|
|
43
|
+
/**
|
|
44
|
+
* Generates a XML example from the structured value.
|
|
45
|
+
*
|
|
46
|
+
* @param schema The Shape definition
|
|
47
|
+
*/
|
|
48
|
+
generate(schema: IShapeUnion): string {
|
|
49
|
+
const value = this.processNode(schema);
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Processes the Shape definition and returns a JavaScript object or array.
|
|
55
|
+
*/
|
|
56
|
+
processNode(schema: IShapeUnion, options: IProcessNodeOptions = {}): string {
|
|
57
|
+
const { types } = schema;
|
|
58
|
+
if (types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
59
|
+
return this._scalarShapeObject(schema as IScalarShape, options);
|
|
60
|
+
}
|
|
61
|
+
if (types.includes(ns.w3.shacl.NodeShape)) {
|
|
62
|
+
return this._nodeShapeObject(schema as INodeShape, options);
|
|
63
|
+
}
|
|
64
|
+
if (types.includes(ns.aml.vocabularies.shapes.UnionShape)) {
|
|
65
|
+
return this._unionShapeObject(schema as IUnionShape);
|
|
66
|
+
}
|
|
67
|
+
if (types.includes(ns.aml.vocabularies.shapes.FileShape)) {
|
|
68
|
+
return this._fileShapeObject();
|
|
69
|
+
}
|
|
70
|
+
if (types.includes(ns.aml.vocabularies.shapes.SchemaShape)) {
|
|
71
|
+
return this._schemaShapeObject();
|
|
72
|
+
}
|
|
73
|
+
if (types.includes(ns.aml.vocabularies.shapes.TupleShape)) {
|
|
74
|
+
return this._tupleShapeObject();
|
|
75
|
+
}
|
|
76
|
+
if (types.includes(ns.aml.vocabularies.shapes.ArrayShape) || types.includes(ns.aml.vocabularies.shapes.MatrixShape)) {
|
|
77
|
+
return this._arrayShapeObject(schema as IArrayShape, options);
|
|
78
|
+
}
|
|
79
|
+
return this._anyShapeObject(schema as IAnyShape);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Serializes generated JS value according to the mime type.
|
|
84
|
+
*/
|
|
85
|
+
serialize(value: any): string|undefined {
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Picks the union member to render.
|
|
91
|
+
*/
|
|
92
|
+
protected _readCurrentUnion(anyOf: IShapeUnion[]): IShapeUnion {
|
|
93
|
+
const { selectedUnions } = this.opts;
|
|
94
|
+
return getUnionMember(anyOf, selectedUnions);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
protected _collectProperties(schema: INodeShape): IPropertyShape[] {
|
|
98
|
+
const { selectedUnions } = this.opts;
|
|
99
|
+
return collectNodeProperties(schema, selectedUnions);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
protected _nodeShapeObject(schema: INodeShape, options: IProcessNodeOptions={}): string {
|
|
103
|
+
const label = options.forceName || shapeToXmlTagName(schema);
|
|
104
|
+
const parts = [];
|
|
105
|
+
const currentIndent = (options.indent || 0);
|
|
106
|
+
const exampleValue = this._collectExamples(schema, {
|
|
107
|
+
tagFill: new Array(currentIndent * 2 + 0).fill(' ').join(''),
|
|
108
|
+
indent: currentIndent + 1,
|
|
109
|
+
nodeName: label,
|
|
110
|
+
});
|
|
111
|
+
if (exampleValue) {
|
|
112
|
+
return exampleValue;
|
|
113
|
+
}
|
|
114
|
+
const attributes: string[] = [];
|
|
115
|
+
const properties = this._collectProperties(schema);
|
|
116
|
+
properties.forEach((property) => {
|
|
117
|
+
const { range, minCount=0 } = property;
|
|
118
|
+
if (minCount === 0 && !this.opts.renderOptional) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const anyRange = range as IAnyShape;
|
|
122
|
+
if (anyRange.xmlSerialization) {
|
|
123
|
+
// Adds to the parent attributes list.
|
|
124
|
+
// When a non-scalar shape has `attribute` serialization this is an API spec error.
|
|
125
|
+
// Ignore such situation.
|
|
126
|
+
if (anyRange.xmlSerialization.attribute && anyRange.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
127
|
+
let aLabel = normalizeXmlTagName(anyRange.xmlSerialization.name ? anyRange.xmlSerialization.name : property.name || anyRange.name || UNKNOWN_TYPE);
|
|
128
|
+
if (anyRange.xmlSerialization.prefix) {
|
|
129
|
+
aLabel = `${anyRange.xmlSerialization.prefix}:${aLabel}`;
|
|
130
|
+
}
|
|
131
|
+
const value = this._scalarValue(anyRange);
|
|
132
|
+
attributes.push(`${aLabel}="${value}"`);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const value = this._propertyShapeObject(property, { indent: currentIndent });
|
|
137
|
+
if (typeof value !== 'undefined') {
|
|
138
|
+
const fill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
139
|
+
parts.push(formatXmlValue(fill, value));
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
let opening = `<${label}`;
|
|
144
|
+
if (attributes.length) {
|
|
145
|
+
opening += ' ';
|
|
146
|
+
opening += attributes.join(' ');
|
|
147
|
+
}
|
|
148
|
+
parts.unshift(`${opening}>`);
|
|
149
|
+
const fill = new Array(currentIndent*2).fill(' ').join('');
|
|
150
|
+
parts.push(`${fill}</${label}>`);
|
|
151
|
+
return parts.join('\n');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
protected _scalarShapeObject(schema: IScalarShape, options: IProcessNodeOptions={}): any {
|
|
155
|
+
const { xmlSerialization, defaultValue } = schema;
|
|
156
|
+
let content;
|
|
157
|
+
if (defaultValue) {
|
|
158
|
+
const gen = new XmlDataNodeGenerator();
|
|
159
|
+
content = gen.processNode(defaultValue);
|
|
160
|
+
} else {
|
|
161
|
+
content = this._scalarValue(schema);
|
|
162
|
+
}
|
|
163
|
+
let label = options.forceName || shapeToXmlTagName(schema);
|
|
164
|
+
const attributes = [];
|
|
165
|
+
const parts = [];
|
|
166
|
+
if (xmlSerialization) {
|
|
167
|
+
const { namespace, prefix } = xmlSerialization;
|
|
168
|
+
if (namespace) {
|
|
169
|
+
const attrName = prefix ? `xmlns:${prefix}` : 'xmlns';
|
|
170
|
+
attributes.push(`${attrName}="${namespace}"`);
|
|
171
|
+
}
|
|
172
|
+
if (prefix) {
|
|
173
|
+
label = `${prefix}:${label}`;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
let opening = `<${label}`;
|
|
177
|
+
if (attributes.length) {
|
|
178
|
+
opening += ' ';
|
|
179
|
+
opening += attributes.join(' ');
|
|
180
|
+
}
|
|
181
|
+
opening += '>';
|
|
182
|
+
parts.push(opening);
|
|
183
|
+
parts.push(content);
|
|
184
|
+
parts.push(`</${label}>`);
|
|
185
|
+
|
|
186
|
+
return parts.join('');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
protected _nilShapeObject(schema: IScalarShape, options: IProcessNodeOptions={}): any|undefined {
|
|
190
|
+
const { xmlSerialization } = schema;
|
|
191
|
+
const content = '';
|
|
192
|
+
let label = options.forceName || shapeToXmlTagName(schema);
|
|
193
|
+
const attributes = [];
|
|
194
|
+
const parts = [];
|
|
195
|
+
if (xmlSerialization) {
|
|
196
|
+
const { namespace, prefix } = xmlSerialization;
|
|
197
|
+
if (namespace) {
|
|
198
|
+
const attrName = prefix ? `xmlns:${prefix}` : 'xmlns';
|
|
199
|
+
attributes.push(`${attrName}="${namespace}"`);
|
|
200
|
+
}
|
|
201
|
+
if (prefix) {
|
|
202
|
+
label = `${prefix}:${label}`;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
let opening = `<${label}`;
|
|
206
|
+
if (attributes.length) {
|
|
207
|
+
opening += ' ';
|
|
208
|
+
opening += attributes.join(' ');
|
|
209
|
+
}
|
|
210
|
+
opening += '>';
|
|
211
|
+
parts.push(opening);
|
|
212
|
+
parts.push(content);
|
|
213
|
+
parts.push(`</${label}>`);
|
|
214
|
+
|
|
215
|
+
return parts.join('');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* @returns The value for the property or undefined when cannot generate the value.
|
|
220
|
+
*/
|
|
221
|
+
protected _propertyShapeObject(schema: IPropertyShape, options: IProcessNodeOptions = {}): string|undefined {
|
|
222
|
+
const { range, minCount=0 } = schema;
|
|
223
|
+
if (minCount === 0 && !this.opts.renderOptional || !range) {
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
const { types } = range;
|
|
227
|
+
const name = shapeToXmlTagName(schema as IAnyShape);
|
|
228
|
+
if (types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
229
|
+
return this._scalarShapeObject(range as IScalarShape, { ...options });
|
|
230
|
+
}
|
|
231
|
+
if (types.includes(ns.aml.vocabularies.shapes.NilShape)) {
|
|
232
|
+
return this._nilShapeObject(range as IScalarShape, { ...options, forceName: name });
|
|
233
|
+
}
|
|
234
|
+
if (types.includes(ns.aml.vocabularies.shapes.RecursiveShape)) {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
if (types.includes(ns.w3.shacl.NodeShape)) {
|
|
238
|
+
return this._nodePropertyObject(schema as IPropertyShape<INodeShape>, options);
|
|
239
|
+
}
|
|
240
|
+
if (types.includes(ns.aml.vocabularies.shapes.ArrayShape) || types.includes(ns.aml.vocabularies.shapes.MatrixShape)) {
|
|
241
|
+
return this._nodePropertyArray(schema as IPropertyShape<IArrayShape>, options);
|
|
242
|
+
}
|
|
243
|
+
return this.processNode(range, { ...options, forceName: name });
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
protected _collectExamples(schema: IShapeUnion, opts: ICollectExamplesOptions = {}): string | undefined {
|
|
247
|
+
if (!this.opts.renderExamples) {
|
|
248
|
+
return undefined;
|
|
249
|
+
}
|
|
250
|
+
const { isWrapped, nodeName, tagFill='', indent = 0 } = opts;
|
|
251
|
+
let { examples=[] } = (schema as IAnyShape);
|
|
252
|
+
const { inherits } = schema;
|
|
253
|
+
if (Array.isArray(inherits) && inherits.length) {
|
|
254
|
+
inherits.forEach((parent) => {
|
|
255
|
+
let node = parent;
|
|
256
|
+
if (node.types.includes(ns.aml.vocabularies.shapes.UnionShape)) {
|
|
257
|
+
const union = node as IUnionShape;
|
|
258
|
+
const { anyOf=[] } = union;
|
|
259
|
+
node = this._readCurrentUnion(anyOf);
|
|
260
|
+
}
|
|
261
|
+
const anyShape = node as IAnyShape;
|
|
262
|
+
if (Array.isArray(anyShape.examples) && anyShape.examples.length) {
|
|
263
|
+
examples = examples.concat(anyShape.examples);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const validExamples = examples.filter(item => !!item.structuredValue);
|
|
269
|
+
if (!validExamples.length) {
|
|
270
|
+
return undefined;
|
|
271
|
+
}
|
|
272
|
+
const parts: string[] = [];
|
|
273
|
+
if (isWrapped) {
|
|
274
|
+
parts.push(`${tagFill}<${nodeName}>`);
|
|
275
|
+
}
|
|
276
|
+
const generator = new XmlDataNodeGenerator();
|
|
277
|
+
validExamples.forEach((item) => {
|
|
278
|
+
const value = generator.generate(item.structuredValue!, nodeName, {
|
|
279
|
+
indent: indent + 1,
|
|
280
|
+
});
|
|
281
|
+
if (value !== undefined) {
|
|
282
|
+
parts.push(value);
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
if (isWrapped) {
|
|
286
|
+
parts.push(`${tagFill}</${nodeName}>`);
|
|
287
|
+
}
|
|
288
|
+
return parts.join('\n');
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
protected _createTabs(indent: number = 0, offset: number = 0): string {
|
|
292
|
+
return new Array(indent * 2 + offset).fill(' ').join('');
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
protected _nodePropertyObject(schema: IPropertyShape<INodeShape>, options: IProcessNodeOptions = {}): string {
|
|
296
|
+
const parts: string[] = [];
|
|
297
|
+
const name = normalizeXmlTagName(String(schema.name));
|
|
298
|
+
const { indent=0 } = options;
|
|
299
|
+
const baseTabs = this._createTabs(indent);
|
|
300
|
+
let value: string;
|
|
301
|
+
const range = schema.range as INodeShape;
|
|
302
|
+
const exampleValue = this._collectExamples(range, {
|
|
303
|
+
tagFill: baseTabs,
|
|
304
|
+
indent: indent + 1,
|
|
305
|
+
nodeName: name,
|
|
306
|
+
});
|
|
307
|
+
const attributes: string[] = [];
|
|
308
|
+
if (exampleValue) {
|
|
309
|
+
value = exampleValue;
|
|
310
|
+
} else {
|
|
311
|
+
const propertyParts: string[] = [];
|
|
312
|
+
const properties = this._collectProperties(range);
|
|
313
|
+
|
|
314
|
+
properties.forEach((property) => {
|
|
315
|
+
const { range, minCount=0 } = property;
|
|
316
|
+
if (minCount === 0 && !this.opts.renderOptional) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const anyRange = range as IAnyShape;
|
|
320
|
+
|
|
321
|
+
if (anyRange.xmlSerialization) {
|
|
322
|
+
const { prefix, attribute } = anyRange.xmlSerialization;
|
|
323
|
+
if (attribute && anyRange.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
324
|
+
let aLabel = normalizeXmlTagName(anyRange.xmlSerialization.name ? anyRange.xmlSerialization.name : property.name || anyRange.name || UNKNOWN_TYPE);
|
|
325
|
+
if (prefix) {
|
|
326
|
+
aLabel = `${prefix}:${aLabel}`;
|
|
327
|
+
}
|
|
328
|
+
const value = this._scalarValue(anyRange);
|
|
329
|
+
attributes.push(`${aLabel}="${value}"`);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const value = this._propertyShapeObject(property, { indent });
|
|
334
|
+
if (value !== undefined) {
|
|
335
|
+
const fill = this._createTabs(indent, 2);
|
|
336
|
+
propertyParts.push(formatXmlValue(fill, value));
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
value = propertyParts.join('\n');
|
|
341
|
+
}
|
|
342
|
+
let opening = `${baseTabs}<${name}`;
|
|
343
|
+
if (attributes.length) {
|
|
344
|
+
opening += ' ';
|
|
345
|
+
opening += attributes.join(' ');
|
|
346
|
+
}
|
|
347
|
+
parts.unshift(`${opening}>`);
|
|
348
|
+
parts.push(value);
|
|
349
|
+
parts.push(`${baseTabs}</${name}>`);
|
|
350
|
+
return parts.join('\n');
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
protected _nodePropertyArray(schema: IPropertyShape<IArrayShape>, options: IProcessNodeOptions = {}): string {
|
|
354
|
+
const range = schema.range as IArrayShape;
|
|
355
|
+
const parts: string[] = [];
|
|
356
|
+
let name: string;
|
|
357
|
+
if (range.xmlSerialization && range.xmlSerialization.name) {
|
|
358
|
+
name = normalizeXmlTagName(range.xmlSerialization.name);
|
|
359
|
+
} else {
|
|
360
|
+
name = normalizeXmlTagName(String(schema.name));
|
|
361
|
+
}
|
|
362
|
+
const { indent=0 } = options;
|
|
363
|
+
const isScalarItems = !!range.items && range.items.types.includes(ns.aml.vocabularies.shapes.ScalarShape);
|
|
364
|
+
|
|
365
|
+
const baseTabs = this._createTabs(indent);
|
|
366
|
+
if (!isScalarItems) {
|
|
367
|
+
parts.push(`${baseTabs}<${name}>`);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const result = this._arrayShapeObject(range, { ...options, indent });
|
|
371
|
+
parts.push(result);
|
|
372
|
+
if (!isScalarItems) {
|
|
373
|
+
parts.push(`${baseTabs}</${name}>`);
|
|
374
|
+
}
|
|
375
|
+
return parts.join('\n');
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
protected _arrayShapeObject(schema: IArrayShape, options: IProcessNodeOptions={}): string {
|
|
379
|
+
const { items } = schema;
|
|
380
|
+
const isScalarItems = !!items && items.types.includes(ns.aml.vocabularies.shapes.ScalarShape);
|
|
381
|
+
// the name is either from the XML serialization info, or the parent property.
|
|
382
|
+
// IT IS NOT THE `item`'s name unless its a scalar.
|
|
383
|
+
let nodeName: string | undefined;
|
|
384
|
+
|
|
385
|
+
// if (isScalarItems && schema.name) {
|
|
386
|
+
// nodeName = normalizeXmlTagName(schema.name);
|
|
387
|
+
// } else if (!isScalarItems && items?.name) {
|
|
388
|
+
// nodeName = normalizeXmlTagName(items.name);
|
|
389
|
+
// }
|
|
390
|
+
|
|
391
|
+
if (!isScalarItems && items?.name) {
|
|
392
|
+
nodeName = normalizeXmlTagName(items.name);
|
|
393
|
+
} else if (schema.name) {
|
|
394
|
+
nodeName = normalizeXmlTagName(schema.name);
|
|
395
|
+
}
|
|
396
|
+
// wrapping can only be defined on the array shape and it "wraps" the generated
|
|
397
|
+
// content into an element that has the same name as the items
|
|
398
|
+
const isWrapped = !!schema.xmlSerialization && !!schema.xmlSerialization.wrapped;
|
|
399
|
+
|
|
400
|
+
const currentIndent = (options.indent || 0);
|
|
401
|
+
const tagFill = new Array((currentIndent) * 2 + 0).fill(' ').join('');
|
|
402
|
+
const valueFill = new Array((currentIndent) * 2 + (isWrapped ? 2 : 0)).fill(' ').join('');
|
|
403
|
+
// let's start with examples
|
|
404
|
+
if (this.opts.renderExamples) {
|
|
405
|
+
let { examples=[] } = schema;
|
|
406
|
+
const validExamples = examples.filter(item => !!item.structuredValue);
|
|
407
|
+
if (validExamples.length) {
|
|
408
|
+
const parts: string[] = [];
|
|
409
|
+
const generator = new XmlDataNodeGenerator();
|
|
410
|
+
|
|
411
|
+
// scalar items are always wrapped with it's own range
|
|
412
|
+
if (isScalarItems && !isWrapped) {
|
|
413
|
+
const [example] = validExamples;
|
|
414
|
+
if (!nodeName && items?.name) {
|
|
415
|
+
nodeName = normalizeXmlTagName(items.name);
|
|
416
|
+
}
|
|
417
|
+
const value = generator.generate(example.structuredValue!, nodeName, {
|
|
418
|
+
indent: currentIndent,
|
|
419
|
+
});
|
|
420
|
+
if (value !== undefined) {
|
|
421
|
+
parts.push(value);
|
|
422
|
+
}
|
|
423
|
+
} else if (isScalarItems && isWrapped) {
|
|
424
|
+
const [example] = validExamples;
|
|
425
|
+
const value = generator.generate(example.structuredValue!, nodeName, {
|
|
426
|
+
indent: currentIndent,
|
|
427
|
+
});
|
|
428
|
+
parts.push(`${tagFill}<${nodeName}>`);
|
|
429
|
+
if (value !== undefined) {
|
|
430
|
+
parts.push(value);
|
|
431
|
+
}
|
|
432
|
+
parts.push(`${tagFill}</${nodeName}>`);
|
|
433
|
+
} else if (isWrapped) {
|
|
434
|
+
// when wrapped we wrap each example into the "name", else we render all examples under the "name".
|
|
435
|
+
validExamples.forEach((item) => {
|
|
436
|
+
parts.push(`${tagFill}<${nodeName}>`);
|
|
437
|
+
const value = generator.generate(item.structuredValue!, /* nodeName */ undefined, {
|
|
438
|
+
indent: currentIndent + 2,
|
|
439
|
+
});
|
|
440
|
+
if (value !== undefined) {
|
|
441
|
+
parts.push(value);
|
|
442
|
+
}
|
|
443
|
+
parts.push(`${tagFill}</${nodeName}>`);
|
|
444
|
+
});
|
|
445
|
+
} else {
|
|
446
|
+
validExamples.forEach((item) => {
|
|
447
|
+
const value = generator.generate(item.structuredValue!, /* nodeName */ undefined, {
|
|
448
|
+
indent: currentIndent,
|
|
449
|
+
});
|
|
450
|
+
if (value !== undefined) {
|
|
451
|
+
parts.push(value);
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
return parts.join('\n');
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
if (isScalarItems && schema.defaultValue) {
|
|
460
|
+
const gen = new XmlDataNodeGenerator();
|
|
461
|
+
const value = gen.generate(schema.defaultValue);
|
|
462
|
+
if (!nodeName && items?.name) {
|
|
463
|
+
nodeName = normalizeXmlTagName(items.name);
|
|
464
|
+
}
|
|
465
|
+
return `<${nodeName}>${value?.trim()}</${nodeName}>`;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (items) {
|
|
469
|
+
const parts: string[] = [];
|
|
470
|
+
if (isWrapped && isScalarItems) {
|
|
471
|
+
parts.push(`${tagFill}<${nodeName}>`);
|
|
472
|
+
}
|
|
473
|
+
const init: IProcessNodeOptions = {
|
|
474
|
+
indent: currentIndent + 1,
|
|
475
|
+
}
|
|
476
|
+
if (nodeName) {
|
|
477
|
+
init.forceName = nodeName;
|
|
478
|
+
}
|
|
479
|
+
const value = this.processNode(items, init);
|
|
480
|
+
if (value !== undefined) {
|
|
481
|
+
parts.push(`${valueFill}${value}`);
|
|
482
|
+
}
|
|
483
|
+
if (isWrapped && isScalarItems) {
|
|
484
|
+
parts.push(`${tagFill}</${nodeName}>`);
|
|
485
|
+
}
|
|
486
|
+
return parts.join('\n');
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
return '';
|
|
490
|
+
|
|
491
|
+
// const isScalarItems = !!items && items.types.includes(ns.aml.vocabularies.shapes.ScalarShape);
|
|
492
|
+
// if (items) {
|
|
493
|
+
// if (items.types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
494
|
+
// return this._scalarItems(schema as IArrayShape<IScalarShape>, options);
|
|
495
|
+
// }
|
|
496
|
+
// if (items.types.includes(ns.w3.shacl.NodeShape)) {
|
|
497
|
+
// return this._nodeItems(schema as IArrayShape<INodeShape>, options);
|
|
498
|
+
// }
|
|
499
|
+
// }
|
|
500
|
+
|
|
501
|
+
// let label = shapeToXmlTagName(schema);
|
|
502
|
+
// if (label === UNKNOWN_TYPE && isScalarItems) {
|
|
503
|
+
// // label = shapeToXmlTagName(items as IScalarShape);
|
|
504
|
+
// }
|
|
505
|
+
|
|
506
|
+
// const currentIndent = (options.indent || 0);
|
|
507
|
+
// const rootFill = new Array(currentIndent*2).fill(' ').join('');
|
|
508
|
+
// const parts = [
|
|
509
|
+
// `${rootFill}<${label}>`
|
|
510
|
+
// ];
|
|
511
|
+
// let nodeName = label;
|
|
512
|
+
// const anyItems = items as IAnyShape;
|
|
513
|
+
// if (anyItems.xmlSerialization && anyItems.xmlSerialization.name) {
|
|
514
|
+
// nodeName = normalizeXmlTagName(anyItems.xmlSerialization.name);
|
|
515
|
+
// }
|
|
516
|
+
// // Note about wrapping.
|
|
517
|
+
// // XML array values are not wrapped by default. This means that by default
|
|
518
|
+
// // it produces a value like this:
|
|
519
|
+
// // <ParentArray>
|
|
520
|
+
// // <arrayMemberProperty></arrayMemberProperty>
|
|
521
|
+
// // </ParentArray>
|
|
522
|
+
// //
|
|
523
|
+
// // When the object is marked as wrapped then the object is rendered as follows
|
|
524
|
+
// //
|
|
525
|
+
// // <ParentArray>
|
|
526
|
+
// // <MemberObject>
|
|
527
|
+
// // <arrayMemberProperty></arrayMemberProperty>
|
|
528
|
+
// // <MemberObject>
|
|
529
|
+
// // </ParentArray>
|
|
530
|
+
// const isWrapped = xmlSerialization && !!xmlSerialization.wrapped;
|
|
531
|
+
// const defaultValue = schema.defaultValue || items && items.defaultValue;
|
|
532
|
+
// let itemName;
|
|
533
|
+
// if (isWrapped) {
|
|
534
|
+
// try {
|
|
535
|
+
// itemName = shapeToXmlTagName(items as IAnyShape);
|
|
536
|
+
// } catch (e) {
|
|
537
|
+
// itemName = 'UNKNOWN-NAME'
|
|
538
|
+
// }
|
|
539
|
+
// }
|
|
540
|
+
// let { examples=[] } = schema;
|
|
541
|
+
// if (Array.isArray(anyItems.examples)) {
|
|
542
|
+
// examples = examples.concat(anyItems.examples);
|
|
543
|
+
// }
|
|
544
|
+
// if (this.opts.renderExamples && examples && examples.length) {
|
|
545
|
+
// const example = examples.find((item) => !!item.structuredValue);
|
|
546
|
+
// const value = this._exampleToObject(example);
|
|
547
|
+
// if (typeof value !== 'undefined') {
|
|
548
|
+
// const tagFill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
549
|
+
// const valueFill = new Array(currentIndent * 2 + 4).fill(' ').join('');
|
|
550
|
+
// parts.push(`${tagFill}<${nodeName}>`);
|
|
551
|
+
// parts.push(`${valueFill}${value}`);
|
|
552
|
+
// parts.push(`${tagFill}</${nodeName}>`);
|
|
553
|
+
// }
|
|
554
|
+
// } else if (defaultValue) {
|
|
555
|
+
// const gen = new XmlDataNodeGenerator();
|
|
556
|
+
// const value = gen.generate(defaultValue);
|
|
557
|
+
// if (value) {
|
|
558
|
+
// const tagFill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
559
|
+
// const valueFill = new Array(currentIndent * 2 + 4).fill(' ').join('');
|
|
560
|
+
// parts.push(`${tagFill}<${nodeName}>`);
|
|
561
|
+
// parts.push(`${valueFill}${value.trim()}`);
|
|
562
|
+
// parts.push(`${tagFill}</${nodeName}>`);
|
|
563
|
+
// }
|
|
564
|
+
// } else if (items && items.types.includes(ns.w3.shacl.NodeShape)) {
|
|
565
|
+
// const typed = items as INodeShape;
|
|
566
|
+
// const tagFill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
567
|
+
// const valueFill = isWrapped ? new Array(currentIndent * 2 + 4).fill(' ').join('') : tagFill;
|
|
568
|
+
// if (isWrapped) {
|
|
569
|
+
// parts.push(`${tagFill}<${itemName}>`);
|
|
570
|
+
// }
|
|
571
|
+
// const properties = this._collectProperties(typed);
|
|
572
|
+
// properties.forEach((prop) => {
|
|
573
|
+
// const value = this._propertyShapeObject(prop);
|
|
574
|
+
// if (value) {
|
|
575
|
+
// parts.push(`${valueFill}${value}`);
|
|
576
|
+
// }
|
|
577
|
+
// });
|
|
578
|
+
// if (isWrapped) {
|
|
579
|
+
// parts.push(`${tagFill}</${itemName}>`);
|
|
580
|
+
// }
|
|
581
|
+
// } else if (items) {
|
|
582
|
+
// let name = shapeToXmlTagName(items as IAnyShape);
|
|
583
|
+
// if (name === UNKNOWN_TYPE) {
|
|
584
|
+
// name = label;
|
|
585
|
+
// }
|
|
586
|
+
// const opts = {
|
|
587
|
+
// forceName: name,
|
|
588
|
+
// indent: currentIndent + 1,
|
|
589
|
+
// };
|
|
590
|
+
// const value = items && this.processNode(items, opts);
|
|
591
|
+
// if (typeof value !== 'undefined') {
|
|
592
|
+
// const fill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
593
|
+
// parts.push(`${fill}${value}`);
|
|
594
|
+
// }
|
|
595
|
+
// }
|
|
596
|
+
|
|
597
|
+
// parts.push(`${rootFill}</${label}>`);
|
|
598
|
+
// return parts.join('\n');
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* Creates an example from an array shape when the items is the scalar shape.
|
|
603
|
+
* Note, it assumes the previous step tested whether the `items` is scalar.
|
|
604
|
+
*
|
|
605
|
+
* @param schema The array schema
|
|
606
|
+
* @param options Processing options.
|
|
607
|
+
*/
|
|
608
|
+
protected _scalarItems(schema: IArrayShape<IScalarShape>, options: IProcessNodeOptions={}): string {
|
|
609
|
+
const items = schema.items as IScalarShape;
|
|
610
|
+
const currentIndent = (options.indent || 0);
|
|
611
|
+
const tagFill = new Array(currentIndent * 2).fill(' ').join('');
|
|
612
|
+
|
|
613
|
+
let nodeName = '';
|
|
614
|
+
if (items.xmlSerialization && items.xmlSerialization.name) {
|
|
615
|
+
nodeName = normalizeXmlTagName(items.xmlSerialization.name);
|
|
616
|
+
} else {
|
|
617
|
+
nodeName = shapeToXmlTagName(schema.name ? schema : items);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// let's start with examples
|
|
621
|
+
if (this.opts.renderExamples) {
|
|
622
|
+
let { examples=[] } = schema;
|
|
623
|
+
if (Array.isArray(items.examples)) {
|
|
624
|
+
examples = examples.concat(items.examples);
|
|
625
|
+
}
|
|
626
|
+
const validExamples = examples.filter(item => !!item.structuredValue);
|
|
627
|
+
if (validExamples.length) {
|
|
628
|
+
const parts: string[] = [];
|
|
629
|
+
const generator = new XmlDataNodeGenerator();
|
|
630
|
+
validExamples.forEach((item) => {
|
|
631
|
+
const value = generator.generate(item.structuredValue!, nodeName);
|
|
632
|
+
if (value !== undefined) {
|
|
633
|
+
parts.push(`${tagFill}${value}`);
|
|
634
|
+
}
|
|
635
|
+
});
|
|
636
|
+
return parts.join('\n');
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// then the default value
|
|
641
|
+
if (schema.defaultValue) {
|
|
642
|
+
const generator = new XmlDataNodeGenerator();
|
|
643
|
+
const value = generator.generate(schema.defaultValue, nodeName);
|
|
644
|
+
if (value !== undefined) {
|
|
645
|
+
return `${tagFill}${value}`;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// finally we generate stuff.
|
|
650
|
+
const opts: IProcessNodeOptions = {
|
|
651
|
+
indent: currentIndent + 1,
|
|
652
|
+
forceName: nodeName,
|
|
653
|
+
};
|
|
654
|
+
const value = this.processNode(items, opts);
|
|
655
|
+
return `${tagFill}${value}`;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Renders a NodeShape as an array item.
|
|
660
|
+
*
|
|
661
|
+
* @param schema The array shape with item that is NodeShape.
|
|
662
|
+
* @param options Rendering options.
|
|
663
|
+
*/
|
|
664
|
+
protected _nodeItems(schema: IArrayShape<INodeShape>, options: IProcessNodeOptions={}): string {
|
|
665
|
+
const items = schema.items as INodeShape;
|
|
666
|
+
const currentIndent = (options.indent || 0);
|
|
667
|
+
const tagFill = new Array(currentIndent * 2).fill(' ').join('');
|
|
668
|
+
const isWrapped = schema.xmlSerialization && !!schema.xmlSerialization.wrapped;
|
|
669
|
+
const defaultValue = schema.defaultValue || items.defaultValue;
|
|
670
|
+
|
|
671
|
+
let nodeName = '';
|
|
672
|
+
if (items.xmlSerialization && items.xmlSerialization.name) {
|
|
673
|
+
nodeName = normalizeXmlTagName(items.xmlSerialization.name);
|
|
674
|
+
} else {
|
|
675
|
+
nodeName = shapeToXmlTagName(schema.name ? schema : items);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
const valueFill = isWrapped ? new Array(currentIndent * 2 + 2).fill(' ').join('') : tagFill;
|
|
679
|
+
const parts: string[] = [];
|
|
680
|
+
if (isWrapped) {
|
|
681
|
+
parts.push(`${tagFill}<${nodeName}>`);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
let rendered = false;
|
|
685
|
+
|
|
686
|
+
// let's start with examples
|
|
687
|
+
if (this.opts.renderExamples) {
|
|
688
|
+
let { examples=[] } = schema;
|
|
689
|
+
if (Array.isArray(items.examples)) {
|
|
690
|
+
examples = examples.concat(items.examples);
|
|
691
|
+
}
|
|
692
|
+
const validExamples = examples.filter(item => !!item.structuredValue);
|
|
693
|
+
if (validExamples.length) {
|
|
694
|
+
const parts: string[] = [];
|
|
695
|
+
const generator = new XmlDataNodeGenerator();
|
|
696
|
+
validExamples.forEach((item) => {
|
|
697
|
+
const value = generator.generate(item.structuredValue!, nodeName);
|
|
698
|
+
if (value !== undefined) {
|
|
699
|
+
parts.push(`${tagFill}${value}`);
|
|
700
|
+
}
|
|
701
|
+
});
|
|
702
|
+
rendered = true;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
if (!rendered && defaultValue) {
|
|
707
|
+
const generator = new XmlDataNodeGenerator();
|
|
708
|
+
const value = generator.generate(defaultValue, nodeName);
|
|
709
|
+
if (value !== undefined) {
|
|
710
|
+
parts.push(`${tagFill}${value}`);
|
|
711
|
+
rendered = true;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
if (!rendered) {
|
|
715
|
+
const properties = this._collectProperties(items);
|
|
716
|
+
properties.forEach((prop) => {
|
|
717
|
+
const value = this._propertyShapeObject(prop);
|
|
718
|
+
if (value) {
|
|
719
|
+
parts.push(`${valueFill}${value}`);
|
|
720
|
+
}
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
if (isWrapped) {
|
|
724
|
+
parts.push(`${tagFill}</${nodeName}>`);
|
|
725
|
+
}
|
|
726
|
+
return parts.join('\n');
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* @param example The example to turn into a JS object
|
|
731
|
+
*/
|
|
732
|
+
protected _exampleToObject(example?: IDataExample): any {
|
|
733
|
+
if (example && example.structuredValue) {
|
|
734
|
+
const generator = new XmlDataNodeGenerator();
|
|
735
|
+
return generator.generate(example.structuredValue);
|
|
736
|
+
}
|
|
737
|
+
return undefined;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
protected _unionShapeObject(schema: IUnionShape, options: IProcessNodeOptions={}): any {
|
|
741
|
+
let { anyOf=[], examples=[] } = schema;
|
|
742
|
+
if (Array.isArray(schema.inherits) && schema.inherits) {
|
|
743
|
+
schema.inherits.forEach((parent) => {
|
|
744
|
+
const anyParent = parent as IAnyShape;
|
|
745
|
+
if (Array.isArray(anyParent.examples) && anyParent.examples.length) {
|
|
746
|
+
examples = examples.concat(anyParent.examples);
|
|
747
|
+
}
|
|
748
|
+
const typed = parent as IUnionShape;
|
|
749
|
+
if (Array.isArray(typed.anyOf) && typed.anyOf.length) {
|
|
750
|
+
anyOf = anyOf.concat(typed.anyOf);
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
if (Array.isArray(anyOf) && anyOf.length) {
|
|
755
|
+
if (this._isNotRequiredUnion(anyOf)) {
|
|
756
|
+
// This generates schema for required values.
|
|
757
|
+
// This implicitly mean that the property is not required therefore the value should
|
|
758
|
+
// not be generated.
|
|
759
|
+
return undefined;
|
|
760
|
+
}
|
|
761
|
+
const example = examples.find((item) => !!item.structuredValue);
|
|
762
|
+
const value = this._exampleToObject(example);
|
|
763
|
+
if (value !== undefined) {
|
|
764
|
+
const label = shapeToXmlTagName(schema);
|
|
765
|
+
const currentIndent = (options.indent || 0);
|
|
766
|
+
const rootFill = new Array(currentIndent*2).fill(' ').join('');
|
|
767
|
+
const valueFill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
768
|
+
const parts = [];
|
|
769
|
+
parts.push(`${rootFill}<${label}>`);
|
|
770
|
+
const formatted = String(value).split('\n').filter(i => !!i).map(i => `${valueFill}${i}`).join('\n');
|
|
771
|
+
parts.push(`${formatted}`);
|
|
772
|
+
parts.push(`${rootFill}</${label}>`);
|
|
773
|
+
return parts.join('\n');
|
|
774
|
+
}
|
|
775
|
+
if (schema.defaultValue) {
|
|
776
|
+
return this._unionDefaultValue(schema, schema.defaultValue);
|
|
777
|
+
}
|
|
778
|
+
const member = this._readCurrentUnion(anyOf);
|
|
779
|
+
if (member) {
|
|
780
|
+
return this.processNode(member, { ...options, forceName: schema.name });
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
return undefined;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* @param schema The schema with unions
|
|
788
|
+
* @param defaultValue The definition of a default value.
|
|
789
|
+
*/
|
|
790
|
+
protected _unionDefaultValue(schema: IShapeUnion, defaultValue: IDataNodeUnion, options: IProcessNodeOptions = {}): any|undefined {
|
|
791
|
+
const gen = new XmlDataNodeGenerator();
|
|
792
|
+
const value = gen.generate(defaultValue);
|
|
793
|
+
const anySchema = schema as IAnyShape;
|
|
794
|
+
const label = shapeToXmlTagName(anySchema);
|
|
795
|
+
const currentIndent = (options.indent || 0);
|
|
796
|
+
const rootFill = new Array(currentIndent*2).fill(' ').join('');
|
|
797
|
+
const parts = [
|
|
798
|
+
`${rootFill}<${label}>`
|
|
799
|
+
];
|
|
800
|
+
const valueFill = new Array(currentIndent * 2 + 2).fill(' ').join('');
|
|
801
|
+
parts.push(`${valueFill}${String(value).trim()}`);
|
|
802
|
+
parts.push(`${rootFill}</${label}>`);
|
|
803
|
+
return parts.join('\n');
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
protected _fileShapeObject(): any {
|
|
807
|
+
return undefined;
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
protected _schemaShapeObject(): any {
|
|
811
|
+
return undefined;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
protected _tupleShapeObject(): any {
|
|
815
|
+
return undefined;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
protected _anyShapeObject(schema: IAnyShape): string {
|
|
819
|
+
const { and=[], xone=[], or=[] } = schema;
|
|
820
|
+
if (and.length) {
|
|
821
|
+
// we combine all properties together under `schema` with changed properties
|
|
822
|
+
const copy = JsonCore.clone(schema) as INodeShape;
|
|
823
|
+
copy.and = [];
|
|
824
|
+
copy.properties = [];
|
|
825
|
+
and.forEach((item) => {
|
|
826
|
+
const { types } = item;
|
|
827
|
+
if (types.includes(ns.aml.vocabularies.shapes.ScalarShape)) {
|
|
828
|
+
const typed = item as IScalarShape;
|
|
829
|
+
copy.properties.push(typed);
|
|
830
|
+
} else if (types.includes(ns.w3.shacl.NodeShape)) {
|
|
831
|
+
const typed = item as INodeShape;
|
|
832
|
+
typed.properties.forEach(i => copy.properties.push(i));
|
|
833
|
+
} else if (types.includes(ns.aml.vocabularies.shapes.ArrayShape) || types.includes(ns.aml.vocabularies.shapes.MatrixShape)) {
|
|
834
|
+
const typed = item as IArrayShape;
|
|
835
|
+
if (typed.items) {
|
|
836
|
+
copy.properties.push(typed.items)
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
});
|
|
840
|
+
return this._nodeShapeObject(copy);
|
|
841
|
+
}
|
|
842
|
+
if (xone.length) {
|
|
843
|
+
const { selectedUnions=[] } = this.opts;
|
|
844
|
+
let selected = xone.find(i => selectedUnions.includes(i.id));
|
|
845
|
+
if (!selected) {
|
|
846
|
+
// select firs available
|
|
847
|
+
selected = xone[0];
|
|
848
|
+
}
|
|
849
|
+
return this.processNode(selected);
|
|
850
|
+
}
|
|
851
|
+
if (or.length) {
|
|
852
|
+
const { selectedUnions=[] } = this.opts;
|
|
853
|
+
let selected = or.find(i => selectedUnions.includes(i.id));
|
|
854
|
+
if (!selected) {
|
|
855
|
+
// select firs available
|
|
856
|
+
selected = or[0];
|
|
857
|
+
}
|
|
858
|
+
return this.processNode(selected);
|
|
859
|
+
}
|
|
860
|
+
const { examples=[] } = schema;
|
|
861
|
+
const label = shapeToXmlTagName(schema);
|
|
862
|
+
if (this.opts.renderExamples && examples && examples.length) {
|
|
863
|
+
const example = examples.find((item) => !!item.structuredValue);
|
|
864
|
+
const value = this._exampleToObject(example);
|
|
865
|
+
const parts = [];
|
|
866
|
+
if (typeof value !== 'undefined') {
|
|
867
|
+
const valueFill = ` `;
|
|
868
|
+
parts.push(`<${label}>`);
|
|
869
|
+
parts.push(`${valueFill}${value}`);
|
|
870
|
+
parts.push(`</${label}>`);
|
|
871
|
+
}
|
|
872
|
+
return parts.join('\n');
|
|
873
|
+
}
|
|
874
|
+
return this._scalarShapeObject(schema);
|
|
875
|
+
}
|
|
876
|
+
}
|