node-opcua-factory 2.51.0 → 2.55.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.
@@ -1,125 +1,125 @@
1
- /**
2
- * @module node-opcua-factory
3
- */
4
- import { assert } from "node-opcua-assert";
5
- import { make_debugLog } from "node-opcua-debug";
6
- import { CommonInterface, FieldCategory, FieldType, StructuredTypeField } from "./types";
7
-
8
- const debugLog = make_debugLog(__filename);
9
-
10
- export const parameters = {
11
- debugSchemaHelper: !!process.env.DEBUG_CLASS
12
- };
13
-
14
- /**
15
- * ensure correctness of a schema object.
16
- *
17
- * @method check_schema_correctness
18
- * @param schema
19
- *
20
- */
21
- export function check_schema_correctness(schema: any) {
22
- assert(typeof schema.name === "string", " expecting schema to have a name");
23
- assert(schema.fields instanceof Array, " expecting schema to provide a set of fields " + schema.name);
24
- assert(schema.baseType === undefined || typeof schema.baseType === "string");
25
- }
26
-
27
- /**
28
- * @method initialize_value
29
- * @param value
30
- * @param defaultValue
31
- * @return {*}
32
- */
33
- export function initialize_field(field: StructuredTypeField, value: any): any {
34
- const _t = field.schema;
35
- if (!(_t !== null && typeof _t === "object")) {
36
- throw new Error(
37
- "initialize_field: expecting field.schema to be set field.name = '" + field.name + "' type = " + field.fieldType
38
- );
39
- }
40
- if (field.category === FieldCategory.complex) {
41
- if (field.fieldTypeConstructor) {
42
- return new field.fieldTypeConstructor(value);
43
- } else {
44
- debugLog("xxxx => missing constructor for field type", field.fieldType);
45
- }
46
- }
47
-
48
- if (value === undefined || value === null) {
49
- const defaultValue = _t.computer_default_value ? _t.computer_default_value(field.defaultValue) : field.defaultValue;
50
- if (value === undefined) {
51
- if (_t.coerce) {
52
- return _t.coerce(defaultValue);
53
- }
54
- return defaultValue;
55
- }
56
- if (defaultValue === null) {
57
- if (value === null) {
58
- return null;
59
- }
60
- }
61
- }
62
- if (_t.coerce) {
63
- value = _t.coerce(value);
64
- }
65
- if (field.validate) {
66
- if (!field.validate(value)) {
67
- throw Error(" invalid value " + value + " for field " + field.name + " of type " + field.fieldType);
68
- }
69
- }
70
- return value;
71
- }
72
-
73
- function initialize_value(value: any, defaultValue: any, _t: CommonInterface) {
74
- if (value === undefined) {
75
- return defaultValue;
76
- }
77
- if (defaultValue === null) {
78
- if (value === null) {
79
- return null;
80
- }
81
- }
82
- if (_t.coerce) {
83
- value = _t.coerce(value);
84
- return value;
85
- }
86
- return value;
87
- }
88
- /**
89
- * @method initialize_field_array
90
- * @param field
91
- * @param valueArray
92
- * @return
93
- */
94
- export function initialize_field_array(field: FieldType, valueArray: any) {
95
- const _t = field.schema;
96
-
97
- let value;
98
- let i;
99
- assert(field !== null && typeof field === "object");
100
- assert(field.isArray);
101
-
102
- if (!valueArray && field.defaultValue === null) {
103
- return null;
104
- }
105
-
106
- valueArray = valueArray || [];
107
-
108
- let defaultValue: any;
109
- if (_t.computer_default_value) {
110
- defaultValue = _t.computer_default_value(field.defaultValue);
111
- }
112
- const arr = [];
113
- for (i = 0; i < valueArray.length; i++) {
114
- value = initialize_value(valueArray[i], defaultValue, _t);
115
- arr.push(value);
116
- }
117
- if (field.validate) {
118
- for (i = 0; i < arr.length; i++) {
119
- if (!field.validate(arr[i])) {
120
- throw Error(" invalid value " + arr[i] + " for field " + field.name + " of type " + field.fieldType);
121
- }
122
- }
123
- }
124
- return arr;
125
- }
1
+ /**
2
+ * @module node-opcua-factory
3
+ */
4
+ import { assert } from "node-opcua-assert";
5
+ import { make_debugLog } from "node-opcua-debug";
6
+ import { CommonInterface, FieldCategory, FieldType, StructuredTypeField } from "./types";
7
+
8
+ const debugLog = make_debugLog(__filename);
9
+
10
+ export const parameters = {
11
+ debugSchemaHelper: !!process.env.DEBUG_CLASS
12
+ };
13
+
14
+ /**
15
+ * ensure correctness of a schema object.
16
+ *
17
+ * @method check_schema_correctness
18
+ * @param schema
19
+ *
20
+ */
21
+ export function check_schema_correctness(schema: any) {
22
+ assert(typeof schema.name === "string", " expecting schema to have a name");
23
+ assert(schema.fields instanceof Array, " expecting schema to provide a set of fields " + schema.name);
24
+ assert(schema.baseType === undefined || typeof schema.baseType === "string");
25
+ }
26
+
27
+ /**
28
+ * @method initialize_value
29
+ * @param value
30
+ * @param defaultValue
31
+ * @return {*}
32
+ */
33
+ export function initialize_field(field: StructuredTypeField, value: any): any {
34
+ const _t = field.schema;
35
+ if (!(_t !== null && typeof _t === "object")) {
36
+ throw new Error(
37
+ "initialize_field: expecting field.schema to be set field.name = '" + field.name + "' type = " + field.fieldType
38
+ );
39
+ }
40
+ if (field.category === FieldCategory.complex) {
41
+ if (field.fieldTypeConstructor) {
42
+ return new field.fieldTypeConstructor(value);
43
+ } else {
44
+ debugLog("xxxx => missing constructor for field type", field.fieldType);
45
+ }
46
+ }
47
+
48
+ if (value === undefined || value === null) {
49
+ const defaultValue = _t.computer_default_value ? _t.computer_default_value(field.defaultValue) : field.defaultValue;
50
+ if (value === undefined) {
51
+ if (_t.coerce) {
52
+ return _t.coerce(defaultValue);
53
+ }
54
+ return defaultValue;
55
+ }
56
+ if (defaultValue === null) {
57
+ if (value === null) {
58
+ return null;
59
+ }
60
+ }
61
+ }
62
+ if (_t.coerce) {
63
+ value = _t.coerce(value);
64
+ }
65
+ if (field.validate) {
66
+ if (!field.validate(value)) {
67
+ throw Error(" invalid value " + value + " for field " + field.name + " of type " + field.fieldType);
68
+ }
69
+ }
70
+ return value;
71
+ }
72
+
73
+ function initialize_value(value: any, defaultValue: any, _t: CommonInterface) {
74
+ if (value === undefined) {
75
+ return defaultValue;
76
+ }
77
+ if (defaultValue === null) {
78
+ if (value === null) {
79
+ return null;
80
+ }
81
+ }
82
+ if (_t.coerce) {
83
+ value = _t.coerce(value);
84
+ return value;
85
+ }
86
+ return value;
87
+ }
88
+ /**
89
+ * @method initialize_field_array
90
+ * @param field
91
+ * @param valueArray
92
+ * @return
93
+ */
94
+ export function initialize_field_array(field: FieldType, valueArray: any) {
95
+ const _t = field.schema;
96
+
97
+ let value;
98
+ let i;
99
+ assert(field !== null && typeof field === "object");
100
+ assert(field.isArray);
101
+
102
+ if (!valueArray && field.defaultValue === null) {
103
+ return null;
104
+ }
105
+
106
+ valueArray = valueArray || [];
107
+
108
+ let defaultValue: any;
109
+ if (_t.computer_default_value) {
110
+ defaultValue = _t.computer_default_value(field.defaultValue);
111
+ }
112
+ const arr = [];
113
+ for (i = 0; i < valueArray.length; i++) {
114
+ value = initialize_value(valueArray[i], defaultValue, _t);
115
+ arr.push(value);
116
+ }
117
+ if (field.validate) {
118
+ for (i = 0; i < arr.length; i++) {
119
+ if (!field.validate(arr[i])) {
120
+ throw Error(" invalid value " + arr[i] + " for field " + field.name + " of type " + field.fieldType);
121
+ }
122
+ }
123
+ }
124
+ return arr;
125
+ }
@@ -2,7 +2,6 @@
2
2
  * @module node-opcua-factory
