node-opcua-schemas 2.76.2 → 2.78.0

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.
@@ -61,21 +61,6 @@ const predefinedType: any = {
61
61
  "ua:XmlElement": 1
62
62
  };
63
63
 
64
- const found: any = {};
65
-
66
- function resolveType(typeDictionary: string, typeName: string) {
67
- const namespace = typeName.split(":")[0];
68
- if (predefinedType[typeName]) {
69
- return;
70
- }
71
- if (!found[typeName]) {
72
- found[typeName] = typeName;
73
- }
74
- if (namespace === "ua") {
75
- /** */
76
- }
77
- }
78
-
79
64
  export interface EnumeratedType {
80
65
  name: string;
81
66
  documentation?: string;
@@ -85,36 +70,79 @@ export interface EnumeratedType {
85
70
 
86
71
  export interface StructureTypeRaw {
87
72
  name: string;
88
- baseType?: string;
73
+ baseType: string;
89
74
  base?: StructureTypeRaw;
90
75
  fields: any[];
91
76
  }
92
77
 
93
78
  export interface ITypeDictionary {
94
79
  targetNamespace: string;
80
+ defaultByteOrder: string;
95
81
  imports: string[];
96
- structuredTypesRaw: StructureTypeRaw[];
97
- enumeratedTypesRaw: EnumeratedType[];
98
82
  }
99
83
 
100
- export class TypeDictionary implements ITypeDictionary {
84
+ export class InternalTypeDictionary implements ITypeDictionary {
101
85
  public targetNamespace = "";
86
+ public defaultByteOrder = "";
102
87
  public imports: string[] = [];
103
- public structuredTypesRaw: StructureTypeRaw[] = [];
104
- public enumeratedTypesRaw: EnumeratedType[] = [];
105
- private structuredTypesRawMap: any = {};
88
+
89
+ private structuredTypesRaw: Record<string, StructureTypeRaw> = {};
90
+ private enumeratedTypesRaw: Record<string, EnumeratedType> = {};
91
+
92
+ // tns: "http://opcfoundation.org/a/b"
93
+ public _namespaces: Record<string, string> = {};
94
+
106
95
  constructor() {
107
96
  /** */
108
97
  }
109
- public addRaw(structuredType: StructureTypeRaw): void {
110
- this.structuredTypesRaw.push(structuredType);
111
- this.structuredTypesRawMap[structuredType.name] = structuredType;
98
+ public addEnumeration(name: string, e: EnumeratedType): void {
99
+ this.enumeratedTypesRaw[name] = e;
100
+ }
101
+ public getEnumerations(): EnumeratedType[] {
102
+ return Object.values(this.enumeratedTypesRaw);
103
+ }
104
+ public getStructures(): StructureTypeRaw[] {
105
+ return Object.values(this.structuredTypesRaw);
106
+ }
107
+ public addStructureRaw(structuredType: StructureTypeRaw): void {
108
+ this.structuredTypesRaw[structuredType.name] = structuredType;
112
109
  }
113
110
  public getStructuredTypesRawByName(name: string): StructureTypeRaw {
114
- return this.structuredTypesRawMap[name]! as StructureTypeRaw;
111
+ name = name.split(":")[1] || name;
112
+ return this.structuredTypesRaw[name]! as StructureTypeRaw;
115
113
  }
116
114
  }
117
115
 
116
+ interface _IParser {
117
+ attrs: any;
118
+ text?: string;
119
+ }
120
+ interface ITypeDefinitionParser extends _IParser {
121
+ typeDictionary: InternalTypeDictionary;
122
+ engine: { typeDictionary: InternalTypeDictionary };
123
+ }
124
+ interface IEnumeratedTypeParser extends _IParser {
125
+ typescriptDefinition: string;
126
+ enumeratedType: EnumeratedType;
127
+ parent: { typeDictionary: InternalTypeDictionary };
128
+ }
129
+ interface IEnumeratedTypeDocumentParser extends _IParser {
130
+ parent: IEnumeratedTypeParser;
131
+ }
132
+
133
+ interface IEnumeratedTypeEnumeratedValueParser extends _IParser {
134
+ parent: IEnumeratedTypeParser;
135
+ }
136
+ interface IStructureTypeParser extends _IParser {
137
+ structuredType: Omit<StructuredTypeOptions, "dataTypeFactory">;
138
+ parent: { typeDictionary: InternalTypeDictionary };
139
+ }
140
+ interface IImportParser extends _IParser {
141
+ parent: { typeDictionary: InternalTypeDictionary };
142
+ }
143
+ interface IStructureTypeFieldParser extends _IParser {
144
+ parent: IStructureTypeParser;
145
+ }
118
146
  /* tslint:disable:object-literal-shorthand */
119
147
  const state0: any = {
120
148
  init: () => {
@@ -122,17 +150,25 @@ const state0: any = {
122
150
  },
123
151
  parser: {
124
152
  TypeDictionary: {
125
- init: function (this: any, name: string, attributes: any) {
126
- this.typeDictionary = this.engine.typeDictionary as DataTypeFactory;
153
+ init: function (this: ITypeDefinitionParser, name: string, attributes: Record<string, string>) {
154
+ this.typeDictionary = this.engine.typeDictionary;
127
155
  this.typeDictionary.defaultByteOrder = attributes.DefaultByteOrder;
128
156
  this.typeDictionary.targetNamespace = attributes.TargetNamespace;
157
+
158
+ for (const [k, v] of Object.entries(attributes)) {
159
+ if (k.match(/xmlns:/)) {
160
+ const ns = k.split(":")[1];
161
+ this.typeDictionary._namespaces[ns] = v;
162
+ this.typeDictionary._namespaces[v] = ns;
163
+ }
164
+ }
129
165
  },
130
166
  parser: {
131
167
  Import: {
132
- init: function (this: any, name: string, attributes: any) {
168
+ init: function (this: IImportParser, name: string, attributes: any) {
133
169
  this.parent.typeDictionary.imports.push(attributes.Namespace);
134
170
  },
135
- finish: function (this: any) {
171
+ finish: function (this: IImportParser) {
136
172
  // _register_namespace_uri(this.text);
137
173
  // istanbul ignore next
138
174
  if (doDebug) {
@@ -142,7 +178,7 @@ const state0: any = {
142
178
  },
143
179
 
144
180
  EnumeratedType: {
145
- init: function (this: any) {
181
+ init: function (this: IEnumeratedTypeParser) {
146
182
  this.typescriptDefinition = "";
147
183
  // istanbul ignore next
148
184
  if (doDebug) {
@@ -164,12 +200,12 @@ const state0: any = {
164
200
  },
165
201
  parser: {
166
202
  Documentation: {
167
- finish: function (this: any) {
203
+ finish: function (this: IEnumeratedTypeDocumentParser) {
168
204
  this.parent.enumeratedType.documentation = this.text;
169
205
  }
170
206
  },
171
207
  EnumeratedValue: {
172
- finish: function (this: any) {
208
+ finish: function (this: IEnumeratedTypeEnumeratedValueParser) {
173
209
  // istanbul ignore next
174
210
  if (doDebug) {
175
211
  debugLog(" EnumeratedValue Name=", w(this.attrs.Name, 40), " Value=", this.attrs.Value);
@@ -182,9 +218,9 @@ const state0: any = {
182
218
  }
183
219
  }
184
220
  },
185
- finish: function (this: any) {
221
+ finish: function (this: IEnumeratedTypeParser) {
186
222
  this.typescriptDefinition += `\n}`;
187
- this.parent.typeDictionary.enumeratedTypesRaw[this.attrs.Name] = this.enumeratedType;
223
+ this.parent.typeDictionary.addEnumeration(this.attrs.Name, this.enumeratedType);
188
224
  // istanbul ignore next
189
225
  if (doDebug) {
190
226
  debugLog(" this.typescriptDefinition = ", this.typescriptDefinition);
@@ -192,7 +228,7 @@ const state0: any = {
192
228
  }
193
229
  },
194
230
  StructuredType: {
195
- init: function (this: any) {
231
+ init: function (this: IStructureTypeParser) {
196
232
  // istanbul ignore next
197
233
  if (doDebug) {
198
234
  debugLog(
@@ -205,21 +241,16 @@ const state0: any = {
205
241
 
206
242
  const baseType = this.attrs.BaseType;
207
243
 
208
- const base = this.parent.typeDictionary.structuredTypesRawMap[baseType];
209
-
210
- const structuredType: StructuredTypeOptions = {
244
+ const structuredType: Omit<StructuredTypeOptions, "dataTypeFactory"> = {
211
245
  name: this.attrs.Name,
212
- baseType: baseType,
246
+ baseType,
213
247
  fields: []
214
248
  };
215
- if (base) {
216
- structuredType.base = base;
217
- }
218
249
  this.structuredType = structuredType;
219
250
  },
220
251
  parser: {
221
252
  Field: {
222
- finish: function (this: any) {
253
+ finish: function (this: IStructureTypeFieldParser) {
223
254
  if (this.attrs.SourceType) {
224
255
  // ignore this field, This is a repetition of the base type field with same name
225
256
  return;
@@ -243,14 +274,13 @@ const state0: any = {
243
274
  // chalk.yellow(" lengthField="), w(this.attrs.LengthField, 40)
244
275
  );
245
276
  }
246
- resolveType(this.parent.typeDictionary, this.attrs.TypeName);
247
277
 
248
278
  const field: FieldInterfaceOptions = {
249
279
  name: this.attrs.Name,
250
280
  fieldType: this.attrs.TypeName
251
281
  };
252
282
 
253
- const structuredType: StructuredTypeOptions = this.parent.structuredType;
283
+ const structuredType = this.parent.structuredType;
254
284
  if (field.fieldType === "opc:Bit") {
255
285
  // do something to collect bits but ignore them as field
256
286
  structuredType.bitFields = structuredType.bitFields || [];
@@ -309,9 +339,9 @@ const state0: any = {
309
339
  }
310
340
  }
311
341
  },
312
- finish: function (this: any) {
342
+ finish: function (this: IStructureTypeParser) {
313
343
  assert(this.attrs.Name === this.structuredType.name);
314
- this.parent.typeDictionary.addRaw(this.structuredType);
344
+ this.parent.typeDictionary.addStructureRaw(this.structuredType);
315
345
  }
316
346
  }
317
347
  }
@@ -329,29 +359,29 @@ export interface MapDataTypeAndEncodingIdProvider {
329
359
  // getDataTypeNodeId(key: string): NodeId;
330
360
  getDataTypeAndEncodingId(key: string): DataTypeAndEncodingId | null;
331
361
  }
332
-
362
+ interface WithTypeDictionary extends Xml2Json {
363
+ typeDictionary: InternalTypeDictionary;
364
+ }
333
365
  export function parseBinaryXSD(
334
366
  xmlString: string,
335
367
  idProvider: MapDataTypeAndEncodingIdProvider,
336
368
  dataTypeFactory: DataTypeFactory,
337
369
  callback: (err?: Error | null) => void
338
370
  ): void {
339
- const parser = new Xml2Json(state0);
340
- const typeDictionary = new TypeDictionary();
341
- (parser as any).typeDictionary = typeDictionary;
342
-
371
+ const parser = new Xml2Json(state0) as WithTypeDictionary;
372
+ const typeDictionary = new InternalTypeDictionary();
373
+ parser.typeDictionary = typeDictionary;
374
+ if (!xmlString || xmlString.length === 0) {
375
+ return callback();
376
+ }
343
377
  parser.parseString(xmlString, (err?: Error | null) => {
344
378
  // resolve and prepare enumerations
345
- for (const key in typeDictionary.enumeratedTypesRaw) {
346
- if (!Object.prototype.hasOwnProperty.call(typeDictionary.enumeratedTypesRaw, key)) {
347
- continue;
348
- }
349
- const enumeratedType = typeDictionary.enumeratedTypesRaw[key];
379
+ for (const enumeratedType of typeDictionary.getEnumerations()) {
350
380
  if (Object.keys(enumeratedType.enumeratedValues).length >= 1) {
351
- const e = new EnumerationDefinitionSchema({
381
+ const e = new EnumerationDefinitionSchema(NodeId.nullNodeId, {
352
382
  lengthInBits: enumeratedType.lengthInBits || 32,
353
383
  enumValues: enumeratedType.enumeratedValues,
354
- name: key
384
+ name: enumeratedType.name
355
385
  });
356
386
  dataTypeFactory.registerEnumeration(e);
357
387
  }
@@ -360,43 +390,49 @@ export function parseBinaryXSD(
360
390
  // istanbul ignore next
361
391
  if (doDebug) {
362
392
  debugLog("------------------------------- Resolving complex Type");
363
- typeDictionary.structuredTypesRaw.map((x: any) => debugLog(x.name));
393
+ typeDictionary.getStructures().map((x: any) => debugLog(x.name));
364
394
  }
365
395
 
366
396
  // create area in navigation order
367
397
  function createExplorationOrder(): StructureTypeRaw[] {
368
398
  const array: StructureTypeRaw[] = [];
369
- const map: any = {};
370
-
399
+ const _map: Record<string, string> = {};
400
+ function alreadyVisited(name: string) {
401
+ name = name.split(":")[1] || name;
402
+ return !!_map[name];
403
+ }
404
+ function markAsVisited(name: string) {
405
+ name = name.split(":")[1] || name;
406
+ _map[name] = "1";
407
+ }
371
408
  function visitStructure(structuredType: StructureTypeRaw) {
372
- if (!structuredType) {
409
+ if (!structuredType || structuredType.name === "ua:ExtensionObject") {
373
410
  return;
374
411
  }
375
- if (map[structuredType.name]) {
412
+ if (alreadyVisited(structuredType.name)) {
376
413
  return;
377
414
  }
378
- if (structuredType.baseType) {
415
+ markAsVisited(structuredType.name);
416
+ if (structuredType.baseType && structuredType.baseType !== "ua:ExtensionObject") {
379
417
  const base = typeDictionary.getStructuredTypesRawByName(structuredType.baseType);
380
- if (base) {
418
+ if (base && base.baseType) {
419
+ doDebug && debugLog(" investigating base", chalk.cyan(base.name));
381
420
  visitStructure(base);
382
421
  }
383
422
  }
384
423
  for (const f of structuredType.fields) {
385
- const fieldType = f.fieldType.split(":")[1];
386
- const s = typeDictionary.getStructuredTypesRawByName(fieldType);
424
+ const s = typeDictionary.getStructuredTypesRawByName(f.fieldType);
387
425
  if (s !== structuredType && s) {
388
426
  visitStructure(s);
389
427
  } else {
390
- map[fieldType] = "1";
428
+ markAsVisited(f.fieldType);
391
429
  }
392
430
  }
393
- if (!map[structuredType.name]) {
394
- map[structuredType.name] = 1;
395
- array.push(structuredType);
396
- }
431
+ doDebug && debugLog("processing ", chalk.cyan(structuredType.name));
432
+ array.push(structuredType);
397
433
  }
398
434
 
399
- for (const structuredType of typeDictionary.structuredTypesRaw) {
435
+ for (const structuredType of typeDictionary.getStructures()) {
400
436
  visitStructure(structuredType);
401
437
  }
402
438
  return array;
@@ -404,7 +440,6 @@ export function parseBinaryXSD(
404
440
  // resolve complex types
405
441
  const schemaInVisitingOrder = createExplorationOrder();
406
442
  for (const structuredType of schemaInVisitingOrder) {
407
- debugLog("processing ", chalk.cyan(structuredType.name));
408
443
  getOrCreateStructuredTypeSchema(structuredType.name, typeDictionary, dataTypeFactory, idProvider);
409
444
  }
410
445
  callback(err);
@@ -1,8 +1,20 @@
1
- import { ConstructorFuncWithSchema, DataTypeFactory, EnumerationDefinitionSchema, IStructuredTypeSchema, StructuredTypeSchema } from "node-opcua-factory";
1
+ import {
2
+ ConstructorFuncWithSchema,
3
+ DataTypeFactory,
4
+ EnumerationDefinitionSchema,
5
+ IStructuredTypeSchema,
6
+ StructureInfo
7
+ } from "node-opcua-factory";
2
8
 
3
9
  export function toTypeScript(dataTypeFactory: DataTypeFactory): string {
4
- const enumeratedTypes: Map<string, EnumerationDefinitionSchema> = (dataTypeFactory as any)._enumerations;
5
- const structuredTypes: Map<string, ConstructorFuncWithSchema> = (dataTypeFactory as any)._structureTypeConstructorByNameMap;
10
+ const enumeratedTypes: Map<string, EnumerationDefinitionSchema> = new Map();
11
+ for (const k of dataTypeFactory.enumerations()) {
12
+ enumeratedTypes.set(k, dataTypeFactory.getEnumeration(k)!);
13
+ }
14
+ const structuredTypes: Map<string, StructureInfo> = new Map();
15
+ for (const k of dataTypeFactory.structuredTypesNames()) {
16
+ structuredTypes.set(k, dataTypeFactory.getStructureInfoByTypeName(k)!);
17
+ }
6
18
 
7
19
  const declaration: Map<string, string> = new Map();
8
20
 
@@ -12,7 +24,7 @@ export function toTypeScript(dataTypeFactory: DataTypeFactory): string {
12
24
  } else if (t === "Boolean") {
13
25
  t = "UABoolean";
14
26
  }
15
-
27
+
16
28
  if (!enumeratedTypes.has(t) && !structuredTypes.has(t)) {
17
29
  declaration.set(t, t);
18
30
  }
@@ -35,15 +47,12 @@ export function toTypeScript(dataTypeFactory: DataTypeFactory): string {
35
47
  const alreadyDone: Set<string> = new Set();
36
48
  function dumpType(o: IStructuredTypeSchema) {
37
49
  // base type first
38
- const b = o.baseType;
39
-
40
- const bt = structuredTypes.get(b)?.schema;
41
-
42
- if (b && !alreadyDone.has(o.baseType) && bt) {
50
+ const bt = o.getBaseSchema();
51
+ if (bt && !alreadyDone.has(o.baseType) && bt) {
43
52
  dumpType(bt);
44
53
  }
45
54
  alreadyDone.add(o.name);
46
- const ex1 = b && bt ? `extends ${b} ` : "";
55
+ const ex1 = bt ? `extends ${bt.name} ` : "";
47
56
 
48
57
  if (o.baseType === "Union") {
49
58
  const p: string[] = [];
package/source/tools.ts CHANGED
@@ -8,20 +8,19 @@ import {
8
8
  FieldCategory,
9
9
  getBuiltInType,
10
10
  hasBuiltInType,
11
- hasStructuredType,
12
11
  IStructuredTypeSchema,
13
- StructuredTypeOptions
12
+ StructuredTypeSchema
14
13
  } from "node-opcua-factory";
15
14
  import { ExpandedNodeId } from "node-opcua-nodeid";
16
15
 
17
16
  import { createDynamicObjectConstructor } from "./dynamic_extension_object";
18
- import { MapDataTypeAndEncodingIdProvider, TypeDictionary } from "./parse_binary_xsd";
17
+ import { InternalTypeDictionary, MapDataTypeAndEncodingIdProvider } from "./parse_binary_xsd";
19
18
 
20
19
  const errorLog = make_errorLog(__filename);
21
20
 
22
- function _removeNamespacePart(str?: string): string | undefined {
21
+ function _removeNamespacePart(str?: string): string {
23
22
  if (!str) {
24
- return str;
23
+ return str || "";
25
24
  }
26
25
  const data = str.split(":");
27
26
  return data.length > 1 ? data[1] : str;
@@ -45,13 +44,12 @@ function _adjustFieldTypeName(fieldTypeName: string): string {
45
44
 
46
45
  export function getOrCreateStructuredTypeSchema(
47
46
  name: string,
48
- typeDictionary: TypeDictionary,
47
+ typeDictionary: InternalTypeDictionary,
49
48
  dataTypeFactory: DataTypeFactory,
50
49
  idProvider: MapDataTypeAndEncodingIdProvider
51
50
  ): IStructuredTypeSchema {
52
- // eslint-disable-next-line complexity
53
51
  function _getOrCreateStructuredTypeSchema(_name: string): IStructuredTypeSchema {
54
- if (dataTypeFactory.hasStructuredType(_name)) {
52
+ if (dataTypeFactory.hasStructureByTypeName(_name)) {
55
53
  return dataTypeFactory.getStructuredTypeSchema(_name);
56
54
  }
57
55
  if (dataTypeFactory.hasEnumeration(_name)) {
@@ -94,83 +92,11 @@ export function getOrCreateStructuredTypeSchema(
94
92
  return index < 0;
95
93
  });
96
94
  }
97
- for (const field of structuredType.fields) {
98
- const fieldType = field.fieldType;
99
- if (!field.schema) {
100
- const prefix = _getNamespacePart(fieldType);
101
- const fieldTypeName = _adjustFieldTypeName(_removeNamespacePart(fieldType)!);
102
-
103
- switch (prefix) {
104
- case "tns":
105
- field.fieldType = fieldTypeName;
106
- if (dataTypeFactory.hasEnumeration(fieldTypeName)) {
107
- const enumeratedType = dataTypeFactory.getEnumeration(fieldTypeName);
108
- field.category = FieldCategory.enumeration;
109
- field.schema = enumeratedType;
110
- } else {
111
- // must be a structure then ....
112
- field.category = FieldCategory.complex;
113
- const schema1 = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
114
- field.schema = schema1;
115
- // _getOrCreateStructuredTypeSchema(fieldTypeName);
116
-
117
- // istanbul ignore next
118
- if (!field.schema) {
119
- errorLog("cannot find schema for ", fieldTypeName);
120
- }
121
- }
122
- break;
123
- case "ua":
124
- field.fieldType = fieldTypeName;
125
- if (hasBuiltInType(fieldTypeName)) {
126
- field.category = FieldCategory.basic;
127
- field.schema = getBuiltInType(fieldTypeName);
128
- } else if (dataTypeFactory.hasStructuredType(fieldTypeName)) {
129
- field.category = FieldCategory.complex;
130
- field.schema = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
131
- } else {
132
- field.category = FieldCategory.basic;
133
- // try in this
134
- field.schema = _getOrCreateStructuredTypeSchema(fieldTypeName);
135
- // istanbul ignore next
136
- if (!field.schema) {
137
- errorLog("What should I do ??", fieldTypeName, " ", hasStructuredType(fieldTypeName));
138
- } else {
139
- if (hasBuiltInType(fieldTypeName)) {
140
- field.category = FieldCategory.basic;
141
- } else {
142
- field.category = FieldCategory.complex;
143
- }
144
- }
145
- }
146
- break;
147
- case "opc":
148
- if ((fieldTypeName === "UAString" || fieldTypeName === "String") && field.name === "IndexRange") {
149
- field.fieldType = "NumericRange";
150
- } else {
151
- field.fieldType = fieldTypeName;
152
- }
153
- if (!hasBuiltInType(fieldTypeName)) {
154
- throw new Error("Unknown basic type " + fieldTypeName);
155
- }
156
- field.category = FieldCategory.basic;
157
- break;
158
- default:
159
- if (dataTypeFactory.hasEnumeration(fieldTypeName)) {
160
- field.category = FieldCategory.enumeration;
161
- const enumeratedType = dataTypeFactory.getEnumeration(fieldTypeName);
162
- field.schema = enumeratedType;
163
- } else if (dataTypeFactory.hasStructuredType(fieldTypeName)) {
164
- field.category = FieldCategory.complex;
165
- const schema1 = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
166
- field.schema = schema1;
167
- }
168
- break;
169
- }
170
- }
171
- }
172
-
173
- const schema = buildStructuredType(structuredType as StructuredTypeOptions);
95
+ applyOnFields();
96
+ const schema = new StructuredTypeSchema({
97
+ ...structuredType,
98
+ dataTypeFactory
99
+ });
174
100
  const ids = idProvider.getDataTypeAndEncodingId(schema.name);
175
101
  if (!ids) {
176
102
  // this may happen if the type is abstract or if the type referes to a internal ExtnsionObject
@@ -179,9 +105,8 @@ export function getOrCreateStructuredTypeSchema(
179
105
  const Constructor = createDynamicObjectConstructor(schema, dataTypeFactory) as unknown as ConstructorFuncWithSchema;
180
106
  return schema;
181
107
  }
182
- schema.id = ids.dataTypeNodeId;
183
108
  schema.dataTypeNodeId = ids.dataTypeNodeId;
184
- if (schema.id.namespace === 0 && schema.id.value === 0) {
109
+ if (schema.dataTypeNodeId.namespace === 0 && schema.dataTypeNodeId.value === 0) {
185
110
  return schema;
186
111
  }
187
112
  schema.encodingDefaultXml = ExpandedNodeId.fromNodeId(ids.xmlEncodingNodeId);
@@ -191,8 +116,68 @@ export function getOrCreateStructuredTypeSchema(
191
116
  const Constructor = createDynamicObjectConstructor(schema, dataTypeFactory) as unknown as ConstructorFuncWithSchema;
192
117
  Constructor.encodingDefaultBinary = schema.encodingDefaultBinary;
193
118
  Constructor.encodingDefaultXml = schema.encodingDefaultXml;
194
-
119
+ Constructor.encodingDefaultJson = schema.encodingDefaultJson;
195
120
  return schema;
121
+
122
+ function applyOnFields() {
123
+ for (const field of structuredType.fields) {
124
+ const fieldType = field.fieldType;
125
+ if (!field.schema) {
126
+ const prefix = _getNamespacePart(fieldType);
127
+ const fieldTypeName = _adjustFieldTypeName(_removeNamespacePart(fieldType)!);
128
+
129
+ switch (prefix) {
130
+ case "ua":
131
+ field.fieldType = fieldTypeName;
132
+ if (hasBuiltInType(fieldTypeName)) {
133
+ field.category = FieldCategory.basic;
134
+ field.schema = getBuiltInType(fieldTypeName);
135
+ } else if (dataTypeFactory.hasStructureByTypeName(fieldTypeName)) {
136
+ field.category = FieldCategory.complex;
137
+ field.schema = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
138
+ } else {
139
+ field.category = FieldCategory.basic;
140
+ // try in this
141
+ field.schema = _getOrCreateStructuredTypeSchema(fieldTypeName);
142
+ // istanbul ignore next
143
+ if (!field.schema) {
144
+ errorLog("What should I do ??", fieldTypeName, " ", dataTypeFactory.hasStructureByTypeName(fieldTypeName));
145
+ } else {
146
+ if (hasBuiltInType(fieldTypeName)) {
147
+ field.category = FieldCategory.basic;
148
+ } else {
149
+ field.category = FieldCategory.complex;
150
+ }
151
+ }
152
+ }
153
+ break;
154
+ case "opc":
155
+ if ((fieldTypeName === "UAString" || fieldTypeName === "String") && field.name === "IndexRange") {
156
+ field.fieldType = "NumericRange";
157
+ } else {
158
+ field.fieldType = fieldTypeName;
159
+ }
160
+ if (!hasBuiltInType(fieldTypeName)) {
161
+ throw new Error("Unknown basic type " + fieldTypeName);
162
+ }
163
+ field.category = FieldCategory.basic;
164
+ break;
165
+ default:
166
+ field.fieldType = fieldTypeName;
167
+ if (dataTypeFactory.hasEnumeration(fieldTypeName)) {
168
+ field.category = FieldCategory.enumeration;
169
+ const enumeratedType = dataTypeFactory.getEnumeration(fieldTypeName);
170
+ field.schema = enumeratedType;
171
+ } else if (dataTypeFactory.hasStructureByTypeName(fieldTypeName)) {
172
+ field.category = FieldCategory.complex;
173
+ const schema1 = dataTypeFactory.getStructuredTypeSchema(fieldTypeName);
174
+ field.schema = schema1;
175
+ }
176
+ break;
177
+ }
178
+ }
179
+ }
180
+ }
196
181
  }
197
182
  return _getOrCreateStructuredTypeSchema(name);
198
183
  }