Repository navigation
Add slice and index-of expressions - #9450
Conversation
left a comment
There was a problem hiding this comment.
Thanks a lot for the contribution @lbutler! Much appreciated!
I think your approach looks good overall, but looks like theres a lot of shared code withe the In expression that has been duplicated.
Could you please either extend your new expressions from In so that a lot of the validation logic doesnt have to be duplicated OR make some of the validation functions like isComparableType, isComparableRuntimeValue shared utils?
This would really help keep the bundle size in check!
| const haystack = (this.haystack.evaluate(ctx): any); | ||
|
|
||
| if (needle == null || !haystack) return false; | ||
| if (!haystack && this.type.kind === 'boolean') return false; |
There was a problem hiding this comment.
Previously in would allow the needle to be null but would always return false, this means that if you had an array with null values in it and you were searching for the nulls it would always return false.
[1,2,3,null,5].indexOf(null) >= 0 === trueIn addition, the check returns false if the haystack is null. This was raised in the initial review for the in expression.
The difference is in whether or not we're asserting that the value is a string or array. If we do ['string', ['get', 'str']] and str is null, then we get a type error. But if we don't assert it's a string or array and just directly return null then the check on line 65 of in.js returns false because there's no value that can be checked
I've left it so that the in expression keeps this return path, but potentially we need to make index-of return -1 so it's consistent between the two expressions?
There was a problem hiding this comment.
We need to keep the type checking (e.g. ['string', ['get', 'str']] // -> null should return a type error). But maybe we should allow the needle to be null. See #7197 (review)
since "in" is using the same equality semantics as "==" (i.e. JS ===), it should have the same constraints, so it can't be used with types where === doesn't make sense. Specifically the type of needle should be limited to these types. If the static type is not one of those five, it should be a parse error. Or, if the static type is value and the runtime type isn't one of the other four, it should be an evaluation error.
null === null returns true though so it seems like we should accept that value and this was an oversight. Technically, undefined should work too, but I'm not sure we want to do that. You would need to update the isComparableRuntimeValue function to include null and update the error on line 68 to mention that null is an acceptable value
There was a problem hiding this comment.
Just letting you know that this was updated in the latest commit and I added a test case as well
| return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`); | ||
| } | ||
|
|
||
| if (name === 'index-of' && (args.length <= 2 || args.length >= 5)) |
There was a problem hiding this comment.
I was actually suggesting an inheritance based refactor rather than a runtime check based one, in which both In and IndexOf would share a common base-class
There was a problem hiding this comment.
Ahh that sucks because that what I initially did but then swapped it to this method which is how it is in ./coercion.js.
There is also another method used in ./comparison.js which uses a function to create the class.
I didn't want to add a third method of doing it so I went this way, probably should have gotten some clarification before I went and did these changes!
There was a problem hiding this comment.
Ah, I was unaware of those patterns in those files. They seem like a pattern carried over from ES5 days, but I'm not 100% certain on this. @ryanhamley do you have any thoughts?
There was a problem hiding this comment.
No problems at all, happy to modify to whatever the mapbox team thinks fits best. I see at least three options:
- As is, used in
coercion- single class and checkarg[0]to change function return (will admit, it is a bit messy..) - Factory method used in
comparision - New pattern for expressions using a base class plus inheritances classes for
index-ofandin
Potentially as a much larger refactor you could change the Definition type that goes into CompoundExpression.register() to accept generic return types, or overload with a return type and then you wouldn't need custom definitions for index-of, slice and in
Either way, we can wait for @ryanhamley to provide some input and I can have enough crack at this PR tonight.
There was a problem hiding this comment.
I think coercion is a bit of a special case that's mostly used internally. Instead of combining in and index-of or having index-of inherit from in, I think it makes more sense to move common functions such as isComparableType to a single file. It may make sense to put them in expression/types.js since there are already some shared type checking functions in that file.
There was a problem hiding this comment.
@ryanhamley & @arindam1993 thanks both for your input. I've refactored with the latest commits. I've moved the functions that check the types into expression/types.js and updated the three expressions to use these common functions.
I haven't created a base class that the other extends but if that's what we want to do then I'm happy to do another refactor.
commented
Mar 24, 2020
|
cc @mapbox/gl-native @mapbox/studio |
| type.kind === 'string' || | ||
| type.kind === 'value'; | ||
| } | ||
| function isAcceptableInputRuntimeValue(input: Array<Value> | string) { |
There was a problem hiding this comment.
minot nit: this is the same logic as isSearchableRuntimeValue, could you move it to a shared util file.
Sorry if this seems v minor, just trying to be extra strict on bundle size rn.
left a comment
There was a problem hiding this comment.
Changes look good to me @lbutler ! Thanks for the contribution! 🎉
left a comment
There was a problem hiding this comment.
This looks great! Thanks for the contribution @lbutler
Launch Checklist
The expression API master ticket #6484 lists slicing for both strings and arrays. As per #9443, @arindam1993 & @ryanhamley both suggested also including
index-ofto be able to search a string or array otherwise you would be limited to hardcoded indexes only.This is my first PR into mapbox-gl-js so please let me know if I need to change/update anything to get this merged in, thanks!
sliceto extract part of string or arrayindex-ofto find the index of a value within a string or arrayinexpression will now search fornullwithin an array instead of always returning false@mapbox/map-design-team@mapbox/static-apisif this PR includes style spec API or visual changes@mapbox/gl-nativeif this PR includes shader changes or needs a native portmapbox-gl-jschangelog:<changelog>Addindex-ofandsliceexpressions to search arrays and strings for the first occurrence of a specified value and return a section of the original array or string.</changelog>Edit Todo: