Skip to content

Commit 23f0dbe

Browse files
committed
feat: add semiliteral operator for expressions inside arrays and objects
1 parent 93a8cd3 commit 23f0dbe

File tree

10 files changed

+296
-3
lines changed

10 files changed

+296
-3
lines changed

build/generate-style-spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export type ExpressionSpecification =
162162
| ['format', ...(string | ['image', ExpressionSpecification] | ExpressionSpecification | {'font-scale'?: number | ExpressionSpecification, 'text-font'?: string[] | ExpressionSpecification, 'text-color'?: ColorSpecification | ExpressionSpecification})[]] // string
163163
| ['image', unknown | ExpressionSpecification] // image
164164
| ['literal', unknown]
165+
| ['semiliteral', unknown]
165166
| ['number', unknown | ExpressionSpecification, ...(unknown | ExpressionSpecification)[]] // number
166167
| ['number-format', number | ExpressionSpecification, {'locale'?: string | ExpressionSpecification, 'currency'?: string | ExpressionSpecification, 'min-fraction-digits'?: number | ExpressionSpecification, 'max-fraction-digits'?: number | ExpressionSpecification}] // string
167168
| ['object', unknown | ExpressionSpecification, ...(unknown | ExpressionSpecification)[]] // object

src/expression/definitions/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {ImageExpression} from './image';
2828
import {Length} from './length';
2929
import {Within} from './within';
3030
import {Distance} from './distance';
31+
import {Semiliteral} from './semiliteral';
3132

3233
import type {ExpressionRegistry} from '../expression';
3334

