-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main' into kbn-xxx-SOR-cleanup
- Loading branch information
Showing
208 changed files
with
1,041 additions
and
6,107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...core-saved-objects-base-server-internal/src/model_version/aggregate_model_changes.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { SavedObjectsModelChange } from '@kbn/core-saved-objects-server'; | ||
import { aggregateMappingAdditions } from './aggregate_model_changes'; | ||
|
||
describe('aggregateMappingAdditions', () => { | ||
it('merges top level properties', () => { | ||
const changes: SavedObjectsModelChange[] = [ | ||
{ type: 'mappings_addition', addedMappings: { foo: { type: 'text' } } }, | ||
{ type: 'mappings_addition', addedMappings: { bar: { type: 'keyword' } } }, | ||
]; | ||
const output = aggregateMappingAdditions(changes); | ||
expect(output).toEqual({ | ||
foo: { type: 'text' }, | ||
bar: { type: 'keyword' }, | ||
}); | ||
}); | ||
|
||
it('merges nested properties', () => { | ||
const changes: SavedObjectsModelChange[] = [ | ||
{ | ||
type: 'mappings_addition', | ||
addedMappings: { nested: { properties: { foo: { type: 'text' } } } }, | ||
}, | ||
{ | ||
type: 'mappings_addition', | ||
addedMappings: { nested: { properties: { bar: { type: 'keyword' } } } }, | ||
}, | ||
]; | ||
const output = aggregateMappingAdditions(changes); | ||
expect(output).toEqual({ | ||
nested: { | ||
properties: { | ||
foo: { type: 'text' }, | ||
bar: { type: 'keyword' }, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('accepts other types of changes', () => { | ||
const changes: SavedObjectsModelChange[] = [ | ||
{ type: 'mappings_addition', addedMappings: { foo: { type: 'text' } } }, | ||
{ type: 'mappings_deprecation', deprecatedMappings: [] }, | ||
{ type: 'data_backfill', transform: jest.fn() }, | ||
]; | ||
const output = aggregateMappingAdditions(changes); | ||
expect(output).toEqual({ | ||
foo: { type: 'text' }, | ||
}); | ||
}); | ||
|
||
it('override in order in case of definition conflict', () => { | ||
const changes: SavedObjectsModelChange[] = [ | ||
{ type: 'mappings_addition', addedMappings: { foo: { type: 'text' } } }, | ||
{ type: 'mappings_addition', addedMappings: { foo: { type: 'keyword' } } }, | ||
]; | ||
const output = aggregateMappingAdditions(changes); | ||
expect(output).toEqual({ | ||
foo: { type: 'keyword' }, | ||
}); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
...ects/core-saved-objects-base-server-internal/src/model_version/aggregate_model_changes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { merge } from 'lodash'; | ||
import type { | ||
SavedObjectsModelChange, | ||
SavedObjectsMappingProperties, | ||
} from '@kbn/core-saved-objects-server'; | ||
|
||
/** | ||
* Merge the added mappings from the given list of model changes. | ||
* Note: only changes of the `mappings_addition` type have mapping addition. | ||
*/ | ||
export const aggregateMappingAdditions = ( | ||
changes: SavedObjectsModelChange[] | ||
): SavedObjectsMappingProperties => { | ||
let mappings: SavedObjectsMappingProperties = {}; | ||
changes.forEach((change) => { | ||
if (change.type === 'mappings_addition') { | ||
mappings = merge(mappings, change.addedMappings); | ||
} | ||
}); | ||
return mappings; | ||
}; |
127 changes: 127 additions & 0 deletions
127
...ects/core-saved-objects-base-server-internal/src/model_version/build_transform_fn.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { loggerMock } from '@kbn/logging-mocks'; | ||
import type { | ||
SavedObjectsModelChange, | ||
SavedObjectModelTransformationFn, | ||
SavedObjectModelTransformationDoc, | ||
SavedObjectModelTransformationContext, | ||
} from '@kbn/core-saved-objects-server'; | ||
import { buildModelVersionTransformFn } from './build_transform_fn'; | ||
|
||
describe('buildModelVersionTransformFn', () => { | ||
const stubTransform = (): jest.MockedFn<SavedObjectModelTransformationFn> => | ||
jest.fn().mockImplementation((doc: SavedObjectModelTransformationDoc) => ({ document: doc })); | ||
|
||
const createContext = (): SavedObjectModelTransformationContext => ({ | ||
log: loggerMock.create(), | ||
modelVersion: 42, | ||
}); | ||
|
||
const createDoc = (): SavedObjectModelTransformationDoc => ({ | ||
id: 'foo', | ||
type: 'bar', | ||
attributes: {}, | ||
}); | ||
|
||
it('builds the transform from a single function', () => { | ||
const transform1 = stubTransform(); | ||
const changes: SavedObjectsModelChange[] = [{ type: 'data_backfill', transform: transform1 }]; | ||
|
||
const mergedTransform = buildModelVersionTransformFn(changes); | ||
|
||
const context = createContext(); | ||
const document = createDoc(); | ||
|
||
mergedTransform(document, context); | ||
|
||
expect(transform1).toHaveBeenCalledTimes(1); | ||
expect(transform1).toHaveBeenCalledWith(document, context); | ||
}); | ||
|
||
it('builds the transform from 2 functions', () => { | ||
const transform1 = stubTransform(); | ||
const transform2 = stubTransform(); | ||
const changes: SavedObjectsModelChange[] = [ | ||
{ type: 'data_backfill', transform: transform1 }, | ||
{ type: 'data_backfill', transform: transform2 }, | ||
]; | ||
|
||
const mergedTransform = buildModelVersionTransformFn(changes); | ||
|
||
const context = createContext(); | ||
const document = createDoc(); | ||
|
||
mergedTransform(document, context); | ||
|
||
expect(transform1).toHaveBeenCalledTimes(1); | ||
expect(transform1).toHaveBeenCalledWith(document, context); | ||
expect(transform2).toHaveBeenCalledTimes(1); | ||
expect(transform2).toHaveBeenCalledWith(document, context); | ||
expect(transform1.mock.invocationCallOrder[0]).toBeLessThan( | ||
transform2.mock.invocationCallOrder[0] | ||
); | ||
}); | ||
|
||
it('builds the transform from more functions', () => { | ||
const transform1 = stubTransform(); | ||
const transform2 = stubTransform(); | ||
const transform3 = stubTransform(); | ||
const transform4 = stubTransform(); | ||
const changes: SavedObjectsModelChange[] = [ | ||
{ type: 'data_backfill', transform: transform1 }, | ||
{ type: 'data_backfill', transform: transform2 }, | ||
{ type: 'data_backfill', transform: transform3 }, | ||
{ type: 'data_backfill', transform: transform4 }, | ||
]; | ||
|
||
const mergedTransform = buildModelVersionTransformFn(changes); | ||
|
||
const context = createContext(); | ||
const document = createDoc(); | ||
|
||
mergedTransform(document, context); | ||
|
||
expect(transform1).toHaveBeenCalledTimes(1); | ||
expect(transform2).toHaveBeenCalledTimes(1); | ||
expect(transform3).toHaveBeenCalledTimes(1); | ||
expect(transform4).toHaveBeenCalledTimes(1); | ||
|
||
expect(transform1).toHaveBeenCalledWith(document, context); | ||
expect(transform2).toHaveBeenCalledWith(document, context); | ||
expect(transform3).toHaveBeenCalledWith(document, context); | ||
expect(transform4).toHaveBeenCalledWith(document, context); | ||
|
||
expect(transform1.mock.invocationCallOrder[0]).toBeLessThan( | ||
transform2.mock.invocationCallOrder[0] | ||
); | ||
expect(transform2.mock.invocationCallOrder[0]).toBeLessThan( | ||
transform3.mock.invocationCallOrder[0] | ||
); | ||
expect(transform3.mock.invocationCallOrder[0]).toBeLessThan( | ||
transform4.mock.invocationCallOrder[0] | ||
); | ||
}); | ||
|
||
it('returns a no-op function when no transform functions are present', () => { | ||
const changes: SavedObjectsModelChange[] = [ | ||
{ type: 'mappings_addition', addedMappings: {} }, | ||
{ type: 'mappings_deprecation', deprecatedMappings: [] }, | ||
]; | ||
|
||
const mergedTransform = buildModelVersionTransformFn(changes); | ||
|
||
const context = createContext(); | ||
const document = createDoc(); | ||
|
||
const output = mergedTransform(document, context); | ||
// test that it returns the source doc unmodified | ||
expect(output.document).toEqual(createDoc()); | ||
}); | ||
}); |
54 changes: 54 additions & 0 deletions
54
...d-objects/core-saved-objects-base-server-internal/src/model_version/build_transform_fn.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { | ||
SavedObjectsModelChange, | ||
SavedObjectModelTransformationFn, | ||
} from '@kbn/core-saved-objects-server'; | ||
|
||
/** | ||
* Build the transform function for given model version, by chaining the transformations from its model changes. | ||
*/ | ||
export const buildModelVersionTransformFn = ( | ||
modelChanges: SavedObjectsModelChange[] | ||
): SavedObjectModelTransformationFn => { | ||
const transformFns: SavedObjectModelTransformationFn[] = []; | ||
modelChanges.forEach((change) => { | ||
if (change.type === 'data_backfill') { | ||
transformFns.push(change.transform); | ||
} | ||
}); | ||
return mergeTransformFunctions(transformFns); | ||
}; | ||
|
||
const mergeTransformFunctions = ( | ||
transformFns: SavedObjectModelTransformationFn[] | ||
): SavedObjectModelTransformationFn => { | ||
if (transformFns.length === 0) { | ||
return noopTransform; | ||
} | ||
if (transformFns.length === 1) { | ||
return transformFns[0]; | ||
} | ||
let mergedFn = transformFns[0]; | ||
for (let i = 1; i < transformFns.length; i++) { | ||
mergedFn = merge(transformFns[i], mergedFn); | ||
} | ||
return mergedFn; | ||
}; | ||
|
||
const noopTransform: SavedObjectModelTransformationFn = (doc) => ({ document: doc }); | ||
|
||
const merge = ( | ||
outer: SavedObjectModelTransformationFn, | ||
inner: SavedObjectModelTransformationFn | ||
): SavedObjectModelTransformationFn => { | ||
return (document, context) => { | ||
return outer(inner(document, context).document, context); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.