node-opcua-factory 2.71.0 → 2.73.1

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.
Files changed (94) hide show
  1. package/dist/base_ua_object.d.ts +53 -0
  2. package/dist/base_ua_object.js +536 -0
  3. package/dist/base_ua_object.js.map +1 -0
  4. package/dist/basic_type.d.ts +40 -0
  5. package/dist/basic_type.js +119 -0
  6. package/dist/basic_type.js.map +1 -0
  7. package/dist/builtin_types.d.ts +46 -0
  8. package/dist/builtin_types.js +284 -0
  9. package/dist/builtin_types.js.map +1 -0
  10. package/dist/builtin_types_special.d.ts +5 -0
  11. package/dist/builtin_types_special.js +46 -0
  12. package/dist/builtin_types_special.js.map +1 -0
  13. package/dist/constructor_type.d.ts +15 -15
  14. package/dist/constructor_type.js +2 -2
  15. package/dist/datatype_factory.d.ts +36 -39
  16. package/dist/datatype_factory.js +305 -322
  17. package/dist/datatype_factory.js.map +1 -1
  18. package/dist/encode_decode.d.ts +9 -0
  19. package/dist/encode_decode.js +40 -0
  20. package/dist/encode_decode.js.map +1 -0
  21. package/dist/enumerations.d.ts +32 -0
  22. package/dist/enumerations.js +78 -0
  23. package/dist/enumerations.js.map +1 -0
  24. package/dist/factories_baseobject.d.ts +56 -56
  25. package/dist/factories_baseobject.js +534 -534
  26. package/dist/factories_basic_type.d.ts +40 -40
  27. package/dist/factories_basic_type.js +135 -135
  28. package/dist/factories_builtin_types.d.ts +32 -32
  29. package/dist/factories_builtin_types.js +261 -261
  30. package/dist/factories_builtin_types_special.d.ts +5 -5
  31. package/dist/factories_builtin_types_special.js +45 -45
  32. package/dist/factories_enumerations.d.ts +31 -31
  33. package/dist/factories_enumerations.js +77 -77
  34. package/dist/factories_factories.d.ts +17 -17
  35. package/dist/factories_factories.js +53 -53
  36. package/dist/factories_id_generator.d.ts +3 -3
  37. package/dist/factories_id_generator.js +21 -21
  38. package/dist/factories_schema_helpers.d.ts +27 -27
  39. package/dist/factories_schema_helpers.js +121 -121
  40. package/dist/factories_structuredTypeSchema.d.ts +45 -45
  41. package/dist/factories_structuredTypeSchema.js +276 -276
  42. package/dist/get_built_in_type.d.ts +0 -0
  43. package/dist/get_built_in_type.js +2 -0
  44. package/dist/get_built_in_type.js.map +1 -0
  45. package/dist/get_standard_data_type_factory.d.ts +12 -0
  46. package/dist/get_standard_data_type_factory.js +35 -0
  47. package/dist/get_standard_data_type_factory.js.map +1 -0
  48. package/dist/get_structured_type_schema.d.ts +4 -0
  49. package/dist/get_structured_type_schema.js +13 -0
  50. package/dist/get_structured_type_schema.js.map +1 -0
  51. package/dist/id_generator.d.ts +3 -0
  52. package/dist/id_generator.js +22 -0
  53. package/dist/id_generator.js.map +1 -0
  54. package/dist/index.d.ts +18 -15
  55. package/dist/index.js +34 -31
  56. package/dist/index.js.map +1 -1
  57. package/dist/nodeid_type.d.ts +13 -0
  58. package/dist/nodeid_type.js +19 -0
  59. package/dist/nodeid_type.js.map +1 -0
  60. package/dist/parameters.d.ts +3 -0
  61. package/dist/parameters.js +7 -0
  62. package/dist/parameters.js.map +1 -0
  63. package/dist/register_class_definition.d.ts +3 -0
  64. package/dist/register_class_definition.js +9 -0
  65. package/dist/register_class_definition.js.map +1 -0
  66. package/dist/schema_helpers.d.ts +24 -0
  67. package/dist/schema_helpers.js +100 -0
  68. package/dist/schema_helpers.js.map +1 -0
  69. package/dist/structured_type_schema.d.ts +44 -0
  70. package/dist/structured_type_schema.js +279 -0
  71. package/dist/structured_type_schema.js.map +1 -0
  72. package/dist/types.d.ts +151 -110
  73. package/dist/types.js +9 -51
  74. package/dist/types.js.map +1 -1
  75. package/package.json +7 -6
  76. package/source/{factories_baseobject.ts → base_ua_object.ts} +48 -44
  77. package/source/{factories_basic_type.ts → basic_type.ts} +7 -28
  78. package/source/{factories_builtin_types.ts → builtin_types.ts} +111 -89
  79. package/source/{factories_builtin_types_special.ts → builtin_types_special.ts} +2 -2
  80. package/source/datatype_factory.ts +44 -47
  81. package/source/encode_decode.ts +37 -0
  82. package/source/{factories_enumerations.ts → enumerations.ts} +6 -4
  83. package/source/get_built_in_type.ts +0 -0
  84. package/source/{factories_factories.ts → get_standard_data_type_factory.ts} +7 -25
  85. package/source/get_structured_type_schema.ts +10 -0
  86. package/source/{factories_id_generator.ts → id_generator.ts} +0 -0
  87. package/source/index.ts +14 -11
  88. package/source/nodeid_type.ts +17 -0
  89. package/source/parameters.ts +3 -0
  90. package/source/register_class_definition.ts +11 -0
  91. package/source/{factories_schema_helpers.ts → schema_helpers.ts} +15 -37
  92. package/source/{factories_structuredTypeSchema.ts → structured_type_schema.ts} +37 -36
  93. package/source/types.ts +104 -79
  94. package/source/constructor_type.ts +0 -18
