Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move canvas interpreter to OSS #25711

Merged
merged 12 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ bower_components
/x-pack/coverage
/x-pack/build
/x-pack/plugins/**/__tests__/fixtures/**
/x-pack/plugins/canvas/common/lib/grammar.js
/packages/kbn-interpreter/common/lib/grammar.js
/packages/kbn-interpreter/plugin
/x-pack/plugins/canvas/canvas_plugin
/x-pack/plugins/canvas/canvas_plugin_src/lib/flot-charts
**/*.js.snap
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"@kbn/pm": "1.0.0",
"@kbn/test-subj-selector": "0.2.1",
"@kbn/ui-framework": "1.0.0",
"@kbn/interpreter": "1.0.0",
"JSONStream": "1.1.1",
"abortcontroller-polyfill": "^1.1.9",
"angular": "1.6.9",
Expand Down Expand Up @@ -384,4 +385,4 @@
"node": "8.11.4",
"yarn": "^1.10.1"
}
}
}
3 changes: 3 additions & 0 deletions packages/kbn-interpreter/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@kbn/babel-preset/webpack_preset"]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { getType } from '../lib/get_type';
Expand All @@ -19,8 +32,9 @@ export function castProvider(types) {

for (let i = 0; i < toTypeNames.length; i++) {
// First check if the current type can cast to this type
if (fromTypeDef && fromTypeDef.castsTo(toTypeNames[i]))
if (fromTypeDef && fromTypeDef.castsTo(toTypeNames[i])) {
return fromTypeDef.to(node, toTypeNames[i], types);
}

// If that isn't possible, check if this type can cast from the current type
const toTypeDef = types[toTypeNames[i]];
Expand Down
26 changes: 26 additions & 0 deletions packages/kbn-interpreter/common/interpreter/create_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export const createError = err => ({
type: 'error',
error: {
stack: process.env.NODE_ENV === 'production' ? undefined : err.stack,
message: typeof err === 'string' ? err : err.message,
},
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import clone from 'lodash.clone';
Expand Down Expand Up @@ -112,8 +125,9 @@ export function interpretProvider(config) {
(argAsts, argAst, argName) => {
const argDef = getByAlias(argDefs, argName);
// TODO: Implement a system to allow for undeclared arguments
if (!argDef)
if (!argDef) {
throw new Error(`Unknown argument '${argName}' passed to function '${fnDef.name}'`);
}

argAsts[argDef.name] = (argAsts[argDef.name] || []).concat(argAst);
return argAsts;
Expand Down Expand Up @@ -142,8 +156,9 @@ export function interpretProvider(config) {
const argAstsWithDefaults = reduce(
argDefs,
(argAsts, argDef, argName) => {
if (typeof argAsts[argName] === 'undefined' && typeof argDef.default !== 'undefined')
if (typeof argAsts[argName] === 'undefined' && typeof argDef.default !== 'undefined') {
argAsts[argName] = [fromExpression(argDef.default, 'argument')];
}

return argAsts;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import uuid from 'uuid/v4';
Expand Down Expand Up @@ -40,8 +53,9 @@ export function socketInterpreterProvider({
// Get the list of functions that are known elsewhere
return Promise.resolve(referableFunctions).then(referableFunctionMap => {
// Check if the not-found function is in the list of alternatives, if not, throw
if (!getByAlias(referableFunctionMap, functionName))
if (!getByAlias(referableFunctionMap, functionName)) {
throw new Error(`Function not found: ${functionName}`);
}

// set a unique message ID so the code knows what response to process
const id = uuid();
Expand Down
37 changes: 37 additions & 0 deletions packages/kbn-interpreter/common/lib/arg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { includes } from 'lodash';

export function Arg(config) {
if (config.name === '_') throw Error('Arg names must not be _. Use it in aliases instead.');
this.name = config.name;
this.required = config.required || false;
this.help = config.help || '';
this.types = config.types || [];
this.default = config.default;
this.aliases = config.aliases || [];
this.multi = config.multi == null ? false : config.multi;
this.resolve = config.resolve == null ? true : config.resolve;
this.options = config.options || [];
this.accepts = type => {
if (!this.types.length) return true;
return includes(config.types, type);
};
}
35 changes: 35 additions & 0 deletions packages/kbn-interpreter/common/lib/arg.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { Arg } from './arg';

describe('Arg', () => {
it('sets required to false by default', () => {
const isOptional = new Arg({
name: 'optional_me',
});
expect(isOptional.required).toBe(false);

const isRequired = new Arg({
name: 'require_me',
required: true,
});
expect(isRequired.required).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import expect from 'expect.js';
import { fromExpression } from '../ast';
import { getType } from '../../lib/get_type';
import { fromExpression } from './ast';
import { getType } from './get_type';

describe('ast fromExpression', () => {
describe('invalid expression', () => {
it('throws when empty', () => {
const check = () => fromExpression('');
expect(check).to.throwException(/Unable to parse expression/i);
expect(check).toThrowError(/Unable to parse expression/i);
});

it('throws with invalid expression', () => {
const check = () => fromExpression('wat!');
expect(check).to.throwException(/Unable to parse expression/i);
expect(check).toThrowError(/Unable to parse expression/i);
});
});

describe('single item expression', () => {
it('is a chain', () => {
const expression = 'whatever';
expect(fromExpression(expression)).to.have.property('chain');
expect(fromExpression(expression)).toHaveProperty('chain');
});

it('is a value', () => {
const expression = '"hello"';
expect(fromExpression(expression, 'argument')).to.equal('hello');
expect(fromExpression(expression, 'argument')).toBe('hello');
});

describe('function without arguments', () => {
Expand All @@ -44,15 +56,15 @@ describe('ast fromExpression', () => {
});

it('is a function ', () => {
expect(getType(block)).to.equal('function');
expect(getType(block)).toBe('function');
});

it('is csv function', () => {
expect(block.function).to.equal('csv');
expect(block.function).toBe('csv');
});

it('has no arguments', () => {
expect(block.arguments).to.eql({});
expect(block.arguments).toEqual({});
});
});

Expand All @@ -68,17 +80,17 @@ describe('ast fromExpression', () => {
});

it('has arguemnts properties', () => {
expect(block.arguments).not.to.eql({});
expect(block.arguments).not.toEqual({});
});

it('has index argument with string value', () => {
expect(block.arguments).to.have.property('index');
expect(block.arguments.index).to.eql(['logstash-*']);
expect(block.arguments).toHaveProperty('index');
expect(block.arguments.index).toEqual(['logstash-*']);
});

it('has oranges argument with string value', () => {
expect(block.arguments).to.have.property('oranges');
expect(block.arguments.oranges).to.eql(['bananas']);
expect(block.arguments).toHaveProperty('oranges');
expect(block.arguments.oranges).toEqual(['bananas']);
});
});

Expand All @@ -94,12 +106,12 @@ describe('ast fromExpression', () => {
});

it('is expression type', () => {
expect(block.arguments).to.have.property('exampleFunction');
expect(block.arguments.exampleFunction[0]).to.have.property('type', 'expression');
expect(block.arguments).toHaveProperty('exampleFunction');
expect(block.arguments.exampleFunction[0]).toHaveProperty('type');
});

it('has expected shape', () => {
expect(block.arguments.exampleFunction).to.eql([
expect(block.arguments.exampleFunction).toEqual([
{
type: 'expression',
chain: [
Expand Down Expand Up @@ -128,12 +140,12 @@ describe('ast fromExpression', () => {
});

it('is expression type', () => {
expect(block.arguments).to.have.property('examplePartial');
expect(block.arguments.examplePartial[0]).to.have.property('type', 'expression');
expect(block.arguments).toHaveProperty('examplePartial');
expect(block.arguments.examplePartial[0]).toHaveProperty('type');
});

it('has expected shape', () => {
expect(block.arguments.examplePartial).to.eql([
expect(block.arguments.examplePartial).toEqual([
{
type: 'expression',
chain: [
Expand Down
Loading