-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add semiliteral operator for expressions inside arrays and objects
- Loading branch information
Showing
10 changed files
with
293 additions
and
3 deletions.
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
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,109 @@ | ||
import { | ||
ObjectType, | ||
ValueType, | ||
array, | ||
} from '../types'; | ||
|
||
import type {Expression} from '../expression'; | ||
import type {ParsingContext} from '../parsing_context'; | ||
import type {EvaluationContext} from '../evaluation_context'; | ||
import type {Type} from '../types'; | ||
import {typeOf, Value, isValue} from '../values'; | ||
import {Literal} from './literal'; | ||
import exp from 'constants'; | ||
|
||
export abstract class Semiliteral implements Expression { | ||
type: Type; | ||
|
||
constructor(type: Type) { | ||
this.type = type; | ||
} | ||
|
||
static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression { | ||
if (args.length !== 2) | ||
return context.error(`'semiliteral' expression requires exactly one argument, but found ${args.length - 1} instead.`) as null; | ||
|
||
if (!isValue(args[1])) | ||
return context.error('invalid value') as null; | ||
|
||
const value = args[1] as Value; | ||
const type = typeOf(value); | ||
|
||
if (type.kind === 'array') { | ||
const arr = value as Array<unknown>; | ||
const parsed = arr.map(item => context.parse(item, null, ValueType)); | ||
return new ArraySemiliteral(parsed); | ||
} else if (type.kind === 'object') { | ||
const obj = value as Record<string, unknown>; | ||
const parsed = Object.keys(obj).reduce((acc, key) => { | ||
acc[key] = context.parse(obj[key], null, ValueType); | ||
return acc; | ||
}, {} as Record<string, Expression>); | ||
return new ObjectSemiliteral(parsed); | ||
} else { | ||
return new Literal(type, value); | ||
} | ||
} | ||
|
||
abstract evaluate(ctx: EvaluationContext): Value; | ||
|
||
abstract eachChild(fn: (_: Expression) => void): void; | ||
|
||
abstract outputDefined(): boolean; | ||
} | ||
|
||
class ArraySemiliteral extends Semiliteral { | ||
arr: Array<Expression>; | ||
|
||
constructor(arr: Array<Expression>) { | ||
let elementType: Type | null = null; | ||
for (const expr of arr) { | ||
if (!elementType) { | ||
elementType = expr.type; | ||
} else if (elementType === expr.type) { | ||
continue; | ||
} else { | ||
elementType = ValueType; | ||
break; | ||
} | ||
} | ||
super(array(elementType ?? ValueType, arr.length)); | ||
this.arr = arr; | ||
} | ||
|
||
evaluate(ctx: EvaluationContext): Array<Value> { | ||
return this.arr.map(arg => arg.evaluate(ctx)); | ||
} | ||
|
||
eachChild(fn: (_: Expression) => void) { | ||
this.arr.forEach(fn); | ||
} | ||
|
||
outputDefined() { | ||
return this.arr.every(arg => arg.outputDefined()); | ||
} | ||
} | ||
|
||
class ObjectSemiliteral extends Semiliteral { | ||
obj: Record<string, Expression>; | ||
|
||
constructor(obj: Record<string, Expression>) { | ||
super(ObjectType); | ||
this.obj = obj; | ||
} | ||
|
||
evaluate(ctx: EvaluationContext): Record<string, Value> { | ||
return Object.keys(this.obj).reduce((acc, key) => { | ||
acc[key] = this.obj[key].evaluate(ctx); | ||
return acc; | ||
}, {} as Record<string, Value>); | ||
} | ||
|
||
eachChild(fn: (_: Expression) => void) { | ||
Object.values(this.obj).forEach(fn); | ||
} | ||
|
||
outputDefined() { | ||
return Object.values(this.obj).every(arg => arg.outputDefined()); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
test/integration/expression/tests/semiliteral/directional-offset/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,35 @@ | ||
{ | ||
"expression": [ | ||
"semiliteral", | ||
[ | ||
["round", ["*", ["cos", ["get", "direction"]], ["get", "magnitude"]]], | ||
["round", ["*", ["sin", ["get", "direction"]], ["get", "magnitude"]]] | ||
] | ||
], | ||
"inputs": [ | ||
[ | ||
{ | ||
}, | ||
{ | ||
"properties": { | ||
"direction": 0, | ||
"magnitude": 10 | ||
} | ||
} | ||
] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "array<number, 2>" | ||
}, | ||
"outputs": [ | ||
[ | ||
10, | ||
0 | ||
] | ||
] | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
test/integration/expression/tests/semiliteral/get-offset-components/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,34 @@ | ||
{ | ||
"expression": [ | ||
"semiliteral", | ||
[ | ||
["number", ["get", "x"]], | ||
["number", ["get", "y"]] | ||
] | ||
], | ||
"inputs": [ | ||
[ | ||
{}, | ||
{ | ||
"properties": { | ||
"x": 1, | ||
"y": 2 | ||
} | ||
} | ||
] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "array<number, 2>" | ||
}, | ||
"outputs": [ | ||
[ | ||
1, | ||
2 | ||
] | ||
] | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
test/integration/expression/tests/semiliteral/number-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,29 @@ | ||
{ | ||
"expression": [ | ||
"semiliteral", | ||
[ | ||
1, | ||
2 | ||
] | ||
], | ||
"inputs": [ | ||
[ | ||
{}, | ||
{} | ||
] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": true, | ||
"isZoomConstant": true, | ||
"type": "array<number, 2>" | ||
}, | ||
"outputs": [ | ||
[ | ||
1, | ||
2 | ||
] | ||
] | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
test/integration/expression/tests/semiliteral/scale-offset/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,36 @@ | ||
{ | ||
"expression": [ | ||
"let", | ||
"dp_per_em", | ||
12, | ||
[ | ||
"semiliteral", | ||
[ | ||
["/", 6, ["var", "dp_per_em"]], | ||
["/", 18, ["var", "dp_per_em"]] | ||
] | ||
] | ||
], | ||
"inputs": [ | ||
[ | ||
{ | ||
}, | ||
{ | ||
} | ||
] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": true, | ||
"isZoomConstant": true, | ||
"type": "array<number, 2>" | ||
}, | ||
"outputs": [ | ||
[ | ||
0.5, | ||
1.5 | ||
] | ||
] | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
test/integration/expression/tests/semiliteral/string-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,29 @@ | ||
{ | ||
"expression": [ | ||
"semiliteral", | ||
[ | ||
"x", | ||
"y" | ||
] | ||
], | ||
"inputs": [ | ||
[ | ||
{}, | ||
{} | ||
] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": true, | ||
"isZoomConstant": true, | ||
"type": "array<string, 2>" | ||
}, | ||
"outputs": [ | ||
[ | ||
"x", | ||
"y" | ||
] | ||
] | ||
} | ||
} |