-
Notifications
You must be signed in to change notification settings - Fork 89
/
index.ts
101 lines (91 loc) · 3.27 KB
/
index.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
/*
* Copyright 2020 Salto Labs Ltd.
*
* Licensed 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 _ from 'lodash'
import {
ObjectType, isType, isObjectType, isInstanceElement, Element,
isPrimitiveType, BuiltinTypes, TypeMap,
} from '@salto-io/adapter-api'
import { logger } from '@salto-io/logging'
import { mergeObjectTypes } from './internal/object_types'
import { mergeInstances } from './internal/instances'
import { mergePrimitives } from './internal/primitives'
import { MergeResult as InternalMergeResult } from './internal/common'
export { MergeError, DuplicateAnnotationError } from './internal/common'
export type MergeResult = InternalMergeResult<Element[]>
export {
FieldDefinitionMergeError, NoBaseDefinitionMergeError, MultipleBaseDefinitionsMergeError,
DuplicateAnnotationFieldDefinitionError, DuplicateAnnotationTypeError,
} from './internal/object_types'
export { DuplicateInstanceKeyError, createDefaultInstanceFromType } from './internal/instances'
export { MultiplePrimitiveTypesUnsupportedError } from './internal/primitives'
const log = logger(module)
/**
* Replace the pointers to all the merged elements to the merged version.
*/
const updateMergedTypes = (
elements: Element[],
mergedTypes: TypeMap
): Element[] => elements.map(elem => {
if (isType(elem)) {
elem.annotationTypes = _.mapValues(
elem.annotationTypes,
anno => mergedTypes[anno.elemID.getFullName()] || anno,
)
}
if (isObjectType(elem)) {
elem.fields = _.mapValues(
elem.fields,
field => {
field.type = mergedTypes[field.type.elemID.getFullName()] || field.type
return field
}
)
}
if (isInstanceElement(elem)) {
elem.type = mergedTypes[elem.type.elemID.getFullName()] as ObjectType || elem.type
}
return elem
})
/**
* Merge a list of elements by applying all updates, and replacing the pointers
* to the updated elements.
*/
export const mergeElements = (elements: ReadonlyArray<Element>): MergeResult => {
const objects = mergeObjectTypes(elements.filter(isObjectType))
const instances = mergeInstances(elements.filter(isInstanceElement))
const primitiveElements = [...elements.filter(isPrimitiveType), ...Object.values(BuiltinTypes)]
const primitives = mergePrimitives(primitiveElements)
const mergedElements = [
...elements.filter(e => !isObjectType(e) && !isInstanceElement(e)),
...Object.values(objects.merged),
...instances.merged,
]
const updated = updateMergedTypes(
mergedElements,
_.merge({}, objects.merged, primitives.merged)
)
const errors = [
...objects.errors,
...instances.errors,
...primitives.errors,
]
log.debug(`merged ${elements.length} elements to ${updated.length} elements [errors=${
errors.length}]`)
return {
merged: updated,
errors,
}
}