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

[legacy/server/config] remove unnecessary deps for simple helper #64954

Merged
merged 6 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/legacy/server/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import Joi from 'joi';
import _ from 'lodash';
import override from './override';
import { override } from './override';
import createDefaultSchema from './schema';
import { unset, deepCloneWithBuffers as clone, IS_KIBANA_DISTRIBUTABLE } from '../../utils';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
Expand Down
37 changes: 0 additions & 37 deletions src/legacy/server/config/explode_by.js

This file was deleted.

48 changes: 0 additions & 48 deletions src/legacy/server/config/explode_by.test.js

This file was deleted.

28 changes: 0 additions & 28 deletions src/legacy/server/config/override.js

This file was deleted.

44 changes: 0 additions & 44 deletions src/legacy/server/config/override.test.js

This file was deleted.

130 changes: 130 additions & 0 deletions src/legacy/server/config/override.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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 { override } from './override';

describe('override(target, source)', function() {
it('should override the values form source to target', function() {
const target = {
test: {
enable: true,
host: ['something else'],
client: {
type: 'sql',
},
},
};

const source = {
test: {
host: ['host-01', 'host-02'],
client: {
type: 'nosql',
},
foo: {
bar: {
baz: 1,
},
},
},
};

expect(override(target, source)).toMatchInlineSnapshot(`
Object {
"test": Object {
"client": Object {
"type": "nosql",
},
"enable": true,
"foo": Object {
"bar": Object {
"baz": 1,
},
},
"host": Array [
"host-01",
"host-02",
],
},
}
`);
});

it('does not mutate arguments', () => {
const target = {
foo: {
bar: 1,
baz: 1,
},
};

const source = {
foo: {
bar: 2,
},
box: 2,
};

expect(override(target, source)).toMatchInlineSnapshot(`
Object {
"box": 2,
"foo": Object {
"bar": 2,
"baz": 1,
},
}
`);
expect(target).not.toHaveProperty('box');
expect(source.foo).not.toHaveProperty('baz');
});

it('explodes keys with dots in them', () => {
const target = {
foo: {
bar: 1,
},
'baz.box.boot.bar.bar': 20,
};

const source = {
'foo.bar': 2,
'baz.box.boot': {
'bar.foo': 10,
},
};

expect(override(target, source)).toMatchInlineSnapshot(`
Object {
"baz": Object {
"box": Object {
"boot": Object {
"bar": Object {
"bar": 20,
"foo": 10,
},
},
},
},
"foo": Object {
"bar": 2,
},
}
`);
});
});
52 changes: 52 additions & 0 deletions src/legacy/server/config/override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.
*/

const isObject = (v: any): v is Record<string, any> =>
typeof v === 'object' && v !== null && !Array.isArray(v);

const assignDeep = (target: Record<string, any>, source: Record<string, any>) => {
for (let [key, value] of Object.entries(source)) {
// unwrap dot-separated keys
if (key.includes('.')) {
const [first, ...others] = key.split('.');
key = first;
value = { [others.join('.')]: value };
}

if (isObject(value)) {
if (!target.hasOwnProperty(key)) {
target[key] = {};
}
spalger marked this conversation as resolved.
Show resolved Hide resolved

assignDeep(target[key], value);
} else {
target[key] = value;
}
}
};

export const override = (...sources: Array<Record<string, any>>): Record<string, any> => {
const result = {};

for (const object of sources) {
assignDeep(result, object);
}

return result;
};