Skip to content

Rework spec function/expression taxonomy - #6505

Merged
lbud merged 3 commits into
masterfrom
expression-spec
Apr 16, 2018
Merged

lbud merged 3 commits into
masterfrom
expression-spec

Conversation

@lbud

@lbud lbud commented Apr 12, 2018 •

Copy link
Copy Markdown
Contributor

This PR reworks the taxonomy to describe how a property can use functions/expressions, as described in #6389 (comment) and #6389 (comment):

type ExpressionType = 'data-driven' | 'cross-faded' | 'cross-faded-data-driven' | 'color-ramp' | 'data-constant' | 'constant';

type ExpressionParameters = Array<'zoom' | 'feature' | 'heatmap-density' | 'line-progress'>;

type ExpressionSpecification = {
    interpolated: boolean,
    parameters: ExpressionParameters
}

StylePropertySpecification = {
    ...
    'property-type': ExpressionType,
    'expression': ExpressionSpecification
}

so now any property all properties include a property-type key, and for all property-types except constant also an expression property, i.e.

    "type": "number",
    "property-type": "data-driven",
    "expression": {
        "interpolated": true,
        "parameters": [
            "zoom",
            "feature"
        ]
    }

Benchmarks: http://bl.ocks.org/lbud/raw/2ac3b8869074c112802166978cea8345/ (I couldn't reproduce the Layer discrepancies on subsequent isolated bench runs).

Closes #6389
Supersedes / closes #4194
Refs #6430
Refs #6303

Launch Checklist

  • briefly describe the changes in this PR
  • write tests for all new functionality
  • post benchmark scores
  • manually test the debug page

@lbud
lbud requested review from anandthakker and jfirebaugh April 12, 2018 00:04

@anandthakker anandthakker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels so much cleaner ☺️

Comment thread src/style-spec/style-spec.js Outdated
type ExpressionParameters = Array<'zoom' | 'feature' | 'heatmap-density' | 'line-progress'>;

type ExpressionSpecification = {
type: ExpressionType,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like field is currently called property-type, not type

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels slightly weird to me to have property-type nested under expression. Would it make sense to move it up to the layout/paint property specification, and also add constant as one of the options for properties that can't take any expression? E.g.:

"visibility": {
  "property-type": "constant",
  // no "expression" field
  "type": "enum",
   /* ... */
}
"line-cap": {
  "property-type": "data-constant",
  "expression": {
    "interpolated": false,
    "parameters": ["zoom"]
   },
  "type": "enum",
   /* ... */
}

Then we could always use propertySpec["property-type"] and never have to rely on the presence/absence of the "expression" field

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me @anandthakker!

Comment thread src/style-spec/util/properties.js Outdated
// @flow

export function isPropertyFunction(spec: Object): boolean {
const dataDrivenTypes = new Set(['data-driven', 'cross-faded-data-driven']);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can use Set / Map, since they'd require polyfills for IE 😢

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this would have showed up in the style-spec build test, but it's not getting run on CI 😬 #6433


export function isZoomFunction(spec: Object): boolean {
return spec.expression && spec.expression.parameters.indexOf('zoom') > -1;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to isPropertyExpression and isZoomExpression?

@lbud
lbud requested a review from anandthakker April 12, 2018 18:32
return `ColorRampProperty`;
case 'data-constant':
default:
return `DataConstantProperty<${flowType(property)}>`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for completeness, should we also list case 'constant': here? (Alternatively, since DataConstantProperty<T> isn't really intended to represent a totally constant property, it could be case 'constant': throw new Error("Constant properties unimplemented");)

return `new ColorRampProperty(styleSpec["${type}_${property.layerType}"]["${property.name}"])`;
case 'data-constant':
default:
return `new DataConstantProperty(styleSpec["${type}_${property.layerType}"]["${property.name}"])`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Comment thread src/style-spec/util/properties.js Outdated
@@ -0,0 +1,13 @@
// @flow

export function isPropertyExpression(spec: Object): boolean {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isPropertyExpression → supportsPropertyExpressions -- or something similar that doesn't imply that the function is testing whether the input itself is a property expression. (Similarly for isZoomExpression and isInterpolated.)

Also, can spec be typed as StylePropertySpecification?

Comment thread src/style-spec/function/convert.js Outdated
return parameters.type;
} else if (propertySpec.function) {
return propertySpec.function === 'interpolated' ? 'exponential' : 'interval';
} else if (propertySpec.expression) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be asserting propertySpec.expression? We shouldn't be trying to create a function for something that doesn't support expressions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 this was a bad workaround for a flow error — will fix.

@lbud
lbud force-pushed the expression-spec branch from d37c94c to caba650 Compare April 16, 2018 21:23
@lbud
lbud merged commit 77b9821 into master Apr 16, 2018
@lbud
lbud deleted the expression-spec branch April 16, 2018 21:47
lbud pushed a commit that referenced this pull request Apr 16, 2018
@lbud
lbud restored the expression-spec branch April 16, 2018 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants