-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[APM] Improve router types #83620
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
Merged
Merged
[APM] Improve router types #83620
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
x-pack/plugins/apm/common/runtime_types/merge/index.test.ts
This file contains hidden or 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,71 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
| import * as t from 'io-ts'; | ||
| import { isLeft } from 'fp-ts/lib/Either'; | ||
| import { merge } from './'; | ||
| import { jsonRt } from '../json_rt'; | ||
|
|
||
| describe('merge', () => { | ||
| it('fails on one or more errors', () => { | ||
| const type = merge([t.type({ foo: t.string }), t.type({ bar: t.number })]); | ||
|
|
||
| const result = type.decode({ foo: '' }); | ||
|
|
||
| expect(isLeft(result)).toBe(true); | ||
| }); | ||
|
|
||
| it('merges left to right', () => { | ||
| const typeBoolean = merge([ | ||
| t.type({ foo: t.string }), | ||
| t.type({ foo: jsonRt.pipe(t.boolean) }), | ||
| ]); | ||
|
|
||
| const resultBoolean = typeBoolean.decode({ | ||
| foo: 'true', | ||
| }); | ||
|
|
||
| // @ts-expect-error | ||
| expect(resultBoolean.right).toEqual({ | ||
| foo: true, | ||
| }); | ||
|
|
||
| const typeString = merge([ | ||
| t.type({ foo: jsonRt.pipe(t.boolean) }), | ||
| t.type({ foo: t.string }), | ||
| ]); | ||
|
|
||
| const resultString = typeString.decode({ | ||
| foo: 'true', | ||
| }); | ||
|
|
||
| // @ts-expect-error | ||
| expect(resultString.right).toEqual({ | ||
| foo: 'true', | ||
| }); | ||
| }); | ||
|
|
||
| it('deeply merges values', () => { | ||
| const type = merge([ | ||
| t.type({ foo: t.type({ baz: t.string }) }), | ||
| t.type({ foo: t.type({ bar: t.string }) }), | ||
| ]); | ||
|
|
||
| const result = type.decode({ | ||
| foo: { | ||
| bar: '', | ||
| baz: '', | ||
| }, | ||
| }); | ||
|
|
||
| // @ts-expect-error | ||
| expect(result.right).toEqual({ | ||
| foo: { | ||
| bar: '', | ||
| baz: '', | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,68 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
| import * as t from 'io-ts'; | ||
| import { merge as lodashMerge } from 'lodash'; | ||
| import { isLeft } from 'fp-ts/lib/Either'; | ||
| import { ValuesType } from 'utility-types'; | ||
|
|
||
| export type MergeType< | ||
| T extends t.Any[], | ||
| U extends ValuesType<T> = ValuesType<T> | ||
| > = t.Type<U['_A'], U['_O'], U['_I']> & { | ||
| _tag: 'MergeType'; | ||
| types: T; | ||
| }; | ||
|
|
||
| // this is similar to t.intersection, but does a deep merge | ||
| // instead of a shallow merge | ||
|
|
||
| export function merge<A extends t.Mixed, B extends t.Mixed>( | ||
| types: [A, B] | ||
| ): MergeType<[A, B]>; | ||
|
|
||
| export function merge(types: t.Any[]) { | ||
| const mergeType = new t.Type( | ||
| 'merge', | ||
| (u): u is unknown => { | ||
| return types.every((type) => type.is(u)); | ||
| }, | ||
| (input, context) => { | ||
| const errors: t.Errors = []; | ||
|
|
||
| const successes: unknown[] = []; | ||
|
|
||
| const results = types.map((type, index) => | ||
| type.validate( | ||
| input, | ||
| context.concat({ | ||
| key: String(index), | ||
| type, | ||
| actual: input, | ||
| }) | ||
| ) | ||
| ); | ||
|
|
||
| results.forEach((result) => { | ||
| if (isLeft(result)) { | ||
| errors.push(...result.left); | ||
| } else { | ||
| successes.push(result.right); | ||
| } | ||
| }); | ||
|
|
||
| const mergedValues = lodashMerge({}, ...successes); | ||
|
|
||
| return errors.length > 0 ? t.failures(errors) : t.success(mergedValues); | ||
| }, | ||
| (a) => types.reduce((val, type) => type.encode(val), a) | ||
| ); | ||
|
|
||
| return { | ||
| ...mergeType, | ||
| _tag: 'MergeType', | ||
| types, | ||
| }; | ||
| } | ||
106 changes: 106 additions & 0 deletions
106
x-pack/plugins/apm/common/runtime_types/strict_keys_rt/index.test.ts
This file contains hidden or 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,106 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
| import * as t from 'io-ts'; | ||
| import { isRight, isLeft } from 'fp-ts/lib/Either'; | ||
| import { strictKeysRt } from './'; | ||
| import { jsonRt } from '../json_rt'; | ||
|
|
||
| describe('strictKeysRt', () => { | ||
| it('correctly and deeply validates object keys', () => { | ||
| const checks: Array<{ type: t.Type<any>; passes: any[]; fails: any[] }> = [ | ||
| { | ||
| type: t.intersection([ | ||
| t.type({ foo: t.string }), | ||
| t.partial({ bar: t.string }), | ||
| ]), | ||
| passes: [{ foo: '' }, { foo: '', bar: '' }], | ||
| fails: [ | ||
| { foo: '', unknownKey: '' }, | ||
| { foo: '', bar: '', unknownKey: '' }, | ||
| ], | ||
| }, | ||
| { | ||
| type: t.type({ | ||
| path: t.union([ | ||
| t.type({ serviceName: t.string }), | ||
| t.type({ transactionType: t.string }), | ||
| ]), | ||
| }), | ||
| passes: [ | ||
| { path: { serviceName: '' } }, | ||
| { path: { transactionType: '' } }, | ||
| ], | ||
| fails: [ | ||
| { path: { serviceName: '', unknownKey: '' } }, | ||
| { path: { transactionType: '', unknownKey: '' } }, | ||
| { path: { serviceName: '', transactionType: '' } }, | ||
| { path: { serviceName: '' }, unknownKey: '' }, | ||
| ], | ||
| }, | ||
| { | ||
| type: t.intersection([ | ||
| t.type({ query: t.type({ bar: t.string }) }), | ||
| t.partial({ query: t.partial({ _debug: t.boolean }) }), | ||
| ]), | ||
| passes: [{ query: { bar: '', _debug: true } }], | ||
| fails: [{ query: { _debug: true } }], | ||
| }, | ||
| ]; | ||
|
|
||
| checks.forEach((check) => { | ||
| const { type, passes, fails } = check; | ||
|
|
||
| const strictType = strictKeysRt(type); | ||
|
|
||
| passes.forEach((value) => { | ||
| const result = strictType.decode(value); | ||
|
|
||
| if (!isRight(result)) { | ||
| throw new Error( | ||
| `Expected ${JSON.stringify( | ||
| value | ||
| )} to be allowed, but validation failed with ${ | ||
| result.left[0].message | ||
| }` | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| fails.forEach((value) => { | ||
| const result = strictType.decode(value); | ||
|
|
||
| if (!isLeft(result)) { | ||
| throw new Error( | ||
| `Expected ${JSON.stringify( | ||
| value | ||
| )} to be disallowed, but validation succeeded` | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('does not support piped types', () => { | ||
| const typeA = t.type({ | ||
| query: t.type({ filterNames: jsonRt.pipe(t.array(t.string)) }), | ||
| } as Record<string, any>); | ||
|
|
||
| const typeB = t.partial({ | ||
| query: t.partial({ _debug: jsonRt.pipe(t.boolean) }), | ||
| }); | ||
|
|
||
| const value = { | ||
| query: { | ||
| _debug: 'true', | ||
| filterNames: JSON.stringify(['host', 'agentName']), | ||
| }, | ||
| }; | ||
|
|
||
| const pipedType = strictKeysRt(typeA.pipe(typeB)); | ||
|
|
||
| expect(isLeft(pipedType.decode(value))).toBe(true); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be called
deepMergethen?Btw. I'm surprised this doesn't already exist in the shared typescript utils.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's
mergebecause it useslodash.merge. I figureddeepMergewould be overkill.