Skip to content

Add slice and index-of expressions - #9450

Merged
ryanhamley merged 7 commits into
mapbox:masterfrom
lbutler:slice-expression
Apr 2, 2020
Merged

ryanhamley merged 7 commits into
mapbox:masterfrom
lbutler:slice-expression

Conversation

@lbutler

@lbutler lbutler commented Mar 23, 2020 •

Copy link
Copy Markdown
Contributor

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-of to 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!

  • briefly describe the changes in this PR
  • write tests for all new functionality
  • document any changes to public APIs
  • tagged @mapbox/map-design-team @mapbox/static-apis if this PR includes style spec API or visual changes
  • tagged @mapbox/gl-native if this PR includes shader changes or needs a native port
  • apply changelog label ('bug', 'feature', 'docs', etc) or use the label 'skip changelog'
  • add an entry inside this element for inclusion in the mapbox-gl-js changelog: <changelog>Add index-ofandslice expressions 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:

  • Fix Flow type errors - apologies, first time with flow and didn't run the tests locally

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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;

ghost Mar 24, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 === true

In 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?

ghost Mar 25, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

ghost Apr 1, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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))

ghost Mar 24, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

ghost Mar 24, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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!

ghost Mar 25, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

ghost Mar 25, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No problems at all, happy to modify to whatever the mapbox team thinks fits best. I see at least three options:

  1. As is, used in coercion - single class and check arg[0] to change function return (will admit, it is a bit messy..)
  2. Factory method used in comparision
  3. New pattern for expressions using a base class plus inheritances classes for index-of and in

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.

ghost Mar 25, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

ghost Apr 1, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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.

@asheemmamoowala

ghost commented Mar 24, 2020

Copy link
Copy Markdown
Contributor

cc @mapbox/gl-native @mapbox/studio

type.kind === 'string' ||
type.kind === 'value';
}
function isAcceptableInputRuntimeValue(input: Array<Value> | string) {

ghost Mar 25, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@lbutler
lbutler requested a review from arindam1993 April 1, 2020 19:45

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Spec changes look solid 👍

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes look good to me @lbutler ! Thanks for the contribution! 🎉

@karimnaaji karimnaaji added this to the release-d milestone Apr 2, 2020

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks great! Thanks for the contribution @lbutler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"in" expression always returns false if needle argument is null Add support for slicing strings and arrays in expressions

6 participants