Skip to content

Commit

Permalink
Fix implicit conversions for "formatted" type
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Sep 13, 2018
1 parent 3b480cd commit 77a1d50
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/style-spec/expression/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,22 +351,23 @@ function findZoomCurve(expression: Expression): Step | Interpolate | ParsingErro
return result;
}

import { ColorType, StringType, NumberType, BooleanType, ValueType, array } from './types';
import { ColorType, StringType, NumberType, BooleanType, ValueType, FormattedType, array } from './types';

function getExpectedType(spec: StylePropertySpecification): Type | null {
function getExpectedType(spec: StylePropertySpecification): Type {
const types = {
color: ColorType,
string: StringType,
number: NumberType,
enum: StringType,
boolean: BooleanType
boolean: BooleanType,
formatted: FormattedType
};

if (spec.type === 'array') {
return array(types[spec.value] || ValueType, spec.length);
}

return types[spec.type] || null;
return types[spec.type];
}

function getDefaultValue(spec: StylePropertySpecification): Value {
Expand Down
8 changes: 6 additions & 2 deletions src/style-spec/expression/parsing_context.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow

import assert from 'assert';
import Scope from './scope';

import { checkSubtype } from './types';
import ParsingError from './parsing_error';
import Literal from './definitions/literal';
Expand All @@ -16,6 +16,7 @@ import Var from './definitions/var';

import type {Expression, ExpressionRegistry} from './expression';
import type {Type} from './types';
import {FormatExpression} from './definitions/formatted';

/**
* State associated parsing at a given point in an expression tree.
Expand Down Expand Up @@ -114,8 +115,11 @@ class ParsingContext {
//
if ((expected.kind === 'string' || expected.kind === 'number' || expected.kind === 'boolean' || expected.kind === 'object' || expected.kind === 'array') && actual.kind === 'value') {
parsed = annotate(parsed, expected, options.typeAnnotation || 'assert');
} else if ((expected.kind === 'color' || expected.kind === 'formatted') && (actual.kind === 'value' || actual.kind === 'string')) {
} else if (expected.kind === 'color' && (actual.kind === 'value' || actual.kind === 'string')) {
parsed = annotate(parsed, expected, options.typeAnnotation || 'coerce');
} else if (expected.kind === 'formatted' && actual.kind !== 'formatted') {
assert(!options.typeAnnotation);
parsed = new FormatExpression([{text: parsed, scale: null, font: null}]);
} else if (this.checkSubtype(expected, actual)) {
return null;
}
Expand Down
53 changes: 53 additions & 0 deletions test/integration/expression-tests/format/implicit/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"expression": ["get", "p"],
"propertySpec": {
"type": "formatted"
},
"inputs": [
[{}, {"properties": {}}],
[{}, {"properties": {"p": 0}}],
[{}, {"properties": {"p": "a"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "formatted"
},
"outputs": [
{
"sections": [
{
"text": "",
"scale": null,
"fontStack": null
}
]
},
{
"sections": [
{
"text": "0",
"scale": null,
"fontStack": null
}
]
},
{
"sections": [
{
"text": "a",
"scale": null,
"fontStack": null
}
]
}
],
"serialized": [
"format",
["get", "p"],
{}
]
}
}

0 comments on commit 77a1d50

Please sign in to comment.