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..d50497345 --- /dev/null +++ b/src/expression/definitions/semiliteral.ts @@ -0,0 +1,108 @@ +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'; + +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..ff6af861c 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": "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" + } + } + }, "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/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": [ + [ + ] + ] + } +} 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/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/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 + ] + ] + } +} 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/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 + } + ] + } +} 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" + ] + ] + } +} 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" + ] + } +}