Skip to content
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 packages/amazonq/test/e2e/inline/inline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('Amazon Q Inline', async function () {
.query({
metricName: 'codewhisperer_userTriggerDecision',
})
.map((e) => collectionUtil.partialClone(e, 3, ['credentialStartUrl'], '[omitted]'))
.map((e) => collectionUtil.partialClone(e, 3, ['credentialStartUrl'], { replacement: '[omitted]' }))
}

for (const [name, invokeCompletion] of [
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/auth/sso/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ function addLoggingMiddleware(client: SSOOIDCClient) {
args.input as unknown as Record<string, unknown>,
3,
['clientSecret', 'accessToken', 'refreshToken'],
'[omitted]'
{ replacement: '[omitted]' }
)
getLogger().debug('API request (%s %s): %O', hostname, path, input)
}
Expand Down Expand Up @@ -288,7 +288,7 @@ function addLoggingMiddleware(client: SSOOIDCClient) {
result.output as unknown as Record<string, unknown>,
3,
['clientSecret', 'accessToken', 'refreshToken'],
'[omitted]'
{ replacement: '[omitted]' }
)
getLogger().debug('API response (%s %s): %O', hostname, path, output)
}
Expand Down
21 changes: 17 additions & 4 deletions packages/core/src/shared/utilities/collectionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isWeb } from '../extensionGlobals'
import { inspect as nodeInspect } from 'util'
import { AsyncCollection, toCollection } from './asyncCollection'
import { SharedProp, AccumulableKeys, Coalesce, isNonNullable } from './tsUtils'
import { truncate } from './textUtilities'