3
3
  */
4
4
  import * as chalk from "chalk";
5
- import { CommonInterface, FieldCategory, FieldInterfaceOptions, FieldType, StructuredTypeOptions, TypeSchemaBase } from "./types";
6
5
 
7
6
  import { assert } from "node-opcua-assert";
8
7
  import { BinaryStream } from "node-opcua-binary-stream";
@@ -14,6 +13,7 @@ import { getEnumeration, hasEnumeration } from "./factories_enumerations";
14
13
  import { getStructuredTypeSchema, getStructureTypeConstructor, hasStructuredType } from "./factories_factories";
15
14
  import { parameters } from "./factories_schema_helpers";
16
15
  import { DataTypeFactory } from "./datatype_factory";
16
+ import { CommonInterface, FieldCategory, FieldInterfaceOptions, FieldType, StructuredTypeOptions, TypeSchemaBase } from "./types";
17
17
 
18
18
  function figureOutFieldCategory(field: FieldInterfaceOptions): FieldCategory {
19
19
  const fieldType = field.fieldType;
@@ -165,7 +165,7 @@ export class StructuredTypeSchema extends TypeSchemaBase {
165
165
  this._possibleFields = this.fields.map((field) => field.name);
166
166
  this._baseSchema = null;
167
167
  }
168
- public toString() {
168
+ public toString(): string {
169
169
  const str: string[] = [];
170
170
  str.push("name = " + this.name);
171
171
  str.push("baseType = " + this.baseType);
@@ -199,7 +199,7 @@ export class StructuredTypeSchema extends TypeSchemaBase {
199
199
  * @return {*}
200
200
  *
201
201
  */
202
- export function get_base_schema(schema: StructuredTypeSchema) {
202
+ export function get_base_schema(schema: StructuredTypeSchema): StructuredTypeSchema | null {
203
203
  let baseSchema = schema._baseSchema;
204
204
  if (baseSchema) {
205
205
  return baseSchema;
@@ -234,10 +234,9 @@ export function get_base_schema(schema: StructuredTypeSchema) {
234
234
  /**
235
235
  * extract a list of all possible fields for a schema
236
236
  * (by walking up the inheritance chain)
237
- * @method extract_all_fields
238
237
  *
239
238
  */
240
- export function extract_all_fields(schema: StructuredTypeSchema) {
239
+ export function extract_all_fields(schema: StructuredTypeSchema): string[] {
241
240
  // returns cached result if any
242
241
  // istanbul ignore next
243
242
  if (schema._possibleFields) {
package/source/index.ts CHANGED
@@ -1,15 +1,15 @@
1
- /**
2
- * @module node-opcua-factory
3
- */
4
- export * from "./constructor_type";
5
- 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";
1
+ /**
2
+ * @module node-opcua-factory
3
+ */
4
+ export * from "./constructor_type";
5
+ 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";
package/source/types.ts CHANGED
@@ -125,12 +125,6 @@ export interface EnumerationDefinition extends CommonInterface {
125
125
 
126
126
  export type TypeDefinition = BuiltInTypeDefinition | EnumerationDefinition | BasicTypeDefinition | TypeSchemaBase;
127
127
 
128
- // tslint:disable-next-line:no-empty
129
- function defaultEncode(value: any, stream: BinaryStream): void {}
130
-
131
- // tslint:disable-next-line:no-empty
132
- function defaultDecode(stream: BinaryStream): void {}
133
-
134
128
  /**
135
129
  * @class TypeSchemaBase
136
130
  * @param options {Object}
@@ -166,7 +160,7 @@ export class TypeSchemaBase implements CommonInterface {
166
160
  * @param defaultValue {*} the default value
167
161
  * @return {*}
168
162
  */
169
- public computer_default_value(defaultValue: any): any {
163
+ public computer_default_value(defaultValue: unknown): any {
170
164
  if (defaultValue === undefined) {
171
165
  defaultValue = this.defaultValue;
172
166
  }