@@ -59,6 +60,7 @@ export const expressions: ExpressionRegistry = {
5960
'number': Assertion,
6061
'number-format': NumberFormat,
6162
'object': Assertion,
63+
'semiliteral': Semiliteral,
6264
'slice': Slice,
6365
'step': Step,
6466
'string': Assertion,
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import {
2+
ObjectType,
3+
ValueType,
4+
array,
5+
} from '../types';
6+
7+
import type {Expression} from '../expression';
8+
import type {ParsingContext} from '../parsing_context';
9+
import type {EvaluationContext} from '../evaluation_context';
10+
import type {Type} from '../types';
11+
import {typeOf, Value, isValue} from '../values';
12+
import {Literal} from './literal';
13+
import exp from 'constants';
14+
15+
export abstract class Semiliteral implements Expression {
16+
type: Type;
17+
18+
constructor(type: Type) {
19+
this.type = type;
20+
}
21+
22+
static parse(args: ReadonlyArray<unknown>, context: ParsingContext): Expression {
23+
if (args.length !== 2)
24+
return context.error(`'semiliteral' expression requires exactly one argument, but found ${args.length - 1} instead.`) as null;
25+
26+
if (!isValue(args[1]))
27+
return context.error('invalid value') as null;
28+
29+
const value = args[1] as Value;
30+
const type = typeOf(value);
31+
32+
if (type.kind === 'array') {
33+
const arr = value as Array<unknown>;
34+
const parsed = arr.map(item => context.parse(item, null, ValueType));
35+
return new ArraySemiliteral(parsed);
36+
} else if (type.kind === 'object') {
37+
const obj = value as Record<string, unknown>;
38+
const parsed = Object.keys(obj).reduce((acc, key) => {
39+
acc[key] = context.parse(obj[key], null, ValueType);
40+
return acc;
41+
}, {} as Record<string, Expression>);
42+
return new ObjectSemiliteral(parsed);
43+
} else {
44+
return new Literal(type, value);
45+
}
46+
}
47+
48+
abstract evaluate(ctx: EvaluationContext): Value;
49+
50+
abstract eachChild(fn: (_: Expression) => void): void;
51+
52+
abstract outputDefined(): boolean;
53+
}
54+
55+
class ArraySemiliteral extends Semiliteral {
56+
arr: Array<Expression>;
57+
58+
constructor(arr: Array<Expression>) {
59+
let elementType: Type | null = null;
60+
for (const expr of arr) {
61+
if (!elementType) {
62+
elementType = expr.type;
63+
} else if (elementType === expr.type) {
64+
continue;
65+
} else {
66+
elementType = ValueType;
67+
break;
68+
}
69+
}
70+
super(array(elementType ?? ValueType, arr.length));
71+
this.arr = arr;
72+
}
73+
74+
evaluate(ctx: EvaluationContext): Array<Value> {
75+
return this.arr.map(arg => arg.evaluate(ctx));
76+
}
77+
78+
eachChild(fn: (_: Expression) => void) {
79+
this.arr.forEach(fn);
80+
}
81+
82+
outputDefined() {
83+
return this.arr.every(arg => arg.outputDefined());
84+
}
85+
}
86+
87+
class ObjectSemiliteral extends Semiliteral {
88+
obj: Record<string, Expression>;
89+
90+
constructor(obj: Record<string, Expression>) {
91+
super(ObjectType);
92+
this.obj = obj;
93+
}
94+
95+
evaluate(ctx: EvaluationContext): Record<string, Value> {
96+
return Object.keys(this.obj).reduce((acc, key) => {
97+
acc[key] = this.obj[key].evaluate(ctx);
98+
return acc;
99+
}, {} as Record<string, Value>);
100+
}
101+
102+
eachChild(fn: (_: Expression) => void) {
103+
Object.values(this.obj).forEach(fn);
104+
}
105+
106+
outputDefined() {
107+
return Object.values(this.obj).every(arg => arg.outputDefined());
108+
}
109+
}

src/expression/expression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface Expression {
1818

1919
export type ExpressionParser = (args: ReadonlyArray<unknown>, context: ParsingContext) => Expression;
2020
export type ExpressionRegistration = {
21-
new (...args: any): Expression;
21+
prototype: Expression;
2222
} & {
2323
readonly parse: ExpressionParser;
2424
};

src/reference/v8.json

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,7 +2830,7 @@
28302830
}
28312831
},
28322832
"literal": {
2833-
"doc": "Provides a literal array or object value.\n\n - [Display and style rich text labels](https://maplibre.org/maplibre-gl-js/docs/examples/display-and-style-rich-text-labels/)",
2833+
"doc": "Provides a literal array or object value, treating nested values as literals themselves.\n\n - [Display and style rich text labels](https://maplibre.org/maplibre-gl-js/docs/examples/display-and-style-rich-text-labels/)",
28342834
"example": {
28352835
"syntax": {
28362836
"method": ["JSON object or array"],
@@ -2847,6 +2847,24 @@
28472847
}
28482848
}
28492849
},
2850+
"semiliteral": {
2851+
"doc": "Provides an array or object value, evaluating expressions in the elements of the array or values of the object.",
2852+
"example": {
2853+
"syntax": {
2854+
"method": ["JSON object or array"],
2855+
"result": "array | object"
2856+
},
2857+
"value": ["semiliteral",[["get", "label_x"], ["get", "label_y"]]]
2858+
},
2859+
"group": "Types",
2860+
"sdk-support": {
2861+
"basic functionality": {
2862+
"js": "TODO",
2863+
"android": "TODO",
2864+
"ios": "TODO"
2865+
}
2866+
}
2867+
},
28502868
"array": {
28512869
"doc": "Asserts that the input is an array (optionally with a specific item type and length). If, when the input expression is evaluated, it is not of the asserted type, then this assertion will cause the whole expression to be aborted.",
28522870
"example": {
@@ -6660,4 +6678,4 @@
66606678
"doc": "A name of a feature property to use as ID for feature state."
66616679
}
66626680
}
6663-
}
6681+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"expression": [
3+
"semiliteral",
4+
[
5+
["round", ["*", ["cos", ["get", "direction"]], ["get", "magnitude"]]],
6+
["round", ["*", ["sin", ["get", "direction"]], ["get", "magnitude"]]]
7+
]
8+
],
9+
"inputs": [
10+
[
11+
{
12+
},
13+
{
14+
"properties": {
15+
"direction": 0,
16+
"magnitude": 10
17+
}
18+
}
19+
]
20+
],
21+
"expected": {
22+
"compiled": {
23+
"result": "success",
24+
"isFeatureConstant": false,
25+
"isZoomConstant": true,
26+
"type": "array<number, 2>"
27+
},
28+
"outputs": [
29+
[
30+
10,
31+
0
32+
]
33+
]
34+
}
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"expression": [
3+
"semiliteral",
4+
[
5+
["number", ["get", "x"]],
6+
["number", ["get", "y"]]
7+
]
8+
],
9+
"inputs": [
10+
[
11+
{},
12+
{
13+
"properties": {
14+
"x": 1,
15+
"y": 2
16+
}
17+
}
18+
]
19+
],
20+
"expected": {
21+
"compiled": {
22+
"result": "success",
23+
"isFeatureConstant": false,
24+
"isZoomConstant": true,
25+
"type": "array<number, 2>"
26+
},
27+
"outputs": [
28+
[
29+
1,
30+
2
31+
]
32+
]
33+
}
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"expression": [
3+
"semiliteral",
4+
[
5+
1,
6+
2
7+
]
8+
],
9+
"inputs": [
10+
[
11+
{},
12+
{}
13+
]
14+
],
15+
"expected": {
16+
"compiled": {
17+
"result": "success",
18+
"isFeatureConstant": true,
19+
"isZoomConstant": true,
20+
"type": "array<number, 2>"
21+
},
22+
"outputs": [
23+
[
24+
1,
25+
2
26+
]
27+
]
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"expression": [
3+
"let",
4+
"dp_per_em",
5+
12,
6+
[
7+
"semiliteral",
8+
[
9+
["/", 6, ["var", "dp_per_em"]],
10+
["/", 18, ["var", "dp_per_em"]]
11+
]
12+
]
13+
],
14+
"inputs": [
15+
[
16+
{
17+
},
18+
{
19+
}
20+
]
21+
],
22+
"expected": {
23+
"compiled": {
24+
"result": "success",
25+
"isFeatureConstant": true,
26+
"isZoomConstant": true,
27+
"type": "array<number, 2>"
28+
},
29+
"outputs": [
30+
[
31+
0.5,
32+
1.5
33+
]
34+
]
35+
}
36+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"expression": [
3+
"semiliteral",
4+
[
5+
"x",
6+
"y"
7+
]
8+
],
9+
"inputs": [
10+
[
11+
{},
12+
{}
13+
]
14+
],
15+
"expected": {
16+
"compiled": {
17+
"result": "success",
18+
"isFeatureConstant": true,
19+
"isZoomConstant": true,
20+
"type": "array<string, 2>"
21+
},
22+
"outputs": [
23+
[
24+
"x",
25+
"y"
26+
]
27+
]
28+
}
29+
}

0 commit comments

Comments
 (0)