export function union<T>(a: Iterable<T>, b: Iterable<T>): Set<T> {
const result = new Set<T>()
Expand Down Expand Up @@ -304,26 +305,38 @@ export function assign<T extends Record<any, any>, U extends Partial<T>>(data: T
* @param depth
* @param omitKeys Omit properties matching these names (at any depth).
* @param replacement Replacement for object whose fields extend beyond `depth`, and properties matching `omitKeys`.
* @param maxStringLength truncates string values that exceed this threshold (includes values in nested arrays)
*/
export function partialClone(obj: any, depth: number = 3, omitKeys: string[] = [], replacement?: any): any {
export function partialClone(
obj: any,
depth: number = 3,
omitKeys: string[] = [],
options?: {
replacement?: any
maxStringLength?: number
}
): any {
// Base case: If input is not an object or has no children, return it.
if (typeof obj !== 'object' || obj === null || 0 === Object.getOwnPropertyNames(obj).length) {
if (typeof obj === 'string' && options?.maxStringLength) {
return truncate(obj, options?.maxStringLength, '...')
}
return obj
}

// Create a new object of the same type as the input object.
const clonedObj = Array.isArray(obj) ? [] : {}

if (depth === 0) {
return replacement ? replacement : clonedObj
return options?.replacement ? options.replacement : clonedObj
}

// Recursively clone properties of the input object
for (const key in obj) {
if (omitKeys.includes(key)) {
;(clonedObj as any)[key] = replacement ? replacement : Array.isArray(obj) ? [] : {}
;(clonedObj as any)[key] = options?.replacement ? options.replacement : Array.isArray(obj) ? [] : {}
} else if (Object.prototype.hasOwnProperty.call(obj, key)) {
;(clonedObj as any)[key] = partialClone(obj[key], depth - 1, omitKeys, replacement)
;(clonedObj as any)[key] = partialClone(obj[key], depth - 1, omitKeys, options)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/shared/utilities/textUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { default as stripAnsi } from 'strip-ansi'
import { getLogger } from '../logger/logger'

/**
* Truncates string `s` if it exceeds `n` chars.
* Truncates string `s` if it has or exceeds `n` chars.
*
* If `n` is negative, truncates at start instead of end.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/shared/vscode/commands2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ async function runCommand<T extends Callback>(fn: T, info: CommandInfo<T>): Prom

logger.debug(
`command: running ${label} with arguments: %O`,
partialClone(args, 3, ['clientSecret', 'accessToken', 'refreshToken', 'tooltip'], '[omitted]')
partialClone(args, 3, ['clientSecret', 'accessToken', 'refreshToken', 'tooltip'], { replacement: '[omitted]' })
)

try {
Expand Down
91 changes: 58 additions & 33 deletions packages/core/src/test/shared/utilities/collectionUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,10 @@ describe('CollectionUtils', async function () {
})

describe('partialClone', function () {
it('omits properties by depth', function () {
const testObj = {
let multipleTypedObj: object
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is changed to avoid duplicate code lint fail.


before(async function () {
multipleTypedObj = {
a: 34234234234,
b: '123456789',
c: new Date(2023, 1, 1),
Expand All @@ -724,57 +726,80 @@ describe('CollectionUtils', async function () {
throw Error()
},
}
})

assert.deepStrictEqual(partialClone(testObj, 1), {
...testObj,
it('omits properties by depth', function () {
assert.deepStrictEqual(partialClone(multipleTypedObj, 1), {
...multipleTypedObj,
d: {},
e: {},
})
assert.deepStrictEqual(partialClone(testObj, 0, [], '[replaced]'), '[replaced]')
assert.deepStrictEqual(partialClone(testObj, 1, [], '[replaced]'), {
...testObj,
assert.deepStrictEqual(partialClone(multipleTypedObj, 0, [], { replacement: '[replaced]' }), '[replaced]')
assert.deepStrictEqual(partialClone(multipleTypedObj, 1, [], { replacement: '[replaced]' }), {
...multipleTypedObj,
d: '[replaced]',
e: '[replaced]',
})
assert.deepStrictEqual(partialClone(testObj, 3), {
...testObj,
assert.deepStrictEqual(partialClone(multipleTypedObj, 3), {
...multipleTypedObj,
d: { d1: { d2: {} } },
})
assert.deepStrictEqual(partialClone(testObj, 4), testObj)
assert.deepStrictEqual(partialClone(multipleTypedObj, 4), multipleTypedObj)
})

it('omits properties by name', function () {
const testObj = {
a: 34234234234,
b: '123456789',
c: new Date(2023, 1, 1),
d: { d1: { d2: { d3: 'deep' } } },
assert.deepStrictEqual(partialClone(multipleTypedObj, 2, ['c', 'e2'], { replacement: '[replaced]' }), {
...multipleTypedObj,
c: '[replaced]',
d: { d1: '[replaced]' },
e: {
e1: '[replaced]',
e2: '[replaced]',
},
})
assert.deepStrictEqual(partialClone(multipleTypedObj, 3, ['c', 'e2'], { replacement: '[replaced]' }), {
...multipleTypedObj,
c: '[replaced]',
d: { d1: { d2: '[replaced]' } },
e: {
e1: [4, 3, 7],
e2: 'loooooooooo \n nnnnnnnnnnn \n gggggggg \n string',
e2: '[replaced]',
},
f: () => {
throw Error()
})
})

it('truncates properties by maxLength', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to also test when there are other non object types like number or boolean

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout, I've adjusted the test to avoid only using strings.

const testObj = {
strValue: '1',
boolValue: true,
longString: '11111',
nestedObj: {
nestedObjAgain: {
longNestedStr: '11111',
shortNestedStr: '11',
},
},
nestedObj2: {
functionValue: (_: unknown) => {},
},
nestedObj3: {
myArray: ['1', '11111', '1'],
},
objInArray: [{ shortString: '11', longString: '11111' }],
}

assert.deepStrictEqual(partialClone(testObj, 2, ['c', 'e2'], '[omitted]'), {
assert.deepStrictEqual(partialClone(testObj, 5, [], { maxStringLength: 2 }), {
...testObj,
c: '[omitted]',
d: { d1: '[omitted]' },
e: {
e1: '[omitted]',
e2: '[omitted]',
longString: '11...',
nestedObj: {
nestedObjAgain: {
longNestedStr: '11...',
shortNestedStr: '11',
},
},
})
assert.deepStrictEqual(partialClone(testObj, 3, ['c', 'e2'], '[omitted]'), {
...testObj,
c: '[omitted]',
d: { d1: { d2: '[omitted]' } },
e: {
e1: [4, 3, 7],
e2: '[omitted]',
nestedObj3: {
myArray: ['1', '11...', '1'],
},
objInArray: [{ shortString: '11', longString: '11...' }],
})
})
})
Expand Down
Loading