node-opcua-schemas
Advanced tools
Comparing version 2.1.2 to 2.1.3
@@ -14,4 +14,4 @@ "use strict"; | ||
function getOrCreateConstructor(fieldType, typeDictionary, encodingDefaultBinary, encodingDefaultXml) { | ||
if (node_opcua_factory_1.hasStructuredType(fieldType)) { | ||
return node_opcua_factory_1.getStructureTypeConstructor(fieldType); | ||
if (typeDictionary.hasStructuredType(fieldType)) { | ||
return typeDictionary.getStructureTypeConstructor(fieldType); | ||
} | ||
@@ -24,2 +24,11 @@ const schema = typeDictionary.structuredTypes[fieldType]; | ||
const constructor = createDynamicObjectConstructor(schema, typeDictionary); | ||
if (!constructor) { | ||
return constructor; | ||
} | ||
// istanbul ignore next | ||
if (!typeDictionary.hasStructuredType(fieldType)) { | ||
typeDictionary.registerFactory(fieldType, constructor); | ||
return constructor; | ||
// hrow new Error("constructor should now be registered - " + fieldType); | ||
} | ||
if (encodingDefaultBinary && encodingDefaultBinary.value !== 0) { | ||
@@ -30,15 +39,4 @@ schema.encodingDefaultBinary = encodingDefaultBinary; | ||
constructor.encodingDefaultXml = encodingDefaultXml; | ||
// istanbul ignore next | ||
if (doDebug) { | ||
debugLog("registering class definition , ", fieldType, encodingDefaultBinary.toString()); | ||
} | ||
node_opcua_factory_1.registerClassDefinition(fieldType, constructor); | ||
typeDictionary.associateWithBinaryEncoding(fieldType, encodingDefaultBinary); | ||
} | ||
else { | ||
// istanbul ignore next | ||
if (doDebug) { | ||
debugLog("registering factory , ", fieldType); | ||
} | ||
node_opcua_factory_1.registerFactory(fieldType, constructor); | ||
} | ||
return constructor; | ||
@@ -80,3 +78,3 @@ } | ||
} | ||
function decodeArrayOrElement(field, obj, stream, decodeFunc) { | ||
function decodeArrayOrElement(typeDictionary, field, obj, stream, decodeFunc) { | ||
if (field.isArray) { | ||
@@ -95,3 +93,3 @@ const array = []; | ||
// construct an instance | ||
const constructor = node_opcua_factory_1.getStructureTypeConstructor(field.fieldType); | ||
const constructor = typeDictionary.getStructureTypeConstructor(field.fieldType); | ||
const element = new constructor({}); | ||
@@ -110,2 +108,5 @@ element.decode(stream); | ||
else { | ||
if (!obj[field.name]) { | ||
throw new Error(" cannot find field with name " + field.name); | ||
} | ||
obj[field.name].decode(stream); | ||
@@ -115,2 +116,27 @@ } | ||
} | ||
function initializeField(field, thisAny, options, schema, typeDictionary) { | ||
const name = field.name; | ||
switch (field.category) { | ||
case node_opcua_factory_1.FieldCategory.complex: { | ||
const constuctor = getOrCreateConstructor(field.fieldType, typeDictionary) || node_opcua_factory_1.BaseUAObject; | ||
if (field.isArray) { | ||
(thisAny)[name] = (options[name] || []).map((x) => constuctor ? new constuctor(x) : null); | ||
} | ||
else { | ||
(thisAny)[name] = constuctor ? new constuctor(options[name]) : null; | ||
} | ||
// xx processStructuredType(fieldSchema); | ||
break; | ||
} | ||
case node_opcua_factory_1.FieldCategory.enumeration: | ||
case node_opcua_factory_1.FieldCategory.basic: | ||
if (field.isArray) { | ||
(thisAny)[name] = node_opcua_factory_1.initialize_field_array(field, options[name]); | ||
} | ||
else { | ||
(thisAny)[name] = node_opcua_factory_1.initialize_field(field, options[name]); | ||
} | ||
break; | ||
} | ||
} | ||
/** | ||
@@ -136,24 +162,3 @@ * @private | ||
} | ||
switch (field.category) { | ||
case node_opcua_factory_1.FieldCategory.complex: { | ||
const constuctor = getOrCreateConstructor(field.fieldType, typeDictionary) || node_opcua_factory_1.BaseUAObject; | ||
if (field.isArray) { | ||
(thisAny)[name] = (options[name] || []).map((x) => constuctor ? new constuctor(x) : null); | ||
} | ||
else { | ||
(thisAny)[name] = constuctor ? new constuctor(options[name]) : null; | ||
} | ||
// xx processStructuredType(fieldSchema); | ||
break; | ||
} | ||
case node_opcua_factory_1.FieldCategory.enumeration: | ||
case node_opcua_factory_1.FieldCategory.basic: | ||
if (field.isArray) { | ||
(thisAny)[name] = node_opcua_factory_1.initialize_field_array(field, options[name]); | ||
} | ||
else { | ||
(thisAny)[name] = node_opcua_factory_1.initialize_field(field, options[name]); | ||
} | ||
break; | ||
} | ||
initializeField(field, thisAny, options, schema, typeDictionary); | ||
} | ||
@@ -201,6 +206,6 @@ } | ||
} | ||
function decodeFields(thisAny, schema, stream) { | ||
function decodeFields(thisAny, schema, stream, typeDictionary) { | ||
// encodeFields base class first | ||
if (schema._baseSchema && schema._baseSchema.fields.length) { | ||
decodeFields(thisAny, schema._baseSchema, stream); | ||
decodeFields(thisAny, schema._baseSchema, stream, typeDictionary); | ||
} | ||
@@ -220,10 +225,16 @@ // ============ Deal with switchBits | ||
} | ||
else { | ||
if (field.category === node_opcua_factory_1.FieldCategory.complex && (thisAny)[field.name] === undefined) { | ||
// need to create empty structure for deserialisation | ||
initializeField(field, thisAny, {}, schema, typeDictionary); | ||
} | ||
} | ||
} | ||
switch (field.category) { | ||
case node_opcua_factory_1.FieldCategory.complex: | ||
decodeArrayOrElement(field, thisAny, stream); | ||
decodeArrayOrElement(typeDictionary, field, thisAny, stream); | ||
break; | ||
case node_opcua_factory_1.FieldCategory.enumeration: | ||
case node_opcua_factory_1.FieldCategory.basic: | ||
decodeArrayOrElement(field, thisAny, stream, field.schema.decode); | ||
decodeArrayOrElement(typeDictionary, field, thisAny, stream, field.schema.decode); | ||
break; | ||
@@ -243,2 +254,3 @@ default: | ||
this.__schema = schema; | ||
this._typeDictionary = typeDictionary; | ||
node_opcua_factory_1.check_options_correctness_against_schema(this, this.schema, options); | ||
@@ -253,3 +265,3 @@ initializeFields(this, options, this.schema, typeDictionary); | ||
super.decode(stream); | ||
decodeFields(this, this.schema, stream); | ||
decodeFields(this, this.schema, stream, this._typeDictionary); | ||
} | ||
@@ -277,22 +289,35 @@ get schema() { | ||
if (field.switchValue === undefined) { | ||
// this is the switch value field | ||
switchFieldName = field.name; | ||
// this is the switch value field | ||
continue; | ||
} | ||
node_opcua_assert_1.assert(switchFieldName.length > 0); | ||
node_opcua_assert_1.assert(field.switchValue !== undefined, "union must only have field with switchedValue"); | ||
node_opcua_assert_1.assert(switchFieldName.length > 0, "It seems that there is no switch field in union schema"); | ||
node_opcua_assert_1.assert(field.switchValue !== undefined, "union schema must only have one switched value field"); | ||
// dealing with optional fields | ||
/* istanbul ignore next */ | ||
if (uniqueFieldHasBeenFound && options[field.name] !== undefined) { | ||
throw new Error("union must have only one choice"); | ||
// let try to be helpful for the developper by providing some hint | ||
debugLog(this.schema); | ||
throw new Error("union must have only one choice in " + JSON.stringify(options) + | ||
"\n found while investigating " + field.name + | ||
"\n switchFieldName = " + switchFieldName); | ||
} | ||
if (options[field.name] === undefined) { | ||
continue; | ||
} | ||
uniqueFieldHasBeenFound = true; | ||
//Xx // the field can be ignore | ||
//Xx if (options[field.name] === undefined) { | ||
//Xx continue; | ||
//Xx } | ||
if (options[switchFieldName] !== undefined) { | ||
// then options[switchFieldName] must equql | ||
// then options[switchFieldName] must equal | ||
if (options[switchFieldName] !== field.switchValue) { | ||
throw new Error("Invalid " + switchFieldName + " value : expecting " + field.switchValue); | ||
continue; | ||
//xx throw new Error("Invalid " + switchFieldName + " value : expecting " + field.switchValue); | ||
} | ||
} | ||
else { | ||
// the is no switchFieldName , in this case the i | ||
if (options[name] === undefined) { | ||
continue; | ||
} | ||
} | ||
uniqueFieldHasBeenFound = true; | ||
this[switchFieldName] = field.switchValue; | ||
@@ -328,3 +353,11 @@ switch (field.category) { | ||
const r = schema.fields.filter((f) => f.switchValue !== undefined).map((f) => f.name).join(" , "); | ||
throw new Error(" At least one of " + r + " must be specified"); | ||
// it is possible also that the switchfield value do not correspond to a valid field | ||
const foundFieldForSwitchValue = schema.fields.findIndex((f) => f.switchValue !== undefined && f.switchValue === options[switchFieldName]); | ||
if (foundFieldForSwitchValue) { | ||
//xx throw new Error(this.schema.name + ": cannot find field with value " + options[switchFieldName]); | ||
} | ||
else { | ||
console.log(this.schema); | ||
throw new Error(this.schema.name + ": At least one of [ " + r + " ] must be specified in " + JSON.stringify(options)); | ||
} | ||
} | ||
@@ -359,2 +392,3 @@ } | ||
decode(stream) { | ||
const typeDictionary = this.schema.$typeDictionary; | ||
const switchValue = stream.readUInt32(); | ||
@@ -369,7 +403,7 @@ const switchFieldName = this.schema.fields[0].name; | ||
case node_opcua_factory_1.FieldCategory.complex: | ||
decodeArrayOrElement(field, this, stream); | ||
decodeArrayOrElement(typeDictionary, field, this, stream); | ||
break; | ||
case node_opcua_factory_1.FieldCategory.enumeration: | ||
case node_opcua_factory_1.FieldCategory.basic: | ||
decodeArrayOrElement(field, this, stream, field.schema.decode); | ||
decodeArrayOrElement(typeDictionary, field, this, stream, field.schema.decode); | ||
break; | ||
@@ -474,2 +508,3 @@ default: | ||
schemaPriv.$Constructor = EXTENSION; | ||
typeDictionary.registerFactory(schema.name, EXTENSION); | ||
return EXTENSION; | ||
@@ -476,0 +511,0 @@ } |
@@ -5,2 +5,3 @@ /** | ||
import { EnumerationDefinitionSchema, StructuredTypeSchema } from "node-opcua-factory"; | ||
import { DataTypeFactory } from "node-opcua-factory"; | ||
export interface EnumeratedType { | ||
@@ -18,4 +19,3 @@ name: string; | ||
} | ||
export interface TypeDictionary { | ||
defaultByteOrder: "LittleEndian"; | ||
export interface ITypeDictionary { | ||
targetNamespace: string; | ||
@@ -36,2 +36,17 @@ imports: string[]; | ||
} | ||
export declare function parseBinaryXSD(xmlString: string, callback: (err: Error | null, typeDictionary: TypeDictionary) => void): void; | ||
export declare class TypeDictionary extends DataTypeFactory implements ITypeDictionary { | ||
structuredTypes: { | ||
[key: string]: StructuredTypeSchema; | ||
}; | ||
enumeratedTypes: { | ||
[key: string]: EnumerationDefinitionSchema; | ||
}; | ||
structuredTypesRaw: { | ||
[key: string]: StructureTypeRaw; | ||
}; | ||
enumeratedTypesRaw: { | ||
[key: string]: EnumeratedType; | ||
}; | ||
constructor(baseDataFactories: DataTypeFactory[]); | ||
} | ||
export declare function parseBinaryXSD(xmlString: string, dataTypeFactories: DataTypeFactory[], callback: (err: Error | null, typeDictionary: TypeDictionary) => void): void; |
@@ -13,2 +13,3 @@ "use strict"; | ||
const tools_1 = require("./tools"); | ||
const node_opcua_factory_1 = require("node-opcua-factory"); | ||
const doDebug = node_opcua_debug_1.checkDebugFlag(__filename); | ||
@@ -67,2 +68,12 @@ function w(s, l) { | ||
} | ||
class TypeDictionary extends node_opcua_factory_1.DataTypeFactory { | ||
constructor(baseDataFactories) { | ||
super(baseDataFactories); | ||
this.structuredTypes = {}; | ||
this.structuredTypesRaw = {}; | ||
this.enumeratedTypes = {}; | ||
this.enumeratedTypesRaw = {}; | ||
} | ||
} | ||
exports.TypeDictionary = TypeDictionary; | ||
/* tslint:disable:object-literal-shorthand */ | ||
@@ -209,12 +220,4 @@ const state0 = { | ||
}; | ||
function parseBinaryXSD(xmlString, callback) { | ||
const typeDictionary = { | ||
defaultByteOrder: "LittleEndian", | ||
targetNamespace: "", | ||
imports: [], | ||
structuredTypes: {}, | ||
structuredTypesRaw: {}, | ||
enumeratedTypes: {}, | ||
enumeratedTypesRaw: {} | ||
}; | ||
function parseBinaryXSD(xmlString, dataTypeFactories, callback) { | ||
const typeDictionary = new TypeDictionary(dataTypeFactories); | ||
const parser = new node_opcua_xml2json_1.Xml2Json(state0); | ||
@@ -221,0 +224,0 @@ parser.typeDictionary = typeDictionary; |
{ | ||
"name": "node-opcua-schemas", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"description": "pure nodejs OPCUA SDK - module -schemas", | ||
@@ -14,13 +14,13 @@ "main": "dist/source/index.js", | ||
"node-opcua-binary-stream": "^2.1.0", | ||
"node-opcua-data-model": "^2.1.2", | ||
"node-opcua-data-model": "^2.1.3", | ||
"node-opcua-debug": "^2.1.0", | ||
"node-opcua-enum": "^2.1.0", | ||
"node-opcua-extension-object": "^2.1.2", | ||
"node-opcua-factory": "^2.1.2", | ||
"node-opcua-nodeid": "^2.1.0", | ||
"node-opcua-variant": "^2.1.2", | ||
"node-opcua-xml2json": "^2.1.2" | ||
"node-opcua-extension-object": "^2.1.3", | ||
"node-opcua-factory": "^2.1.3", | ||
"node-opcua-nodeid": "^2.1.3", | ||
"node-opcua-variant": "^2.1.3", | ||
"node-opcua-xml2json": "^2.1.3" | ||
}, | ||
"devDependencies": { | ||
"node-opcua-packet-analyzer": "^2.1.2" | ||
"node-opcua-packet-analyzer": "^2.1.3" | ||
}, | ||
@@ -42,3 +42,3 @@ "author": "Etienne Rossignon", | ||
"homepage": "http://node-opcua.github.io/", | ||
"gitHead": "46c4676f5623126ee95c60c25bcdedd8091ba342" | ||
"gitHead": "a47d959488b88d80c6d4d339d08c80ccc5490ffd" | ||
} |
@@ -13,8 +13,8 @@ /** | ||
FieldCategory, | ||
FieldType, getStructureTypeConstructor, hasStructuredType, | ||
FieldType, | ||
initialize_field, | ||
initialize_field_array, | ||
registerClassDefinition, registerFactory, | ||
StructuredTypeSchema | ||
} from "node-opcua-factory"; | ||
import { ExpandedNodeId, NodeIdType } from "node-opcua-nodeid"; | ||
@@ -33,4 +33,4 @@ import { TypeDictionary } from "./parse_binary_xsd"; | ||
if (hasStructuredType(fieldType)) { | ||
return getStructureTypeConstructor(fieldType); | ||
if (typeDictionary.hasStructuredType(fieldType)) { | ||
return typeDictionary.getStructureTypeConstructor(fieldType); | ||
} | ||
@@ -45,3 +45,14 @@ const schema = typeDictionary.structuredTypes[fieldType]; | ||
const constructor = createDynamicObjectConstructor(schema, typeDictionary); | ||
if (!constructor) { | ||
return constructor; | ||
} | ||
// istanbul ignore next | ||
if(!typeDictionary.hasStructuredType(fieldType)) { | ||
typeDictionary.registerFactory(fieldType, constructor as ConstructorFuncWithSchema); | ||
return constructor; | ||
// hrow new Error("constructor should now be registered - " + fieldType); | ||
} | ||
if (encodingDefaultBinary && encodingDefaultBinary.value !== 0) { | ||
@@ -52,15 +63,3 @@ schema.encodingDefaultBinary = encodingDefaultBinary; | ||
(constructor as any).encodingDefaultXml = encodingDefaultXml; | ||
// istanbul ignore next | ||
if (doDebug) { | ||
debugLog("registering class definition , ", fieldType, encodingDefaultBinary.toString()); | ||
} | ||
registerClassDefinition(fieldType, constructor as ConstructorFuncWithSchema); | ||
} else { | ||
// istanbul ignore next | ||
if (doDebug) { | ||
debugLog("registering factory , ", fieldType); | ||
} | ||
registerFactory(fieldType, constructor as ConstructorFuncWithSchema); | ||
typeDictionary.associateWithBinaryEncoding(fieldType,encodingDefaultBinary); | ||
} | ||
@@ -71,2 +70,3 @@ return constructor; | ||
function encodeArrayOrElement( | ||
field: FieldType, | ||
@@ -107,2 +107,3 @@ obj: any, | ||
function decodeArrayOrElement( | ||
typeDictionary: TypeDictionary, | ||
field: FieldType, | ||
@@ -123,4 +124,5 @@ obj: any, | ||
} else { | ||
// construct an instance | ||
const constructor = getStructureTypeConstructor(field.fieldType); | ||
const constructor = typeDictionary.getStructureTypeConstructor(field.fieldType); | ||
const element = new constructor({}); | ||
@@ -137,2 +139,5 @@ element.decode(stream); | ||
} else { | ||
if (!obj[field.name]) { | ||
throw new Error(" cannot find field with name " + field.name); | ||
} | ||
obj[field.name].decode(stream); | ||
@@ -143,2 +148,36 @@ } | ||
function initializeField( | ||
field: FieldType, | ||
thisAny: any, | ||
options: any, | ||
schema: StructuredTypeSchema, | ||
typeDictionary: TypeDictionary | ||
) { | ||
const name = field.name; | ||
switch (field.category) { | ||
case FieldCategory.complex: { | ||
const constuctor = getOrCreateConstructor(field.fieldType, typeDictionary) || BaseUAObject; | ||
if (field.isArray) { | ||
(thisAny)[name] = (options[name] || []).map((x: any) => | ||
constuctor ? new constuctor(x) : null | ||
); | ||
} else { | ||
(thisAny)[name] = constuctor ? new constuctor(options[name]) : null; | ||
} | ||
// xx processStructuredType(fieldSchema); | ||
break; | ||
} | ||
case FieldCategory.enumeration: | ||
case FieldCategory.basic: | ||
if (field.isArray) { | ||
(thisAny)[name] = initialize_field_array(field, options[name]); | ||
} else { | ||
(thisAny)[name] = initialize_field(field, options[name]); | ||
} | ||
break; | ||
} | ||
} | ||
/** | ||
@@ -153,2 +192,3 @@ * @private | ||
// initialize base class first | ||
@@ -168,25 +208,3 @@ if (schema._baseSchema && schema._baseSchema.fields.length) { | ||
} | ||
switch (field.category) { | ||
case FieldCategory.complex: { | ||
const constuctor = getOrCreateConstructor(field.fieldType, typeDictionary) || BaseUAObject; | ||
if (field.isArray) { | ||
(thisAny)[name] = (options[name] || []).map((x: any) => | ||
constuctor ? new constuctor(x) : null | ||
); | ||
} else { | ||
(thisAny)[name] = constuctor ? new constuctor(options[name]) : null; | ||
} | ||
// xx processStructuredType(fieldSchema); | ||
break; | ||
} | ||
case FieldCategory.enumeration: | ||
case FieldCategory.basic: | ||
if (field.isArray) { | ||
(thisAny)[name] = initialize_field_array(field, options[name]); | ||
} else { | ||
(thisAny)[name] = initialize_field(field, options[name]); | ||
} | ||
break; | ||
} | ||
initializeField(field, thisAny, options, schema, typeDictionary); | ||
} | ||
@@ -244,7 +262,12 @@ | ||
} | ||
function decodeFields(thisAny: any, schema: StructuredTypeSchema, stream: BinaryStream) { | ||
function decodeFields( | ||
thisAny: any, | ||
schema: StructuredTypeSchema, | ||
stream: BinaryStream, | ||
typeDictionary: TypeDictionary | ||
) { | ||
// encodeFields base class first | ||
if (schema._baseSchema && schema._baseSchema.fields.length) { | ||
decodeFields(thisAny, schema._baseSchema!, stream); | ||
decodeFields(thisAny, schema._baseSchema!, stream, typeDictionary); | ||
} | ||
@@ -262,3 +285,2 @@ | ||
if (field.switchBit !== undefined) { | ||
// tslint:disable-next-line:no-bitwise | ||
@@ -268,2 +290,7 @@ if ((bitField & (1 << field.switchBit)) === 0) { | ||
continue; | ||
} else { | ||
if (field.category === FieldCategory.complex && (thisAny)[field.name] === undefined) { | ||
// need to create empty structure for deserialisation | ||
initializeField(field, thisAny, {}, schema, typeDictionary); | ||
} | ||
} | ||
@@ -274,7 +301,7 @@ } | ||
case FieldCategory.complex: | ||
decodeArrayOrElement(field, thisAny, stream); | ||
decodeArrayOrElement(typeDictionary, field, thisAny, stream); | ||
break; | ||
case FieldCategory.enumeration: | ||
case FieldCategory.basic: | ||
decodeArrayOrElement(field, thisAny, stream, field.schema.decode); | ||
decodeArrayOrElement(typeDictionary, field, thisAny, stream, field.schema.decode); | ||
break; | ||
@@ -292,2 +319,3 @@ default: | ||
public static possibleFields: string[] = []; | ||
private readonly _typeDictionary: TypeDictionary; | ||
private __schema?: StructuredTypeSchema; | ||
@@ -303,2 +331,4 @@ | ||
this._typeDictionary = typeDictionary; | ||
check_options_correctness_against_schema(this, this.schema, options); | ||
@@ -316,3 +346,3 @@ | ||
super.decode(stream); | ||
decodeFields(this as any, this.schema, stream); | ||
decodeFields(this as any, this.schema, stream, this._typeDictionary); | ||
} | ||
@@ -341,3 +371,2 @@ | ||
constructor(options: any, schema: StructuredTypeSchema, typeDictionary: TypeDictionary) { | ||
super(); | ||
@@ -358,28 +387,38 @@ | ||
const name = field.name; | ||
if (field.switchValue === undefined) { | ||
// this is the switch value field | ||
switchFieldName = field.name; | ||
// this is the switch value field | ||
continue; | ||
} | ||
assert(switchFieldName.length > 0); | ||
assert(field.switchValue !== undefined, "union must only have field with switchedValue"); | ||
assert(switchFieldName.length > 0, "It seems that there is no switch field in union schema"); | ||
assert(field.switchValue !== undefined, "union schema must only have one switched value field"); | ||
// dealing with optional fields | ||
if (uniqueFieldHasBeenFound && options[field.name] !== undefined) { | ||
throw new Error("union must have only one choice"); | ||
} | ||
if (options[field.name] === undefined) { | ||
continue; | ||
/* istanbul ignore next */ | ||
if (uniqueFieldHasBeenFound && options[field.name] !== undefined) { | ||
// let try to be helpful for the developper by providing some hint | ||
debugLog(this.schema); | ||
throw new Error("union must have only one choice in "+ JSON.stringify(options) + | ||
"\n found while investigating " + field.name + | ||
"\n switchFieldName = " + switchFieldName); | ||
} | ||
uniqueFieldHasBeenFound = true; | ||
//Xx // the field can be ignore | ||
//Xx if (options[field.name] === undefined) { | ||
//Xx continue; | ||
//Xx } | ||
if (options[switchFieldName] !== undefined) { | ||
// then options[switchFieldName] must equql | ||
// then options[switchFieldName] must equal | ||
if (options[switchFieldName] !== field.switchValue) { | ||
throw new Error("Invalid " + switchFieldName + " value : expecting " + field.switchValue); | ||
continue; | ||
//xx throw new Error("Invalid " + switchFieldName + " value : expecting " + field.switchValue); | ||
} | ||
} else { | ||
// the is no switchFieldName , in this case the i | ||
if (options[name] === undefined) { | ||
continue; | ||
} | ||
} | ||
uniqueFieldHasBeenFound = true; | ||
(this as any)[switchFieldName] = field.switchValue; | ||
@@ -417,3 +456,11 @@ | ||
const r = schema.fields.filter((f) => f.switchValue !== undefined).map((f) => f.name).join(" , "); | ||
throw new Error(" At least one of " + r + " must be specified"); | ||
// it is possible also that the switchfield value do not correspond to a valid field | ||
const foundFieldForSwitchValue = schema.fields.findIndex((f) => | ||
f.switchValue !== undefined && f.switchValue === options[switchFieldName]); | ||
if (foundFieldForSwitchValue) { | ||
//xx throw new Error(this.schema.name + ": cannot find field with value " + options[switchFieldName]); | ||
} else { | ||
console.log(this.schema); | ||
throw new Error(this.schema.name + ": At least one of [ " + r + " ] must be specified in " + JSON.stringify(options)); | ||
} | ||
} | ||
@@ -453,2 +500,4 @@ } | ||
const typeDictionary : TypeDictionary = (this.schema as any).$typeDictionary; | ||
const switchValue = stream.readUInt32(); | ||
@@ -467,7 +516,7 @@ const switchFieldName = this.schema.fields[0].name; | ||
case FieldCategory.complex: | ||
decodeArrayOrElement(field, this as any, stream); | ||
decodeArrayOrElement(typeDictionary, field, this as any, stream); | ||
break; | ||
case FieldCategory.enumeration: | ||
case FieldCategory.basic: | ||
decodeArrayOrElement(field, this as any, stream, field.schema.decode); | ||
decodeArrayOrElement(typeDictionary, field, this as any, stream, field.schema.decode); | ||
break; | ||
@@ -606,3 +655,5 @@ default: | ||
typeDictionary.registerFactory(schema.name, EXTENSION as any); | ||
return EXTENSION; | ||
} |
@@ -10,6 +10,8 @@ /** | ||
import { | ||
EnumerationDefinitionSchema, FieldInterfaceOptions, | ||
EnumerationDefinitionSchema, | ||
FieldInterfaceOptions, | ||
StructuredTypeField, | ||
StructuredTypeOptions, | ||
StructuredTypeSchema | ||
StructuredTypeSchema, | ||
ConstructorFuncWithSchema | ||
} from "node-opcua-factory"; | ||
@@ -20,2 +22,3 @@ | ||
import { prepareEnumeratedType, prepareStructureType } from "./tools"; | ||
import { DataTypeFactory } from "node-opcua-factory"; | ||
@@ -96,4 +99,3 @@ const doDebug = checkDebugFlag(__filename); | ||
export interface TypeDictionary { | ||
defaultByteOrder: "LittleEndian"; | ||
export interface ITypeDictionary { | ||
targetNamespace: string; | ||
@@ -108,2 +110,27 @@ imports: string[]; | ||
export class TypeDictionary extends DataTypeFactory implements ITypeDictionary { | ||
structuredTypes: { | ||
[key: string]: StructuredTypeSchema; | ||
}; | ||
enumeratedTypes: { | ||
[key: string]: EnumerationDefinitionSchema; | ||
}; | ||
structuredTypesRaw: { | ||
[key: string]: StructureTypeRaw; | ||
}; | ||
enumeratedTypesRaw: { | ||
[key: string]: EnumeratedType; | ||
}; | ||
constructor(baseDataFactories: DataTypeFactory[]) { | ||
super(baseDataFactories); | ||
this.structuredTypes= {}; | ||
this.structuredTypesRaw= {}; | ||
this.enumeratedTypes= {}; | ||
this.enumeratedTypesRaw= {}; | ||
} | ||
} | ||
/* tslint:disable:object-literal-shorthand */ | ||
@@ -272,16 +299,10 @@ const state0: any = { | ||
export function parseBinaryXSD( | ||
xmlString: string, | ||
dataTypeFactories: DataTypeFactory[], | ||
callback: (err: Error | null, typeDictionary: TypeDictionary | ||
) => void) { | ||
const typeDictionary: TypeDictionary = { | ||
defaultByteOrder: "LittleEndian", | ||
targetNamespace: "", | ||
imports: [], | ||
structuredTypes: {}, | ||
structuredTypesRaw: {}, | ||
enumeratedTypes: {}, | ||
enumeratedTypesRaw: {} | ||
}; | ||
const typeDictionary = new TypeDictionary(dataTypeFactories); | ||
@@ -288,0 +309,0 @@ const parser = new Xml2Json(state0); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
113256
1906
Updatednode-opcua-data-model@^2.1.3
Updatednode-opcua-factory@^2.1.3
Updatednode-opcua-nodeid@^2.1.3
Updatednode-opcua-variant@^2.1.3
Updatednode-opcua-xml2json@^2.1.3