-
Notifications
You must be signed in to change notification settings - Fork 15
[PDE-683] (BREAKING) Resolve curlies to their original data type #139
Conversation
c2810de
to
d21cdee
Compare
src/tools/cleaner.js
Outdated
const s = String(key).replace(/[-[\]/{}()\\*+?.^$|]/g, '\\$&'); | ||
const re = new RegExp(s, 'g'); | ||
out = out.replace(re, bank[key]); | ||
const escapedKey = String(key).replace(/[-[\]/{}()\\*+?.^$|]/g, '\\$&'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the names here to be a bit more descriptive. I think we could drop the String
constructor. Since key
is derived from Object.keys
, we should be able to safely assume it is always a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right. Let's drop it!
d21cdee
to
6665bdd
Compare
@@ -14,6 +14,18 @@ const isPlainObj = o => { | |||
|
|||
const comparison = (obj, needle) => obj === needle; | |||
|
|||
const getObjectType = obj => { | |||
if (_.isPlainObject(obj)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason we have our own isPlainObject
? Is it preferred to lodash's?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤷♂️ but yeah, I think we should stick to lodash's.
@@ -130,8 +142,7 @@ const recurseExtract = (obj, matcher) => { | |||
const _IGNORE = {}; | |||
|
|||
// Flatten a nested object. | |||
const flattenPaths = (data, sep) => { | |||
sep = sep || '.'; | |||
const flattenPaths = (data, sep = '.') => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested locally, I don't find any issues. Good work!
There's a failing test you need to fix in test/logger.js
. Also got some minor suggestions but overall it's a 👍from me. Since it's a breaking change, maybe @bcooksey would like to take a quick look?
src/tools/cleaner.js
Outdated
const s = String(key).replace(/[-[\]/{}()\\*+?.^$|]/g, '\\$&'); | ||
const re = new RegExp(s, 'g'); | ||
out = out.replace(re, bank[key]); | ||
const escapedKey = String(key).replace(/[-[\]/{}()\\*+?.^$|]/g, '\\$&'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right. Let's drop it!
src/tools/cleaner.js
Outdated
const isPartOfString = valueParts.length > 1; | ||
|
||
if (isPartOfString) { | ||
if (_.isArray(replacementValue) || _.isPlainObject(replacementValue)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here we can use native Array.isArray
instead of lodash's _.isArray
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally agree. Only used _.isArray
since we were grabbing _.isPlainObject
right next to it. No strong feeling about it though
} | ||
|
||
out = isPartOfString | ||
? valueParts.join('').replace(matchesKey, replacementValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like valueParts.join('')
always equals to out
here. So can we simplify this line to the following?
? valueParts.join('').replace(matchesKey, replacementValue) | |
? out.replace(matchesKey, replacementValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh. Nice catch @eliangcs
src/tools/data.js
Outdated
return 'Object'; | ||
} | ||
|
||
if (_.isArray(obj)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likewise, native Array.isArray
is preferred.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, same reason as above. Also don't feel strong about it here either.
src/tools/cleaner.js
Outdated
const flattenPaths = require('./data').flattenPaths; | ||
const { isPlainObj, getObjectType } = require('./data'); | ||
const { recurseReplace } = require('./data'); | ||
const { flattenPaths } = require('./data'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice cleanup! Can we make it even better by importing them in one statement?
const { isPlainObj, getObjectType, recurseReplace, flattenPaths } = require('./data');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha yikes. I just now noticed that those were coming from the same file. Thanks for that!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with CH's great review (as per usual). Added a minor gripe on the error message text, but otherwise 👍
src/tools/cleaner.js
Outdated
'Cannot interpolate objects or arrays into a string.', | ||
`You've sent an ${getObjectType(replacementValue)},`, | ||
`which will get coerced to ${replacementValue}` | ||
].join(' ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason this is an array vs a string that prettier breaks up automatically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I just found it easier to read this way than the way that prettier was formatting it. I don't feel strongly about, though. Can totally be just a string.
test/create-request-client.js
Outdated
message: 'No arrays, thank you: {{bundle.inputData.badData}}' | ||
} | ||
}).should.be.rejectedWith( | ||
"Cannot interpolate objects or arrays into a string. You've sent an Array, which will get coerced to 1,2,3" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
coerced to 1,2,3
I find this wording a little confusing, since it's not clear that 1,2,3
is a string. Maybe change the error to be
which will get coerced into the string "1,2,3"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also maybe change it to Cannot reliably interpolate...
to make it clear that we can, but it might have weird behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good points
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is which will get coerced into the string
actually true? We are always going to throw this error, yea? Maybe just a stringified dump of the first 10-20 characters to help them know which array we are getting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it will always throw. The intent was to say, "Hey there, here's what would have gone through". But maybe that's unnecessary. We could just say, "Can't do it. Here's what you sent".
We are still sending them the data above. But I can see the value in keeping the output simpler.
test/create-request-client.js
Outdated
message: 'No arrays, thank you: {{bundle.inputData.badData}}' | ||
} | ||
}).should.be.rejectedWith( | ||
"Cannot interpolate objects or arrays into a string. You've sent an Array, which will get coerced to 1,2,3" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is which will get coerced into the string
actually true? We are always going to throw this error, yea? Maybe just a stringified dump of the first 10-20 characters to help them know which array we are getting?
931d132
to
98a07f5
Compare
98a07f5
to
53472e2
Compare
Description
CLI integrations always use bundle values directly from the bundle we inject to their request functions:
But, if we are using curly tokens to be replaced with an actual value,
core
interpolates value in a string. One example is in a perform shorthand RequestSchema.This fix keeps the data types of values replaced via curlies. The one exception is when we have an interpolated value as part of a string (ex.
'Bearer {{bundle.authData.access_token}}'
). In this case we:The tests do a better job of illustrating what we're doing now.
Jira
https://zapierorg.atlassian.net/browse/PDE-683