From 23f0dbe1aa817793aa806123fc76b591c80a0163 Mon Sep 17 00:00:00 2001 From: Sargun Vohra Date: Sun, 22 Dec 2024 17:19:18 -0500 Subject: [PATCH 1/6] feat: add semiliteral operator for expressions inside arrays and objects --- build/generate-style-spec.ts | 1 + src/expression/definitions/index.ts | 2 + src/expression/definitions/semiliteral.ts | 109 ++++++++++++++++++ src/expression/expression.ts | 2 +- src/reference/v8.json | 22 +++- .../semiliteral/directional-offset/test.json | 35 ++++++ .../get-offset-components/test.json | 34 ++++++ .../tests/semiliteral/number-array/test.json | 29 +++++ .../tests/semiliteral/scale-offset/test.json | 36 ++++++ .../tests/semiliteral/string-array/test.json | 29 +++++ 10 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 src/expression/definitions/semiliteral.ts create mode 100644 test/integration/expression/tests/semiliteral/directional-offset/test.json create mode 100644 test/integration/expression/tests/semiliteral/get-offset-components/test.json create mode 100644 test/integration/expression/tests/semiliteral/number-array/test.json create mode 100644 test/integration/expression/tests/semiliteral/scale-offset/test.json create mode 100644 test/integration/expression/tests/semiliteral/string-array/test.json diff --git a/build/generate-style-spec.ts b/build/generate-style-spec.ts index 8c3c6387c..593b7ee6f 100644 --- a/build/generate-style-spec.ts +++ b/build/generate-style-spec.ts @@ -162,6 +162,7 @@ export type ExpressionSpecification = | ['format', ...(string | ['image', ExpressionSpecification] | ExpressionSpecification | {'font-scale'?: number | ExpressionSpecification, 'text-font'?: string[] | ExpressionSpecification, 'text-color'?: ColorSpecification | ExpressionSpecification})[]] // string | ['image', unknown | ExpressionSpecification] // image | ['literal', unknown] + | ['semiliteral', unknown] | ['number', unknown | ExpressionSpecification, ...(unknown | ExpressionSpecification)[]] // number | ['number-format', number | ExpressionSpecification, {'locale'?: string | ExpressionSpecification, 'currency'?: string | ExpressionSpecification, 'min-fraction-digits'?: number | ExpressionSpecification, 'max-fraction-digits'?: number | ExpressionSpecification}] // string | ['object', unknown | ExpressionSpecification, ...(unknown | ExpressionSpecification)[]] // object diff --git a/src/expression/definitions/index.ts b/src/expression/definitions/index.ts index e00374024..2bd293dd0 100644 --- a/src/expression/definitions/index.ts +++ b/src/expression/definitions/index.ts @@ -28,6 +28,7 @@ import {ImageExpression} from './image'; import {Length} from './length'; import {Within} from './within'; import {Distance} from './distance'; +import {Semiliteral} from './semiliteral'; import type {ExpressionRegistry} from '../expression'; @@ -59,6 +60,7 @@ export const expressions: ExpressionRegistry = { 'number': Assertion, 'number-format': NumberFormat, 'object': Assertion, + 'semiliteral': Semiliteral, 'slice': Slice, 'step': Step, 'string': Assertion, diff --git a/src/expression/definitions/semiliteral.ts b/src/expression/definitions/semiliteral.ts new file mode 100644 index 000000000..2e31d5f2f --- /dev/null +++ b/src/expression/definitions/semiliteral.ts @@ -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, 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; + const parsed = arr.map(item => context.parse(item, null, ValueType)); + return new ArraySemiliteral(parsed); + } else if (type.kind === 'object') { + const obj = value as Record; + const parsed = Object.keys(obj).reduce((acc, key) => { + acc[key] = context.parse(obj[key], null, ValueType); + return acc; + }, {} as Record); + 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; + + constructor(arr: Array) { + 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 { + 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; + + constructor(obj: Record) { + super(ObjectType); + this.obj = obj; + } + + evaluate(ctx: EvaluationContext): Record { + return Object.keys(this.obj).reduce((acc, key) => { + acc[key] = this.obj[key].evaluate(ctx); + return acc; + }, {} as Record); + } + + eachChild(fn: (_: Expression) => void) { + Object.values(this.obj).forEach(fn); + } + + outputDefined() { + return Object.values(this.obj).every(arg => arg.outputDefined()); + } +} diff --git a/src/expression/expression.ts b/src/expression/expression.ts index 0e62c4a49..db5d3dfcb 100644 --- a/src/expression/expression.ts +++ b/src/expression/expression.ts @@ -18,7 +18,7 @@ export interface Expression { export type ExpressionParser = (args: ReadonlyArray, context: ParsingContext) => Expression; export type ExpressionRegistration = { - new (...args: any): Expression; + prototype: Expression; } & { readonly parse: ExpressionParser; }; diff --git a/src/reference/v8.json b/src/reference/v8.json index 427803edc..77f15cfa8 100644 --- a/src/reference/v8.json +++ b/src/reference/v8.json @@ -2830,7 +2830,7 @@ } }, "literal": { - "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/)", + "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/)", "example": { "syntax": { "method": ["JSON object or array"], @@ -2847,6 +2847,24 @@ } } }, + "semiliteral": { + "doc": "Provides an array or object value, evaluating expressions in the elements of the array or values of the object.", + "example": { + "syntax": { + "method": ["JSON object or array"], + "result": "array | object" + }, + "value": ["semiliteral",[["get", "label_x"], ["get", "label_y"]]] + }, + "group": "Types", + "sdk-support": { + "basic functionality": { + "js": "TODO", + "android": "TODO", + "ios": "TODO" + } + } + }, "array": { "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.", "example": { @@ -6660,4 +6678,4 @@ "doc": "A name of a feature property to use as ID for feature state." } } -} \ No newline at end of file +} diff --git a/test/integration/expression/tests/semiliteral/directional-offset/test.json b/test/integration/expression/tests/semiliteral/directional-offset/test.json new file mode 100644 index 000000000..04edf9e76 --- /dev/null +++ b/test/integration/expression/tests/semiliteral/directional-offset/test.json @@ -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" + }, + "outputs": [ + [ + 10, + 0 + ] + ] + } +} diff --git a/test/integration/expression/tests/semiliteral/get-offset-components/test.json b/test/integration/expression/tests/semiliteral/get-offset-components/test.json new file mode 100644 index 000000000..7c8dc57d0 --- /dev/null +++ b/test/integration/expression/tests/semiliteral/get-offset-components/test.json @@ -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" + }, + "outputs": [ + [ + 1, + 2 + ] + ] + } +} diff --git a/test/integration/expression/tests/semiliteral/number-array/test.json b/test/integration/expression/tests/semiliteral/number-array/test.json new file mode 100644 index 000000000..8d555262f --- /dev/null +++ b/test/integration/expression/tests/semiliteral/number-array/test.json @@ -0,0 +1,29 @@ +{ + "expression": [ + "semiliteral", + [ + 1, + 2 + ] + ], + "inputs": [ + [ + {}, + {} + ] + ], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "array" + }, + "outputs": [ + [ + 1, + 2 + ] + ] + } +} diff --git a/test/integration/expression/tests/semiliteral/scale-offset/test.json b/test/integration/expression/tests/semiliteral/scale-offset/test.json new file mode 100644 index 000000000..0625efc6f --- /dev/null +++ b/test/integration/expression/tests/semiliteral/scale-offset/test.json @@ -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" + }, + "outputs": [ + [ + 0.5, + 1.5 + ] + ] + } +} diff --git a/test/integration/expression/tests/semiliteral/string-array/test.json b/test/integration/expression/tests/semiliteral/string-array/test.json new file mode 100644 index 000000000..f3d3fce0d --- /dev/null +++ b/test/integration/expression/tests/semiliteral/string-array/test.json @@ -0,0 +1,29 @@ +{ + "expression": [ + "semiliteral", + [ + "x", + "y" + ] + ], + "inputs": [ + [ + {}, + {} + ] + ], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "array" + }, + "outputs": [ + [ + "x", + "y" + ] + ] + } +} From c0e47660f0af830c5beb6107955b6d357be96180 Mon Sep 17 00:00:00 2001 From: Sargun Vohra Date: Sun, 22 Dec 2024 17:30:24 -0500 Subject: [PATCH 2/6] add tracking issues --- src/reference/v8.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/reference/v8.json b/src/reference/v8.json index 77f15cfa8..ff6af861c 100644 --- a/src/reference/v8.json +++ b/src/reference/v8.json @@ -2859,9 +2859,9 @@ "group": "Types", "sdk-support": { "basic functionality": { - "js": "TODO", - "android": "TODO", - "ios": "TODO" + "js": "https://github.com/maplibre/maplibre-style-spec/issues/950", + "android": "https://github.com/maplibre/maplibre-style-spec/issues/950", + "ios": "https://github.com/maplibre/maplibre-style-spec/issues/950" } } }, From fa501a0b3f76249067eff7f1a334789521a09423 Mon Sep 17 00:00:00 2001 From: Sargun Vohra Date: Sun, 22 Dec 2024 17:41:34 -0500 Subject: [PATCH 3/6] add some tests --- src/expression/definitions/semiliteral.ts | 1 - .../tests/semiliteral/empty/test.json | 22 ++++++++++++++++ .../tests/semiliteral/extra-arg/test.json | 22 ++++++++++++++++ .../tests/semiliteral/string/test.json | 25 +++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 test/integration/expression/tests/semiliteral/empty/test.json create mode 100644 test/integration/expression/tests/semiliteral/extra-arg/test.json create mode 100644 test/integration/expression/tests/semiliteral/string/test.json diff --git a/src/expression/definitions/semiliteral.ts b/src/expression/definitions/semiliteral.ts index 2e31d5f2f..d50497345 100644 --- a/src/expression/definitions/semiliteral.ts +++ b/src/expression/definitions/semiliteral.ts @@ -10,7 +10,6 @@ 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; diff --git a/test/integration/expression/tests/semiliteral/empty/test.json b/test/integration/expression/tests/semiliteral/empty/test.json new file mode 100644 index 000000000..9802a3130 --- /dev/null +++ b/test/integration/expression/tests/semiliteral/empty/test.json @@ -0,0 +1,22 @@ +{ + "expression": ["semiliteral"], + "inputs": [ + [ + { + }, + { + } + ] + ], + "expected": { + "compiled": { + "result": "error", + "errors": [ + { + "key": "", + "error": "'semiliteral' expression requires exactly one argument, but found 0 instead." + } + ] + } + } +} diff --git a/test/integration/expression/tests/semiliteral/extra-arg/test.json b/test/integration/expression/tests/semiliteral/extra-arg/test.json new file mode 100644 index 000000000..f19063904 --- /dev/null +++ b/test/integration/expression/tests/semiliteral/extra-arg/test.json @@ -0,0 +1,22 @@ +{ + "expression": ["semiliteral", [], []], + "inputs": [ + [ + { + }, + { + } + ] + ], + "expected": { + "compiled": { + "result": "error", + "errors": [ + { + "key": "", + "error": "'semiliteral' expression requires exactly one argument, but found 2 instead." + } + ] + } + } +} diff --git a/test/integration/expression/tests/semiliteral/string/test.json b/test/integration/expression/tests/semiliteral/string/test.json new file mode 100644 index 000000000..a0f455569 --- /dev/null +++ b/test/integration/expression/tests/semiliteral/string/test.json @@ -0,0 +1,25 @@ +{ + "expression": [ + "semiliteral", + "example" + ], + "inputs": [ + [ + { + }, + { + } + ] + ], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "string" + }, + "outputs": [ + "example" + ] + } +} From 536caf40d547a1f01e62276502f88f1799371d8c Mon Sep 17 00:00:00 2001 From: Sargun Vohra Date: Sun, 22 Dec 2024 17:45:57 -0500 Subject: [PATCH 4/6] add some tests --- .../semiliteral/mixed-expressions/test.json | 29 +++++++++++++++++++ .../semiliteral/mixed-primitives/test.json | 29 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 test/integration/expression/tests/semiliteral/mixed-expressions/test.json create mode 100644 test/integration/expression/tests/semiliteral/mixed-primitives/test.json diff --git a/test/integration/expression/tests/semiliteral/mixed-expressions/test.json b/test/integration/expression/tests/semiliteral/mixed-expressions/test.json new file mode 100644 index 000000000..e8192e2ee --- /dev/null +++ b/test/integration/expression/tests/semiliteral/mixed-expressions/test.json @@ -0,0 +1,29 @@ +{ + "expression": [ + "semiliteral", + [ + ["*", 2, 3], + ["concat", "x", "y"] + ] + ], + "inputs": [ + [ + {}, + {} + ] + ], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "array" + }, + "outputs": [ + [ + 6, + "xy" + ] + ] + } +} diff --git a/test/integration/expression/tests/semiliteral/mixed-primitives/test.json b/test/integration/expression/tests/semiliteral/mixed-primitives/test.json new file mode 100644 index 000000000..976861f19 --- /dev/null +++ b/test/integration/expression/tests/semiliteral/mixed-primitives/test.json @@ -0,0 +1,29 @@ +{ + "expression": [ + "semiliteral", + [ + "x", + 1 + ] + ], + "inputs": [ + [ + {}, + {} + ] + ], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "array" + }, + "outputs": [ + [ + "x", + 1 + ] + ] + } +} From 061696c44f39fda672e00c3479864600ce341d3b Mon Sep 17 00:00:00 2001 From: Sargun Vohra Date: Sun, 22 Dec 2024 17:49:33 -0500 Subject: [PATCH 5/6] add some tests --- .../semiliteral/object-expressions/test.json | 31 +++++++++++++++++++ .../semiliteral/object-primitives/test.json | 27 ++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 test/integration/expression/tests/semiliteral/object-expressions/test.json create mode 100644 test/integration/expression/tests/semiliteral/object-primitives/test.json diff --git a/test/integration/expression/tests/semiliteral/object-expressions/test.json b/test/integration/expression/tests/semiliteral/object-expressions/test.json new file mode 100644 index 000000000..19afc03c7 --- /dev/null +++ b/test/integration/expression/tests/semiliteral/object-expressions/test.json @@ -0,0 +1,31 @@ +{ + "expression": [ + "semiliteral", + { + "x": ["get", "x"] + } + ], + "inputs": [ + [ + {}, + { + "properties": { + "x": 1 + } + } + ] + ], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": false, + "isZoomConstant": true, + "type": "object" + }, + "outputs": [ + { + "x": 1 + } + ] + } +} diff --git a/test/integration/expression/tests/semiliteral/object-primitives/test.json b/test/integration/expression/tests/semiliteral/object-primitives/test.json new file mode 100644 index 000000000..4d06f5fe6 --- /dev/null +++ b/test/integration/expression/tests/semiliteral/object-primitives/test.json @@ -0,0 +1,27 @@ +{ + "expression": [ + "semiliteral", + { + "x": 1 + } + ], + "inputs": [ + [ + {}, + {} + ] + ], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "object" + }, + "outputs": [ + { + "x": 1 + } + ] + } +} From c4e7d47421b88b15037bda4275ec2940a0e573d8 Mon Sep 17 00:00:00 2001 From: Sargun Vohra Date: Sun, 22 Dec 2024 17:50:46 -0500 Subject: [PATCH 6/6] add some tests --- .../tests/semiliteral/empty-array/test.json | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test/integration/expression/tests/semiliteral/empty-array/test.json diff --git a/test/integration/expression/tests/semiliteral/empty-array/test.json b/test/integration/expression/tests/semiliteral/empty-array/test.json new file mode 100644 index 000000000..9ec503455 --- /dev/null +++ b/test/integration/expression/tests/semiliteral/empty-array/test.json @@ -0,0 +1,25 @@ +{ + "expression": [ + "semiliteral", + [] + ], + "inputs": [ + [ + {}, + { + } + ] + ], + "expected": { + "compiled": { + "result": "success", + "isFeatureConstant": true, + "isZoomConstant": true, + "type": "array" + }, + "outputs": [ + [ + ] + ] + } +}