From 44a9f81b7b9eb3f53e800f3db869a498853e316a Mon Sep 17 00:00:00 2001 From: Christian Fritz Date: Fri, 14 Oct 2016 05:46:54 -0700 Subject: [PATCH] Small bug fix for constants in on-the-fly message classes (#33) Prior to this commit constant values like "1.0", i.e., floats that are integer, were not correctly treated in the md5sum calculation -- they were abbreviated to "1", causing incorrect md5sums. This commit fixes that by using the original string value for md5sum calculation (up to the start of a comment (for strings) or the first whitespace character for any other field type). - also fixing some wrong logic for null values --- utils/messages.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/utils/messages.js b/utils/messages.js index 10229c2..bd8dc1f 100644 --- a/utils/messages.js +++ b/utils/messages.js @@ -341,14 +341,27 @@ function extractFields(content, details, callback) { if (equalIndex !== -1) { fieldName = field.substring(0, equalIndex).trim(); + // truncate comments and whitespace var constant = field.substring(equalIndex + 1, field.length).trim(); + var constantEnd = constant.length; + var constantComment = constant.indexOf("#"); + if (constantComment >= 0) { + if (fieldType != "string") { + var firstWhitespace = constant.match(/\s/); + if (firstWhitespace) { + constantEnd = firstWhitespace.index; + } + } else { + constantEnd = constantComment; + } + } var parsedConstant = fieldsUtil.parsePrimitive(fieldType, constant); constants.push({ name : fieldName , type : fieldType , value : parsedConstant - , raw : constant + , raw : constant.slice(0, constantEnd) , index : fields.length , messageType : null }); @@ -504,7 +517,9 @@ function buildMessageClass(details) { new (field.messageType)(values ? values[field.name] : undefined); } else { // simple value - that[field.name] = values ? values[field.name] : + that[field.name] = + (values && typeof values[field.name] != "undefined") ? + values[field.name] : (field.value || fieldsUtil.getDefaultValue(field.type)); } });