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 14, 2018
1 parent 3b480cd commit 85fce01
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
10 changes: 0 additions & 10 deletions src/style-spec/expression/definitions/coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type ParsingContext from '../parsing_context';
import type EvaluationContext from '../evaluation_context';
import type { Value } from '../values';
import type { Type } from '../types';
import { Formatted, FormattedSection } from './formatted';

const types = {
'to-boolean': BooleanType,
Expand Down Expand Up @@ -84,15 +83,6 @@ class Coercion implements Expression {
}
}
throw new RuntimeError(error || `Could not parse color from value '${typeof input === 'string' ? input : JSON.stringify(input)}'`);
} else if (this.type.kind === 'formatted') {
let input;
for (const arg of this.args) {
input = arg.evaluate(ctx);
if (typeof input === 'string') {
return new Formatted([new FormattedSection(input, null, null)]);
}
}
throw new RuntimeError(`Could not parse formatted text from value '${typeof input === 'string' ? input : JSON.stringify(input)}'`);
} else if (this.type.kind === 'number') {
let value = null;
for (const arg of this.args) {
Expand Down
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 85fce01

Please sign in to comment.