@@ -5,8 +5,8 @@
5
5
  import { assert } from "node-opcua-assert";
6
6
  import { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream";
7
7
 
8
- import { registerType } from "./factories_builtin_types";
9
- import { ConstructorFunc } from "./constructor_type";
8
+ import { registerType } from "./builtin_types";
9
+ import { ConstructorFunc } from "./types";
10
10
 
11
11
  function _self_encode(constructor: any) {
12
12
  assert(typeof constructor === "function");
@@ -7,17 +7,38 @@ import * as chalk from "chalk";
7
7
  import { assert } from "node-opcua-assert";
8
8
  import { checkDebugFlag, make_debugLog } from "node-opcua-debug";
9
9
  import { ExpandedNodeId, NodeId } from "node-opcua-nodeid";
10
+ import { DataTypeIds } from "node-opcua-constants";
10
11
 
11
- import { ConstructorFunc, ConstructorFuncWithSchema } from "./constructor_type";
12
- import { BaseUAObject } from "./factories_baseobject";
13
- import { getBuildInType, hasBuiltInType } from "./factories_builtin_types";
14
- import { EnumerationDefinitionSchema, getEnumeration, hasEnumeration } from "./factories_enumerations";
15
- import { StructuredTypeSchema } from "./factories_structuredTypeSchema";
16
- import { BasicTypeDefinition } from "./types";
12
+ import { getBuiltInType as getBuiltInType, hasBuiltInType } from "./builtin_types";
13
+ import { EnumerationDefinitionSchema, getBuiltInEnumeration, hasBuiltInEnumeration } from "./enumerations";
14
+ import {
15
+ CommonInterface,
16
+ StructuredTypeField,
17
+ IStructuredTypeSchema,
18
+ ConstructorFuncWithSchema,
19
+ ConstructorFunc,
20
+ IBaseUAObject
21
+ } from "./types";
17
22
 
18
23
  const debugLog = make_debugLog(__filename);
19
24
  const doDebug = checkDebugFlag(__filename);
20
25
 
26
+ export function _findFieldSchema(typeDictionary: DataTypeFactory, field: StructuredTypeField, value: any): IStructuredTypeSchema {
27
+ const fieldType = field.fieldType;
28
+
29
+ if (field.allowSubType && field.category === "complex") {
30
+ const fieldTypeConstructor = value ? value.constructor : field.fieldTypeConstructor;
31
+
32
+ const _newFieldSchema = fieldTypeConstructor.schema;
33
+
34
+ return _newFieldSchema as IStructuredTypeSchema;
35
+ }
36
+
37
+ const fieldTypeConstructor = field.fieldTypeConstructor || typeDictionary.getStructureTypeConstructor(fieldType);
38
+
39
+ return (field.schema as IStructuredTypeSchema) || (fieldTypeConstructor as any).schema;
40
+ }
41
+
21
42
  export class DataTypeFactory {
22
43
  public defaultByteOrder: string;
23
44
  public targetNamespace: string;
@@ -27,7 +48,6 @@ export class DataTypeFactory {
27
48
  private _structureTypeConstructorByDataTypeMap: Map<string, ConstructorFuncWithSchema> = new Map();
28
49
  private _structureTypeConstructorByEncodingNodeIdMap: Map<string, any> = new Map();
29
50
  private _enumerations: Map<string, EnumerationDefinitionSchema> = new Map();
30
- private _simpleTypes: Map<string, { nodeId: NodeId; definition: BasicTypeDefinition }> = new Map();
31
51
 
32
52
  private baseDataFactories: DataTypeFactory[];
33
53
 
@@ -40,46 +60,23 @@ export class DataTypeFactory {
40
60
  public repairBaseDataFactories(baseDataFactories: DataTypeFactory[]): void {
41
61
  this.baseDataFactories = baseDataFactories;
42
62
  }
43
- // -----------------------------
44
- public registerSimpleType(name: string, dataTypeNodeId: NodeId, def: BasicTypeDefinition): void {
45
- // istanbul ignore next
46
- if (this._simpleTypes.has(name)) {
47
- throw new Error("registerSimpleType " + name + " already register");
48
- }
49
- this._simpleTypes.set(name, { nodeId: dataTypeNodeId, definition: def });
50
- }
51
63
 
52
- public hasSimpleType(name: string): boolean {
53
- if (this._simpleTypes.has(name)) {
54
- return true;
55
- }
56
- for (const factory of this.baseDataFactories) {
57
- if (factory.hasSimpleType(name)) {
58
- return true;
59
- }
60
- }
61
- const hasSimpleT = hasBuiltInType(name);
62
- if (hasSimpleT) {
63
- return hasSimpleT;
64
- }
64
+ public hasBuiltInType(name: string): boolean {
65
65
  return hasBuiltInType(name);
66
66
  }
67
- public getSimpleType(name: string): BasicTypeDefinition {
68
- if (this._simpleTypes.has(name)) {
69
- return this._simpleTypes.get(name)!.definition;
70
- }
71
- for (const factory of this.baseDataFactories) {
72
- if (factory.hasSimpleType(name)) {
73
- return factory.getSimpleType(name);
74
- }
75
- }
76
- return getBuildInType(name);
67
+
68
+ public getBuiltInType(name: string): CommonInterface {
69
+ return getBuiltInType(name);
77
70
  }
71
+
72
+ public getBuiltInTypeByDataType(nodeId: NodeId): CommonInterface {
73
+ return getBuiltInType(DataTypeIds[nodeId.value as number]);
74
+ }
75
+
78
76
  // -----------------------------
79
77
  // EnumerationDefinitionSchema
80
78
  public registerEnumeration(enumeration: EnumerationDefinitionSchema): void {
81
- debugLog("Registering Enumeration ", enumeration.name);
82
- assert(!this._enumerations.has(enumeration.name));
79
+ assert(!this._enumerations.has(enumeration.name), "enumeration already registered");
83
80
  this._enumerations.set(enumeration.name, enumeration);
84
81
  }
85
82
 
@@ -93,11 +90,12 @@ export class DataTypeFactory {
93
90
  return true;
94
91
  }
95
92
  }
96
- if (hasEnumeration(enumName)) {
93
+ if (hasBuiltInEnumeration(enumName)) {
97
94
  return true;
98
95
  }
99
96
  return false;
100
97
  }
98
+
101
99
  public getEnumeration(enumName: string): EnumerationDefinitionSchema | null {
102
100
  if (this._enumerations.has(enumName)) {
103
101
  return this._enumerations.get(enumName) || null;
@@ -109,7 +107,7 @@ export class DataTypeFactory {
109
107
  return e;
110
108
  }
111
109
  }
112
- const ee = getEnumeration(enumName);
110
+ const ee = getBuiltInEnumeration(enumName);
113
111
  return ee;
114
112
  }
115
113
  // ----------------------------
@@ -178,7 +176,7 @@ export class DataTypeFactory {
178
176
  return false;
179
177
  }
180
178
 
181
- public getStructuredTypeSchema(typeName: string): StructuredTypeSchema {
179
+ public getStructuredTypeSchema(typeName: string): IStructuredTypeSchema {
182
180
  const constructor = this.getStructureTypeConstructor(typeName);
183
181
  return constructor.schema;
184
182
  }
@@ -245,7 +243,7 @@ export class DataTypeFactory {
245
243
  return false;
246
244
  }
247
245
 
248
- public constructObject(binaryEncodingNodeId: NodeId): BaseUAObject {
246
+ public constructObject(binaryEncodingNodeId: NodeId): IBaseUAObject {
249
247
  if (!verifyExpandedNodeId(binaryEncodingNodeId)) {
250
248
  throw new Error(" constructObject : invalid expandedNodeId provided " + binaryEncodingNodeId.toString());
251
249
  }
@@ -253,8 +251,7 @@ export class DataTypeFactory {
253
251
 
254
252
  if (!constructor) {
255
253
  debugLog("Cannot find constructor for " + binaryEncodingNodeId.toString());
256
- return new BaseUAObject();
257
- // throw new Error("Cannot find constructor for " + expandedNodeId.toString());
254
+ throw new Error("Cannot find constructor for " + binaryEncodingNodeId.toString());
258
255
  }
259
256
  return new constructor();
260
257
  }
@@ -315,7 +312,7 @@ export class DataTypeFactory {
315
312
  }
316
313
  }
317
314
 
318
- function dumpSchema(schema: StructuredTypeSchema, write: any) {
315
+ function dumpSchema(schema: IStructuredTypeSchema, write: any) {
319
316
  write("name ", schema.name);
320
317
  write("dataType ", schema.dataTypeNodeId.toString());
321
318
  write("binaryEncoding ", schema.encodingDefaultBinary!.toString());
@@ -0,0 +1,37 @@
1
+ import assert from "node-opcua-assert";
2
+ import { OutputBinaryStream, BinaryStream } from "node-opcua-binary-stream";
3
+
4
+ export function defaultEncode(value: any, stream: OutputBinaryStream): void {
5
+ /** */
6
+ }
7
+
8
+ export function defaultDecode(stream: BinaryStream): any {
9
+ return null;
10
+ }
11
+
12
+ export function defaultGuidValue(): any {
13
+ return Buffer.alloc(0);
14
+ }
15
+
16
+ export function toJSONGuid(value: any): any {
17
+ if (typeof value === "string") {
18
+ return value;
19
+ }
20
+ assert(value instanceof Buffer);
21
+ return value.toString("base64");
22
+ }
23
+
24
+ export function encodeAny(value: any, stream: OutputBinaryStream) {
25
+ assert(false, "type 'Any' cannot be encoded");
26
+ }
27
+
28
+ export function decodeAny(stream: BinaryStream) {
29
+ assert(false, "type 'Any' cannot be decoded");
30
+ }
31
+
32
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
33
+ export function encodeNull(value: any, stream: OutputBinaryStream): void {}
34
+
35
+ export function decodeNull(stream: BinaryStream): any {
36
+ return null;
37
+ }
@@ -5,7 +5,8 @@ import { assert } from "node-opcua-assert";
5
5
 
6
6
  import { BinaryStream, OutputBinaryStream } from "node-opcua-binary-stream";
7
7
  import { Enum, EnumItem, _TypescriptEnum , adaptTypescriptEnum} from "node-opcua-enum";
8
- import { EnumerationDefinition, TypeSchemaBase, TypeSchemaConstructorOptions } from "./types";
8
+ import { TypeSchemaBase } from "./builtin_types";
9
+ import { EnumerationDefinition, TypeSchemaConstructorOptions } from "./types";
9
10
 
10
11
  function _encode_enumeration(typedEnum: Enum, value: number, stream: OutputBinaryStream): void {
11
12
  assert(typeof value === "number", "Expecting a number here");
@@ -88,13 +89,14 @@ export function registerEnumeration(options: EnumerationDefinitionOptions): Enum
88
89
  return enumerationDefinition.typedEnum;
89
90
  }
90
91
 
91
- export function hasEnumeration(enumerationName: string): boolean {
92
+ export function hasBuiltInEnumeration(enumerationName: string): boolean {
92
93
  return _enumerations.has(enumerationName);
93
94
  }
94
95
 
95
- export function getEnumeration(enumerationName: string): EnumerationDefinitionSchema {
96
- if (!exports.hasEnumeration(enumerationName)) {
96
+ export function getBuiltInEnumeration(enumerationName: string): EnumerationDefinitionSchema {
97
+ if (!hasBuiltInEnumeration(enumerationName)) {
97
98
  throw new Error("Cannot find enumeration with type " + enumerationName);
98
99
  }
99
100
  return _enumerations.get(enumerationName) as EnumerationDefinitionSchema;
100
101
  }
102
+
File without changes
@@ -1,24 +1,14 @@
1
1
  /**
2
2
  * @module node-opcua-factory
3
3
  */
4
- // tslint:disable:no-console
5
-
6
- import * as chalk from "chalk";
7
-
8
- import { assert } from "node-opcua-assert";
9
- import { checkDebugFlag, make_debugLog } from "node-opcua-debug";
10
4
  import { ExpandedNodeId, NodeId } from "node-opcua-nodeid";
11
5
 
12
6
  import { DataTypeFactory } from "./datatype_factory";
13
- import { ConstructorFuncWithSchema, ConstructorFunc } from "./constructor_type";
14
-
15
- import { BaseUAObject } from "./factories_baseobject";
16
- import { StructuredTypeSchema } from "./factories_structuredTypeSchema";
17
7
 
18
- const debugLog = make_debugLog(__filename);
19
- const doDebug = checkDebugFlag(__filename);
8
+ import { ConstructorFunc, ConstructorFuncWithSchema } from "./types";
20
9
 
21
10
  let globalFactory: DataTypeFactory;
11
+
22
12
  export function getStandardDataTypeFactory(): DataTypeFactory {
23
13
  if (!globalFactory) {
24
14
  globalFactory = new DataTypeFactory([]);
@@ -26,32 +16,24 @@ export function getStandardDataTypeFactory(): DataTypeFactory {
26
16
  }
27
17
  return globalFactory;
28
18
  }
19
+
29
20
  export function getStructureTypeConstructor(typeName: string): ConstructorFuncWithSchema {
30
21
  return getStandardDataTypeFactory().getStructureTypeConstructor(typeName);
31
22
  }
23
+
32
24
  export function hasStructuredType(typeName: string): boolean {
33
25
  return getStandardDataTypeFactory().hasStructuredType(typeName);
34
26
  }
35
- export function getStructuredTypeSchema(typeName: string): StructuredTypeSchema {
36
- return getStandardDataTypeFactory().getStructuredTypeSchema(typeName);
37
- }
38
27
 
39
28
  export function getConstructor(binaryEncodingNodeId: ExpandedNodeId): ConstructorFunc | null {
40
29
  return getStandardDataTypeFactory().getConstructor(binaryEncodingNodeId);
41
30
  }
31
+
42
32
  export function hasConstructor(binaryEncodingNodeId: ExpandedNodeId): boolean {
43
33
  return getStandardDataTypeFactory().hasConstructor(binaryEncodingNodeId);
44
34
  }
45
- export function constructObject(binaryEncodingNodeId: ExpandedNodeId): BaseUAObject {
46
- return getStandardDataTypeFactory().constructObject(binaryEncodingNodeId);
47
- }
48
- export function registerClassDefinition(
49
- dataTypeNodeId: NodeId,
50
- className: string,
51
- classConstructor: ConstructorFuncWithSchema
52
- ): void {
53
- return getStandardDataTypeFactory().registerClassDefinition(dataTypeNodeId, className, classConstructor);
54
- }
35
+
36
+
55
37
  /* istanbul ignore next */
56
38
  export function dump(): void {
57
39
  getStandardDataTypeFactory().dump();
@@ -0,0 +1,10 @@
1
+ import { ExpandedNodeId } from "node-opcua-nodeid";
2
+ import { getStandardDataTypeFactory } from "./get_standard_data_type_factory";
3
+ import { IBaseUAObject, IStructuredTypeSchema } from "./types";
4
+
5
+ export function getStructuredTypeSchema(typeName: string): IStructuredTypeSchema {
6
+ return getStandardDataTypeFactory().getStructuredTypeSchema(typeName);
7
+ }
8
+ export function constructObject(binaryEncodingNodeId: ExpandedNodeId): IBaseUAObject {
9
+ return getStandardDataTypeFactory().constructObject(binaryEncodingNodeId);
10
+ }
package/source/index.ts CHANGED
@@ -1,15 +1,18 @@
1
1
  /**
2
2
  * @module node-opcua-factory
3
3
  */
4
- export * from "./constructor_type";
4
+ export * from "./base_ua_object";
5
+ export * from "./basic_type";
6
+ export * from "./builtin_types_special";
7
+ export * from "./builtin_types";
5
8
  export * from "./datatype_factory";
6
- export * from "./factories_id_generator";
7
- export * from "./factories_enumerations";
8
- export * from "./factories_basic_type";
9
- export * from "./factories_builtin_types";
10
- export * from "./factories_builtin_types_special";
11
- export * from "./factories_baseobject";
12
- export * from "./types";
13
- export * from "./factories_schema_helpers";
14
- export * from "./factories_factories";
15
- export * from "./factories_structuredTypeSchema";
9
+ export * from "./enumerations";
10
+ export * from "./get_standard_data_type_factory";
11
+ export * from "./get_structured_type_schema";
12
+ export * from "./id_generator";
13
+ export * from "./nodeid_type";
14
+ export * from "./parameters";
15
+ export * from "./register_class_definition";
16
+ export * from "./schema_helpers";
17
+ export * from "./structured_type_schema";
18
+ export * from "./types";
@@ -0,0 +1,17 @@
1
+ import { registerEnumeration } from "./enumerations";
2
+
3
+
4
+
5
+ export enum NodeIdType {
6
+ ReferenceType = 0x01,
7
+ IsForward = 0x02,
8
+ NodeClass = 0x04,
9
+ BrowseName = 0x08,
10
+ DisplayName = 0x10,
11
+ TypeDefinition = 0x20
12
+ }
13
+ export const schemaNodeIdType = {
14
+ name: "NodeIdType",
15
+ enumValues: NodeIdType
16
+ };
17
+ export const _enumerationNodeIdType = registerEnumeration(schemaNodeIdType);
@@ -0,0 +1,3 @@
1
+ export const parameters = {
2
+ debugSchemaHelper: typeof process === "object" && !!process.env.DEBUG_CLASS
3
+ };
@@ -0,0 +1,11 @@
1
+ import { NodeId } from "node-opcua-nodeid";
2
+ import { getStandardDataTypeFactory } from "./get_standard_data_type_factory";
3
+ import { ConstructorFuncWithSchema } from "./types";
4
+
5
+ export function registerClassDefinition(
6
+ dataTypeNodeId: NodeId,
7
+ className: string,
8
+ classConstructor: ConstructorFuncWithSchema
9
+ ): void {
10
+ return getStandardDataTypeFactory().registerClassDefinition(dataTypeNodeId, className, classConstructor);
11
+ }
@@ -3,14 +3,13 @@
3
3
  */
4
4
  import { assert } from "node-opcua-assert";
5
5
  import { make_debugLog } from "node-opcua-debug";
6
- import { StructuredTypeSchema } from "./factories_structuredTypeSchema";
7
- import { CommonInterface, FieldCategory, FieldType, StructuredTypeField } from "./types";
6
+ import { DataTypeIds } from "node-opcua-constants";
7
+ import { DataTypeFactory } from "./datatype_factory";
8
+ import { BaseUAObject } from "./base_ua_object";
9
+ import { FieldCategory, FieldType, IStructuredTypeSchema, StructuredTypeField } from "./types";
8
10
 
9
11
  const debugLog = make_debugLog(__filename);
10
12
 
11
- export const parameters = {
12
- debugSchemaHelper: (typeof process === "object" && !!process.env.DEBUG_CLASS)
13
- };
14
13
 
15
14
  /**
16
15
  * ensure correctness of a schema object.
@@ -19,20 +18,27 @@ export const parameters = {
19
18
  * @param schema
20
19
  *
21
20
  */
22
- export function check_schema_correctness(schema: StructuredTypeSchema): void {
21
+ export function check_schema_correctness(schema: IStructuredTypeSchema): void {
23
22
  assert(typeof schema.name === "string", " expecting schema to have a name");
24
23
  assert(schema.fields instanceof Array, " expecting schema to provide a set of fields " + schema.name);
25
24
  assert(schema.baseType === undefined || typeof schema.baseType === "string");
26
25
  }
27
26
 
27
+
28
28
  /**
29
29
  * @method initialize_value
30
30
  * @param value
31
31
  * @param defaultValue
32
32
  * @return {*}
33
33
  */
34
- export function initialize_field(field: StructuredTypeField, value: unknown): any {
34
+ export function initialize_field(field: StructuredTypeField, value: unknown, factory?: DataTypeFactory): any {
35
35
  const _t = field.schema;
36
+
37
+ if (field.allowSubType && field.category === "complex") {
38
+ if (value instanceof BaseUAObject) {
39
+ value = { dataType: DataTypeIds.Structure, value };
40
+ }
41
+ }
36
42
  if (!(_t !== null && typeof _t === "object")) {
37
43
  throw new Error(
38
44
  "initialize_field: expecting field.schema to be set field.name = '" + field.name + "' type = " + field.fieldType
@@ -71,21 +77,6 @@ export function initialize_field(field: StructuredTypeField, value: unknown): an
71
77
  return value;
72
78
  }
73
79
 
74
- function initialize_value(value: any, defaultValue: any, _t: CommonInterface) {
75
- if (value === undefined) {
76
- return defaultValue;
77
- }
78
- if (defaultValue === null) {
79
- if (value === null) {
80
- return null;
81
- }
82
- }
83
- if (_t.coerce) {
84
- value = _t.coerce(value);
85
- return value;
86
- }
87
- return value;
88
- }
89
80
  /**
90
81
  * @method initialize_field_array
91
82
  * @param field
@@ -93,7 +84,7 @@ function initialize_value(value: any, defaultValue: any, _t: CommonInterface) {
93
84
  * @return
94
85
  */
95
86
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
96
- export function initialize_field_array(field: FieldType, valueArray: any): any {
87
+ export function initialize_field_array(field: FieldType, valueArray: any, factory?: DataTypeFactory): any {
97
88
  const _t = field.schema;
98
89
 
99
90
  let value;
@@ -104,24 +95,11 @@ export function initialize_field_array(field: FieldType, valueArray: any): any {
104
95
  if (!valueArray && field.defaultValue === null) {
105
96
  return null;
106
97
  }
107
-
108
98
  valueArray = valueArray || [];
109
-
110
- let defaultValue: any;
111
- if (_t.computer_default_value) {
112
- defaultValue = _t.computer_default_value(field.defaultValue);
113
- }
114
99
  const arr: unknown[] = [];
115
100
  for (i = 0; i < valueArray.length; i++) {
116
- value = initialize_value(valueArray[i], defaultValue, _t);
101
+ value = initialize_field(field, valueArray[i], factory);
117
102
  arr.push(value);
118
103
  }
119
- if (field.validate) {
120
- for (i = 0; i < arr.length; i++) {
121
- if (!field.validate(arr[i])) {
122
- throw Error(" invalid value " + arr[i] + " for field " + field.name + " of type " + field.fieldType);
123
- }
124
- }
125
- }
126
104
  return arr;
127
105
  }
@@ -8,13 +8,19 @@ import { BinaryStream } from "node-opcua-binary-stream";
8
8
  import { ExpandedNodeId, NodeId } from "node-opcua-nodeid";
9
9
  import { lowerFirstLetter } from "node-opcua-utils";
10
10
 
11
- import { getBuildInType, hasBuiltInType } from "./factories_builtin_types";
12
- import { getEnumeration, hasEnumeration } from "./factories_enumerations";
13
- import { getStructuredTypeSchema, getStructureTypeConstructor, hasStructuredType } from "./factories_factories";
14
- import { parameters } from "./factories_schema_helpers";
15
- import { DataTypeFactory } from "./datatype_factory";
16
- import { CommonInterface, FieldCategory, FieldInterfaceOptions, FieldType, StructuredTypeOptions, TypeSchemaBase } from "./types";
17
- import { BaseUAObject } from "./factories_baseobject";
11
+ import { getBuiltInType, hasBuiltInType, TypeSchemaBase } from "./builtin_types";
12
+ import { getBuiltInEnumeration, hasBuiltInEnumeration } from "./enumerations";
13
+ import { parameters } from "./parameters";
14
+ import { getStructureTypeConstructor, hasStructuredType } from "./get_standard_data_type_factory";
15
+ import { getStructuredTypeSchema } from "./get_structured_type_schema";
16
+ import {
17
+ CommonInterface,
18
+ FieldCategory,
19
+ FieldInterfaceOptions,
20
+ FieldType,
21
+ IStructuredTypeSchema,
22
+ StructuredTypeOptions
23
+ } from "./types";
18
24
 
19
25
  function figureOutFieldCategory(field: FieldInterfaceOptions): FieldCategory {
20
26
  const fieldType = field.fieldType;
@@ -23,7 +29,7 @@ function figureOutFieldCategory(field: FieldInterfaceOptions): FieldCategory {
23
29
  return field.category;
24
30
  }
25
31
 
26
- if (hasEnumeration(fieldType)) {
32
+ if (hasBuiltInEnumeration(fieldType)) {
27
33
  return FieldCategory.enumeration;
28
34
  } else if (hasBuiltInType(fieldType)) {
29
35
  return FieldCategory.basic;
@@ -65,12 +71,12 @@ function figureOutSchema(
65
71
  returnValue = getStructuredTypeSchema(fieldTypeWithoutNS);
66
72
  } else {
67
73
  // LocalizedText etc ...
68
- returnValue = getBuildInType(fieldTypeWithoutNS);
74
+ returnValue = getBuiltInType(fieldTypeWithoutNS);
69
75
  }
70
76
  break;
71
77
  case FieldCategory.basic:
72
- returnValue = getBuildInType(fieldTypeWithoutNS);
73
- if(!returnValue) {
78
+ returnValue = getBuiltInType(fieldTypeWithoutNS);
79
+ if (!returnValue) {
74
80
  returnValue = getStructuredTypeSchema(fieldTypeWithoutNS);
75
81
  if (returnValue) {
76
82
  console.log("Why ?");
@@ -78,14 +84,13 @@ function figureOutSchema(
78
84
  }
79
85
  break;
80
86
  case FieldCategory.enumeration:
81
- returnValue = getEnumeration(fieldTypeWithoutNS);
87
+ returnValue = getBuiltInEnumeration(fieldTypeWithoutNS);
82
88
  break;
83
89
  }
84
90
  if (null === returnValue || undefined === returnValue) {
85
91
  try {
86
- returnValue = getEnumeration(fieldTypeWithoutNS);
87
- }
88
- catch(err){
92
+ returnValue = getBuiltInEnumeration(fieldTypeWithoutNS);
93
+ } catch (err) {
89
94
  console.log(err);
90
95
  }
91
96
  throw new Error(
@@ -119,32 +124,32 @@ function buildField(underConstructSchema: StructuredTypeSchema, fieldLight: Fiel
119
124
  );
120
125
  }
121
126
 
127
+ const { defaultValue, isArray, documentation, fieldType, switchBit, switchValue, allowSubType, dataType, basicDataType } =
128
+ fieldLight;
122
129
  return {
123
130
  name: lowerFirstLetter(fieldLight.name),
124
-
131
+ originalName: fieldLight.name,
125
132
  category,
126
- defaultValue: fieldLight.defaultValue,
127
- isArray: fieldLight.isArray,
128
-
129
- documentation: fieldLight.documentation,
130
- fieldType: fieldLight.fieldType,
131
-
132
- switchBit: fieldLight.switchBit,
133
-
134
- switchValue: fieldLight.switchValue,
135
-
133
+ defaultValue,
134
+ isArray,
135
+ documentation,
136
+ fieldType,
137
+ switchBit,
138
+ switchValue,
139
+ allowSubType,
140
+ dataType,
141
+ basicDataType,
136
142
  schema
137
143
  };
138
144
  }
139
-
140
- export class StructuredTypeSchema extends TypeSchemaBase {
145
+ export class StructuredTypeSchema extends TypeSchemaBase implements IStructuredTypeSchema {
141
146
  public fields: FieldType[];
142
147
  public id: NodeId;
143
148
  public dataTypeNodeId: NodeId;
144
149
 
145
150
  public baseType: string;
146
151
  public _possibleFields: string[];
147
- public _baseSchema: StructuredTypeSchema | null;
152
+ public _baseSchema: IStructuredTypeSchema | null;
148
153
 
149
154
  public documentation?: string;
150
155
 
@@ -211,7 +216,7 @@ export class StructuredTypeSchema extends TypeSchemaBase {
211
216
  * @return {*}
212
217
  *
213
218
  */
214
- export function get_base_schema(schema: StructuredTypeSchema): StructuredTypeSchema | null {
219
+ export function get_base_schema(schema: IStructuredTypeSchema): IStructuredTypeSchema | null {
215
220
  let baseSchema = schema._baseSchema;
216
221
  if (baseSchema) {
217
222
  return baseSchema;
@@ -248,7 +253,7 @@ export function get_base_schema(schema: StructuredTypeSchema): StructuredTypeSch
248
253
  * (by walking up the inheritance chain)
249
254
  *
250
255
  */
251
- export function extract_all_fields(schema: StructuredTypeSchema): string[] {
256
+ export function extract_all_fields(schema: IStructuredTypeSchema): string[] {
252
257
  // returns cached result if any
253
258
  // istanbul ignore next
254
259
  if (schema._possibleFields) {
@@ -276,7 +281,7 @@ export function extract_all_fields(schema: StructuredTypeSchema): string[] {
276
281
  * @method check_options_correctness_against_schema
277
282
  *
278
283
  */
279
- export function check_options_correctness_against_schema(obj: any, schema: StructuredTypeSchema, options: any): boolean {
284
+ export function check_options_correctness_against_schema(obj: any, schema: IStructuredTypeSchema, options: any): boolean {
280
285
  if (!parameters.debugSchemaHelper) {
281
286
  return true; // ignoring set
282
287
  }
@@ -330,10 +335,6 @@ export function check_options_correctness_against_schema(obj: any, schema: Struc
330
335
  return true;
331
336
  }
332
337
 
333
- export function buildStructuredType2(dataTypeFactory: DataTypeFactory, schemaLight: StructuredTypeOptions): StructuredTypeSchema {
334
- return new StructuredTypeSchema(schemaLight);
335
- }
336
-
337
338
  export function buildStructuredType(schemaLight: StructuredTypeOptions): StructuredTypeSchema {
338
339
  return new StructuredTypeSchema(schemaLight);
339
340
  }