-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathdeprecation_factory.ts
128 lines (113 loc) · 3.73 KB
/
deprecation_factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* 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 { get } from 'lodash';
import { set } from '@elastic/safer-lodash-set';
import { unset } from '@kbn/std';
import { ConfigDeprecation, ConfigDeprecationLogger, ConfigDeprecationFactory } from './types';
const _rename = (
config: Record<string, any>,
rootPath: string,
log: ConfigDeprecationLogger,
oldKey: string,
newKey: string,
silent?: boolean
) => {
const fullOldPath = getPath(rootPath, oldKey);
const oldValue = get(config, fullOldPath);
if (oldValue === undefined) {
return config;
}
unset(config, fullOldPath);
const fullNewPath = getPath(rootPath, newKey);
const newValue = get(config, fullNewPath);
if (newValue === undefined) {
set(config, fullNewPath, oldValue);
if (!silent) {
log(`"${fullOldPath}" is deprecated and has been replaced by "${fullNewPath}"`);
}
} else {
if (!silent) {
log(
`"${fullOldPath}" is deprecated and has been replaced by "${fullNewPath}". However both key are present, ignoring "${fullOldPath}"`
);
}
}
return config;
};
const _copy = (
config: Record<string, any>,
rootPath: string,
originKey: string,
destinationKey: string
) => {
const originPath = getPath(rootPath, originKey);
const originValue = get(config, originPath);
if (originValue === undefined) {
return config;
}
const destinationPath = getPath(rootPath, destinationKey);
const destinationValue = get(config, destinationPath);
if (destinationValue === undefined) {
set(config, destinationPath, originValue);
}
return config;
};
const _unused = (
config: Record<string, any>,
rootPath: string,
log: ConfigDeprecationLogger,
unusedKey: string
) => {
const fullPath = getPath(rootPath, unusedKey);
if (get(config, fullPath) === undefined) {
return config;
}
unset(config, fullPath);
log(`${fullPath} is deprecated and is no longer used`);
return config;
};
const rename = (oldKey: string, newKey: string): ConfigDeprecation => (config, rootPath, log) =>
_rename(config, rootPath, log, oldKey, newKey);
const renameFromRoot = (oldKey: string, newKey: string, silent?: boolean): ConfigDeprecation => (
config,
rootPath,
log
) => _rename(config, '', log, oldKey, newKey, silent);
export const copyFromRoot = (originKey: string, destinationKey: string): ConfigDeprecation => (
config,
rootPath,
log
) => _copy(config, '', originKey, destinationKey);
const unused = (unusedKey: string): ConfigDeprecation => (config, rootPath, log) =>
_unused(config, rootPath, log, unusedKey);
const unusedFromRoot = (unusedKey: string): ConfigDeprecation => (config, rootPath, log) =>
_unused(config, '', log, unusedKey);
const getPath = (rootPath: string, subPath: string) =>
rootPath !== '' ? `${rootPath}.${subPath}` : subPath;
/**
* The actual platform implementation of {@link ConfigDeprecationFactory}
*
* @internal
*/
export const configDeprecationFactory: ConfigDeprecationFactory = {
rename,
renameFromRoot,
unused,
unusedFromRoot,
};