-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add slice
and index-of
expressions
#9450
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f49e806
Implement "slice" expression
lbutler 74a2b68
Implement "index-of" expression
lbutler 7a2f901
Fixing flow type error in index-of and slice expression
lbutler 4b2893b
Merged in & index-of expressions to the same class
lbutler 394dd27
Revert "Merged in & index-of expressions to the same class"
lbutler f67dc4d
Refactor type checks for index-of & slice expression
lbutler 1581ac4
Refactor type check for in expression and allowed null needle
lbutler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// @flow | ||
|
||
import {ValueType, toString, NumberType} from '../types'; | ||
import RuntimeError from '../runtime_error'; | ||
import {typeOf} from '../values'; | ||
|
||
import type {Expression} from '../expression'; | ||
import type ParsingContext from '../parsing_context'; | ||
import type EvaluationContext from '../evaluation_context'; | ||
import type {Type} from '../types'; | ||
import type {Value} from '../values'; | ||
|
||
function isComparableType(type: Type) { | ||
return type.kind === 'boolean' || | ||
type.kind === 'string' || | ||
type.kind === 'number' || | ||
type.kind === 'null' || | ||
type.kind === 'value'; | ||
} | ||
|
||
function isComparableRuntimeValue(needle: boolean | string | number | null) { | ||
return typeof needle === 'boolean' || | ||
typeof needle === 'string' || | ||
typeof needle === 'number' || | ||
needle === null; | ||
} | ||
|
||
function isSearchableRuntimeValue(haystack: Array<Value> | string) { | ||
return Array.isArray(haystack) || | ||
typeof haystack === 'string'; | ||
} | ||
|
||
class IndexOf implements Expression { | ||
type: Type; | ||
needle: Expression; | ||
haystack: Expression; | ||
fromIndex: ?Expression; | ||
|
||
constructor(needle: Expression, haystack: Expression, fromIndex?: Expression) { | ||
this.type = NumberType; | ||
this.needle = needle; | ||
this.haystack = haystack; | ||
this.fromIndex = fromIndex; | ||
} | ||
|
||
static parse(args: $ReadOnlyArray<mixed>, context: ParsingContext) { | ||
if (args.length <= 2 || args.length >= 5) { | ||
return context.error(`Expected 3 or 4 arguments, but found ${args.length - 1} instead.`); | ||
} | ||
|
||
const needle = context.parse(args[1], 1, ValueType); | ||
|
||
const haystack = context.parse(args[2], 2, ValueType); | ||
|
||
if (!needle || !haystack) return null; | ||
if (!isComparableType(needle.type)) { | ||
return context.error(`Expected first argument to be of type boolean, string, number or null, but found ${toString(needle.type)} instead`); | ||
} | ||
|
||
if (args.length === 4) { | ||
const fromIndex = context.parse(args[3], 3, NumberType); | ||
if (!fromIndex) return null; | ||
return new IndexOf(needle, haystack, fromIndex); | ||
} else { | ||
return new IndexOf(needle, haystack); | ||
} | ||
} | ||
|
||
evaluate(ctx: EvaluationContext) { | ||
const needle = (this.needle.evaluate(ctx): any); | ||
const haystack = (this.haystack.evaluate(ctx): any); | ||
|
||
if (!isComparableRuntimeValue(needle)) { | ||
throw new RuntimeError(`Expected first argument to be of type boolean, string or number, but found ${toString(typeOf(needle))} instead.`); | ||
} | ||
|
||
if (!isSearchableRuntimeValue(haystack)) { | ||
throw new RuntimeError(`Expected second argument to be of type array or string, but found ${toString(typeOf(haystack))} instead.`); | ||
} | ||
|
||
if (this.fromIndex) { | ||
const fromIndex = (this.fromIndex.evaluate(ctx): number); | ||
return haystack.indexOf(needle, fromIndex); | ||
} | ||
|
||
return haystack.indexOf(needle); | ||
} | ||
|
||
eachChild(fn: (_: Expression) => void) { | ||
fn(this.needle); | ||
fn(this.haystack); | ||
if (this.fromIndex) { | ||
fn(this.fromIndex); | ||
} | ||
} | ||
|
||
outputDefined() { | ||
return false; | ||
} | ||
|
||
serialize() { | ||
if (this.fromIndex != null && this.fromIndex !== undefined) { | ||
const fromIndex = this.fromIndex.serialize(); | ||
return ["index-of", this.needle.serialize(), this.haystack.serialize(), fromIndex]; | ||
} | ||
return ["index-of", this.needle.serialize(), this.haystack.serialize()]; | ||
} | ||
} | ||
|
||
export default IndexOf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// @flow | ||
|
||
import {ValueType, NumberType, toString} from '../types'; | ||
import RuntimeError from '../runtime_error'; | ||
import {typeOf} from '../values'; | ||
|
||
import type {Expression} from '../expression'; | ||
import type ParsingContext from '../parsing_context'; | ||
import type EvaluationContext from '../evaluation_context'; | ||
import type {Type} from '../types'; | ||
import type {Value} from '../values'; | ||
|
||
function isSliceableType(type: Type) { | ||
return type.kind === 'array' || | ||
type.kind === 'string' || | ||
type.kind === 'value'; | ||
} | ||
function isAcceptableInputRuntimeValue(input: Array<Value> | string) { | ||
return Array.isArray(input) || | ||
typeof input === 'string'; | ||
} | ||
|
||
class Slice implements Expression { | ||
type: Type; | ||
input: Expression; | ||
beginIndex: Expression; | ||
endIndex: ?Expression; | ||
|
||
constructor(type: Type, input: Expression, beginIndex: Expression, endIndex?: Expression) { | ||
this.type = type; | ||
this.input = input; | ||
this.beginIndex = beginIndex; | ||
this.endIndex = endIndex; | ||
|
||
} | ||
|
||
static parse(args: $ReadOnlyArray<mixed>, context: ParsingContext) { | ||
if (args.length <= 2 || args.length >= 5) { | ||
return context.error(`Expected 3 or 4 arguments, but found ${args.length - 1} instead.`); | ||
} | ||
|
||
const input = context.parse(args[1], 1, ValueType); | ||
const beginIndex = context.parse(args[2], 2, NumberType); | ||
|
||
if (!input || !beginIndex) return null; | ||
if (!isSliceableType(input.type)) { | ||
return context.error(`Expected first argument to be of type array or string, but found ${toString(input.type)} instead`); | ||
} | ||
|
||
if (args.length === 4) { | ||
const endIndex = context.parse(args[3], 3, NumberType); | ||
if (!endIndex) return null; | ||
return new Slice(input.type, input, beginIndex, endIndex); | ||
} else { | ||
return new Slice(input.type, input, beginIndex); | ||
} | ||
} | ||
|
||
evaluate(ctx: EvaluationContext) { | ||
const input = (this.input.evaluate(ctx): any); | ||
const beginIndex = (this.beginIndex.evaluate(ctx): number); | ||
|
||
if (!isAcceptableInputRuntimeValue(input)) { | ||
throw new RuntimeError(`Expected first argument to be of type array or string, but found ${toString(typeOf(input))} instead.`); | ||
} | ||
|
||
if (this.endIndex) { | ||
const endIndex = (this.endIndex.evaluate(ctx): number); | ||
return input.slice(beginIndex, endIndex); | ||
} | ||
|
||
return input.slice(beginIndex); | ||
} | ||
|
||
eachChild(fn: (_: Expression) => void) { | ||
fn(this.input); | ||
fn(this.beginIndex); | ||
if (this.endIndex) { | ||
fn(this.endIndex); | ||
} | ||
} | ||
|
||
outputDefined() { | ||
return false; | ||
} | ||
|
||
serialize() { | ||
if (this.endIndex != null && this.endIndex !== undefined) { | ||
const endIndex = this.endIndex.serialize(); | ||
return ["slice", this.input.serialize(), this.beginIndex.serialize(), endIndex]; | ||
} | ||
return ["slice", this.input.serialize(), this.beginIndex.serialize()]; | ||
} | ||
} | ||
|
||
export default Slice; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
test/integration/expression-tests/index-of/assert-array/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"expression": ["index-of", ["get", "i"], ["array", ["get", "arr"]]], | ||
"inputs": [ | ||
[{}, { "properties": { "i": null, "arr": [9, 8, 7] } }], | ||
[{}, { "properties": { "i": null, "arr": [9, 8, 7, null] } }], | ||
[{}, { "properties": { "i": 1, "arr": [9, 8, 7] } }], | ||
[{}, { "properties": { "i": 9, "arr": [9, 8, 7, 9] } }], | ||
[{}, { "properties": { "i": 1, "arr": null } }] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "number" | ||
}, | ||
"outputs": [ | ||
-1, | ||
3, | ||
-1, | ||
0, | ||
{ "error": "Expected value to be of type array, but found null instead." } | ||
], | ||
"serialized": ["index-of", ["get", "i"], ["array", ["get", "arr"]]] | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
test/integration/expression-tests/index-of/assert-string/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"expression": ["index-of", ["get", "substr"], ["string", ["get", "str"]]], | ||
"inputs": [ | ||
[{}, { "properties": { "substr": null, "str": "helloworld" } }], | ||
[{}, { "properties": { "substr": "foo", "str": "helloworld" } }], | ||
[{}, { "properties": { "substr": "low", "str": "helloworld" } }], | ||
[{}, { "properties": { "substr": "low", "str": null } }] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "number" | ||
}, | ||
"outputs": [ | ||
-1, | ||
-1, | ||
3, | ||
{ | ||
"error": "Expected value to be of type string, but found null instead." | ||
} | ||
], | ||
"serialized": ["index-of", ["get", "substr"], ["string", ["get", "str"]]] | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
test/integration/expression-tests/index-of/basic-array/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"expression": ["index-of", ["get", "i"], ["get", "arr"]], | ||
"inputs": [ | ||
[{}, { "properties": { "i": null, "arr": [9, 8, 7] } }], | ||
[{}, { "properties": { "i": 1, "arr": [9, 8, 7] } }], | ||
[{}, { "properties": { "i": 9, "arr": [9, 8, 7] } }], | ||
[ | ||
{}, | ||
{ | ||
"properties": { | ||
"i": "foo", | ||
"arr": ["baz", "bar", "hello", "foo", "world"] | ||
} | ||
} | ||
], | ||
[ | ||
{}, | ||
{ | ||
"properties": { | ||
"i": true, | ||
"arr": ["foo", 123, null, 456, false, {}, true] | ||
} | ||
} | ||
], | ||
[{}, { "properties": { "i": 1, "arr": null } }] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "number" | ||
}, | ||
"outputs": [ | ||
-1, | ||
-1, | ||
0, | ||
3, | ||
6, | ||
{ | ||
"error": "Expected second argument to be of type array or string, but found null instead." | ||
} | ||
], | ||
"serialized": ["index-of", ["get", "i"], ["get", "arr"]] | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.