Skip to content

Commit

Permalink
Merge ca81d9e into c7b1b9e
Browse files Browse the repository at this point in the history
  • Loading branch information
ajduberstein committed Dec 11, 2019
2 parents c7b1b9e + ca81d9e commit cc399ec
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 94 deletions.
29 changes: 16 additions & 13 deletions modules/json/src/helpers/convert-functions.js
@@ -1,27 +1,30 @@
import parseExpressionString from './parse-expression-string';
import {getPropTypes, isFunctionProp} from './deck-prop-types';

// Try to determine if any props are function valued
// and if so convert their string values to functions
export default function convertFunctions(Class, props, configuration) {
const propTypes = getPropTypes(Class);
import {FUNCTION_IDENTIFIER} from '../syntactic-sugar';

// Use deck.gl prop types if available.
return convertFunctionsUsingPropTypes(props, propTypes, configuration);
function hasFunctionIdentifier(value) {
return typeof value === 'string' && value.startsWith(FUNCTION_IDENTIFIER);
}

function trimFunctionIdentifier(value) {
return value.replace(FUNCTION_IDENTIFIER, '');
}

function convertFunctionsUsingPropTypes(props, propTypes, configuration) {
// Try to determine if any props are function valued
// and if so convert their string values to functions
export default function convertFunctions(props, configuration) {
// Use deck.gl prop types if available.
const replacedProps = {};
for (const propName in props) {
let propValue = props[propName];

// Parse string valued expressions
const isFunction = isFunctionProp(propTypes, propName);
const isFunction = hasFunctionIdentifier(propValue);

// Parse string as "expression", return equivalent JavaScript function
if (isFunction && typeof propValue === 'string') {
const isAccessor = true;
propValue = parseExpressionString(propValue, configuration, isAccessor);
if (isFunction) {
// Parse string as "expression", return equivalent JavaScript function
propValue = trimFunctionIdentifier(propValue);
propValue = parseExpressionString(propValue, configuration);
}

// Invalid functions return null, show default value instead.
Expand Down
31 changes: 0 additions & 31 deletions modules/json/src/helpers/deck-prop-types.js

This file was deleted.

6 changes: 2 additions & 4 deletions modules/json/src/helpers/instantiate-class.js
Expand Up @@ -27,9 +27,7 @@ function instantiateJavaScriptClass(Class, props, configuration) {
if (configuration.preProcessClassProps) {
props = configuration.preProcessClassProps(Class, props, configuration);
}

props = convertFunctions(Class, props, configuration);

props = convertFunctions(props, configuration);
return new Class(props);
}

Expand All @@ -41,7 +39,7 @@ function instantiateReactComponent(Component, props, configuration) {
props = configuration.preProcessClassProps(Component, props, configuration);
}

props = convertFunctions(Component, props, configuration);
props = convertFunctions(props, configuration);

return React.createElement(Component, props, children);
}
6 changes: 3 additions & 3 deletions modules/json/src/json-configuration.js
Expand Up @@ -2,14 +2,14 @@
import parseExpressionString from './helpers/parse-expression-string';
import assert from './utils/assert';

const DEFAULT_TYPE_KEY = '@@type';
import {TYPE_KEY} from './syntactic-sugar';

const isObject = value => value && typeof value === 'object';

export default class JSONConfiguration {
constructor(...configurations) {
// Initialize config with default values
this.typeKey = DEFAULT_TYPE_KEY;
this.typeKey = TYPE_KEY;
this.log = console; // eslint-disable-line
this.classes = {};
this.reactComponents = {};
Expand Down Expand Up @@ -50,6 +50,6 @@ export default class JSONConfiguration {
}
}

function convertFunction(value, key, configuration) {
function convertFunction(value, configuration) {
return parseExpressionString(value, configuration);
}
11 changes: 6 additions & 5 deletions modules/json/src/json-converter.js
Expand Up @@ -11,11 +11,11 @@
import assert from './utils/assert';
import JSONConfiguration from './json-configuration';
import {instantiateClass} from './helpers/instantiate-class';

import {FUNCTION_IDENTIFIER, CONSTANT_IDENTIFIER} from './syntactic-sugar';
import parseJSON from './helpers/parse-json';

const isObject = value => value && typeof value === 'object';
const FUNCTION_IDENTIFIER = '@@=';
const CONSTANT_IDENTIFIER = '@@#';

export default class JSONConverter {
constructor(props) {
Expand Down Expand Up @@ -82,7 +82,7 @@ function convertJSONRecursively(json, key, configuration) {
return json.map((element, i) => convertJSONRecursively(element, String(i), configuration));
}

// If object.type is in configuration, instantitate
// If object.type is in configuration, instantiate
if (isClassInstance(json, configuration)) {
return convertClassInstance(json, configuration);
}
Expand All @@ -103,7 +103,8 @@ function convertJSONRecursively(json, key, configuration) {
// Returns true if an object has a `type` field
function isClassInstance(json, configuration) {
const {typeKey} = configuration;
return isObject(json) && Boolean(json[typeKey]);
const isClass = isObject(json) && Boolean(json[typeKey]);
return isClass;
}

function convertClassInstance(json, configuration) {
Expand Down Expand Up @@ -139,7 +140,7 @@ function convertString(string, key, configuration) {
// Here the JSON value is supposed to be treated as a function
if (string.startsWith(FUNCTION_IDENTIFIER) && configuration.convertFunction) {
string = string.replace(FUNCTION_IDENTIFIER, '');
return configuration.convertFunction(string, key, configuration);
return configuration.convertFunction(string, configuration);
}
if (string.startsWith(CONSTANT_IDENTIFIER)) {
string = string.replace(CONSTANT_IDENTIFIER, '');
Expand Down
5 changes: 5 additions & 0 deletions modules/json/src/syntactic-sugar.js
@@ -0,0 +1,5 @@
const FUNCTION_IDENTIFIER = '@@=';
const CONSTANT_IDENTIFIER = '@@#';
const TYPE_KEY = '@@type';

export {FUNCTION_IDENTIFIER, CONSTANT_IDENTIFIER, TYPE_KEY};
2 changes: 1 addition & 1 deletion test/modules/json/data/deck-props.json
Expand Up @@ -51,7 +51,7 @@
0,
3000
],
"getText": "Hello World",
"getText": "@@=Hello World",
"billboard": false
}
]
Expand Down
35 changes: 19 additions & 16 deletions test/modules/json/helpers/convert-functions.spec.js
Expand Up @@ -75,27 +75,30 @@ const TEST_CASES = [
{expr: 'this.three', expected: 3}
];

test('convertFunctions#get...', t => {
test('convertFunctions#asStrings', t => {
const props = {};
TEST_CASES.forEach((testCase, i) => {
// Add a mix of function and value props
// TODO this is based on `get` heuristic, we have better ideas and will update tests when we address
const propName = i % 2 === 0 ? `get-${i}` : `value-${i}`;
props[propName] = testCase.expr;
props[`value-{i}`] = testCase.expr;
});
const convertedProps = convertFunctions(null, props, {});
const convertedProps = convertFunctions(props, {});
for (const key in convertedProps) {
if (key.startsWith('get')) {
t.ok(
typeof convertedProps[key] === 'function',
'convertFunctions converted prop to function'
);
} else {
t.ok(
typeof convertedProps[key] !== 'function',
'convertFunctions did not converted string to function'
);
}
t.ok(
typeof convertedProps[key] !== 'function',
'convertFunctions did not convert string to function'
);
}
t.end();
});

test('convertFunctions#asFunctions', t => {
const props = {};
TEST_CASES.forEach((testCase, i) => {
props[`func-{i}`] = `@@=${testCase.expr}`;
});
const convertedProps = convertFunctions(props, {});
for (const key in convertedProps) {
t.ok(typeof convertedProps[key] === 'function', 'convertFunctions converted prop to function');
}
t.end();
});
12 changes: 0 additions & 12 deletions test/modules/json/helpers/deck-prop-types.spec.js

This file was deleted.

1 change: 0 additions & 1 deletion test/modules/json/index.js
@@ -1,7 +1,6 @@
import './utils/get.spec';
import './utils/shallow-equal-objects.spec';

import './helpers/deck-prop-types.spec';
import './helpers/parse-expression-string.spec';
import './helpers/convert-functions.spec';

Expand Down
2 changes: 1 addition & 1 deletion test/modules/json/json-render.spec.js
Expand Up @@ -11,7 +11,7 @@ test('JSONConverter#render', t => {

const deckProps = jsonConverter.convert(JSON_DATA);
t.ok(deckProps, 'JSONConverter converted correctly');

console.log({deckProps}); // eslint-disable-line
const jsonDeck = new Deck(
Object.assign(
{
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cc399ec

Please sign in to comment.