Skip to content

Commit

Permalink
load js-yaml lazily (#79092)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov authored Oct 4, 2020
1 parent acedb35 commit 5cc945f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import _ from 'lodash';
import { get } from 'lodash';
import React from 'react';

import { EuiLink } from '@elastic/eui';
Expand All @@ -31,7 +31,7 @@ export const ConnectedLinkComponent = ({
}

// Shorthand for pathname
const pathname = path || _.get(props.to, 'pathname') || location.pathname;
const pathname = path || get(props.to, 'pathname') || location.pathname;

return (
<Link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Tags Client Domain Lib', () => {
});

it('should use helper function to convert users yaml in tag to config object', async () => {
const convertedBlocks = lib.userConfigsToJson([
const convertedBlocks = await lib.userConfigsToJson([
{
id: 'foo',
tag: 'basic',
Expand All @@ -42,7 +42,7 @@ describe('Tags Client Domain Lib', () => {
});

it('should use helper function to convert user config to json with undefined `other`', async () => {
const convertedTag = lib.userConfigsToJson([
const convertedTag = await lib.userConfigsToJson([
{
id: 'foo',
tag: 'basic',
Expand All @@ -61,7 +61,7 @@ describe('Tags Client Domain Lib', () => {
});

it('should use helper function to convert users yaml in tag to config object, where empty other leads to no other fields saved', async () => {
const convertedTag = lib.userConfigsToJson([
const convertedTag = await lib.userConfigsToJson([
{
id: 'foo',
tag: 'basic',
Expand All @@ -83,7 +83,7 @@ describe('Tags Client Domain Lib', () => {
});

it('should convert tokenized fields to JSON', async () => {
const convertedTag = lib.userConfigsToJson([
const convertedTag = await lib.userConfigsToJson([
{
id: 'foo',
tag: 'basic',
Expand All @@ -106,7 +106,7 @@ describe('Tags Client Domain Lib', () => {
});

it('should use helper function to convert config object to users yaml', async () => {
const convertedTag = lib.jsonConfigToUserYaml([
const convertedTag = await lib.jsonConfigToUserYaml([
{
id: 'foo',
tag: 'basic',
Expand All @@ -127,7 +127,7 @@ describe('Tags Client Domain Lib', () => {
});

it('should use helper function to convert config object to users yaml with empty `other`', async () => {
const convertedTag = lib.jsonConfigToUserYaml([
const convertedTag = await lib.jsonConfigToUserYaml([
{
id: 'foo',
tag: 'basic',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import yaml from 'js-yaml';
import { set } from '@elastic/safer-lodash-set';
import { get, has, omit } from 'lodash';
import { ConfigBlockSchema, ConfigurationBlock } from '../../common/domain_types';
Expand All @@ -19,16 +18,17 @@ export class ConfigBlocksLib {
) {}

public upsert = async (blocks: ConfigurationBlock[]) => {
return await this.adapter.upsert(this.userConfigsToJson(blocks));
return await this.adapter.upsert(await this.userConfigsToJson(blocks));
};

public getForTags = async (tagIds: string[], page: number) => {
const result = await this.adapter.getForTags(tagIds, page);
result.list = this.jsonConfigToUserYaml(result.list);
result.list = await this.jsonConfigToUserYaml(result.list);
return result;
};

public jsonConfigToUserYaml(blocks: ConfigurationBlock[]): ConfigurationBlock[] {
public async jsonConfigToUserYaml(blocks: ConfigurationBlock[]): Promise<ConfigurationBlock[]> {
const yaml = await import('js-yaml');
// configuration_blocks yaml, JS cant read YAML so we parse it into JS,
// because beats flattens all fields, and we need more structure.
// we take tagConfigs, grab the config that applies here, render what we can into
Expand Down Expand Up @@ -73,7 +73,8 @@ export class ConfigBlocksLib {
});
}

public userConfigsToJson(blocks: ConfigurationBlock[]): ConfigurationBlock[] {
public async userConfigsToJson(blocks: ConfigurationBlock[]): Promise<ConfigurationBlock[]> {
const yaml = await import('js-yaml');
// configurations is the JS representation of the config yaml,
// so here we take that JS and convert it into a YAML string.
// we do so while also flattening "other" into the flat yaml beats expect
Expand Down

0 comments on commit 5cc945f

Please sign in to comment.