Skip to content

Stricter Data Type checks - #674

Closed
arthurschreiber wants to merge 27 commits into
masterfrom
arthur/stricter-data-type-checks
Closed

arthurschreiber wants to merge 27 commits into
masterfrom
arthur/stricter-data-type-checks

Conversation

@arthurschreiber

Copy link
Copy Markdown
Collaborator

This pull request tightens up the type checks in the writeParameterData methods across all data types that implement this method.

I tried to keep backwards compatibility where possible, as I don't want this to be a breaking change. E.g. this still allows passing in strings to types like TinyInt, SmallInt, Int, Money, Float and so on. I'm not super happy about doing type conversion, as it can be lossy and lead to incorrect data being stored. But as changing this will be a breaking change for many users, I want to punt on that for now.

I did not try to keep compatibility with object-to-value conversion (via .valueOf methods), as this was never an explicitly supported feature and I doubt anyone really relied on this.

This change also tries to fix some cases where tedious was causing a loss of precision, especially with types like Float and Real, where passing a number type would always cause a conversion to a string and then back to a number via parseFloat. 😞

This pull request also does not try to fix the schism between data type handling of plain Request and BulkLoad. Currently, the difference between those two in the way parameters are handled can lead to different results. This is really crazy, but changing this could also potentially be backwards incompatible.

@v-suhame @chdh @atsage What do you think about these changes?

@arthurschreiber
arthurschreiber force-pushed the arthur/stricter-data-type-checks branch from 1408a28 to 12b3629 Compare January 7, 2018 13:17
Comment thread src/data-type.js
}

buffer.writeUInt8(1);
if (typeof value === 'number') {

@chdh chdh Jan 7, 2018 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the same program logic in validate(), i.e. test whether the value is already a number before using parseInt()?
And maybe use Math.round() if it's a number? Otherwise it's not backward compatible.

Comment thread src/data-type.js
buffer.writeUInt8(4);
buffer.writeUInt16LE(days);
if (!(value instanceof Date)) {
throw new TypeError(`parameter.value must be a Date, undefined or null. Received type ${typeof value}`);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should also be made in validate(), because Request.validateParameters() calls validate() and generates a better error message if the exception occurs there.
If the error occurs later in the constructor of RpcRequestPayload, the error message would not indicate the parameter name.

Comment thread src/data-type.js
buffer.writeUInt8(sign);
buffer.writeUInt64LE(decimalValue);
buffer.writeUInt32LE(0x00000000);
buffer.writeUInt32LE(0x00000000);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still an unnecessary parseFloat() in the corresponding validate() function below.

Comment thread src/data-type.js
}

buffer.writeUInt8(4);
if (typeof value === 'number') {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's a request parameter and the value is the number 0.000000006, the parseInt() call in the corresponding validate() function below will still convert it to 6, because in Request.validateParameters() the return value of validate() is used to replace the original parameter value.

@chdh

chdh commented Jan 8, 2018 •

Copy link
Copy Markdown
Collaborator

I would split the program logic for each data type into the following 3 functions:

  • convert: Convert the value if necessary and return the converted value (or the original input value if no conversion is necessary).
  • validate: Check whether the passed value is valid for the data type, without any conversion. Return a boolean that indicates whether the value is valid or not. The error message is better composed in the caller.
  • write: Write the value to the buffer, without any conversion or validation.

Usage:

  • In Request.validateParameters(): Call convert() and validate(). (Replace the parameter value with the converted value, like it's been done so far).
  • In RpcRequestPayload: Call write().
  • In BulkLoad: Call validate() and write().

Add an "autoconvert" option that can be used to skip the convert() call in validateParameters() (if autoconvert is false). Default could be true for Tedious 2.x and false for Tedious 3.x.

Document the behavior of the 3 functions convert, validate and write, maybe with an interface?

@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

Closing this in favor of #677. 😄

@arthurschreiber
arthurschreiber deleted the arthur/stricter-data-type-checks branch January 8, 2019 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants