From 24f5a8567d98aa1cb8fc2c953b65a8c8efdf2a8f Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Fri, 1 Dec 2023 16:17:52 +0100 Subject: [PATCH] Add "path" to `TransformFunc` context We have a use case where the transform we're applying to a value _depends_ on where that value sits in the object tree. `filterPath` seemed better to expose that `visibilityPath` because: * JQ-style paths with `.` as separate are more common, and are supported by `lodash`'s `get()` function. * For array items, `visibilityPath` doesn't include the array index Signed-off-by: Mitchell Hentges --- .changeset/eighty-phones-peel.md | 5 +++++ packages/config-loader/api-report.md | 1 + packages/config-loader/src/schema/filtering.ts | 2 +- packages/config-loader/src/schema/types.ts | 13 ++++++++++++- 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .changeset/eighty-phones-peel.md diff --git a/.changeset/eighty-phones-peel.md b/.changeset/eighty-phones-peel.md new file mode 100644 index 0000000000..954553427a --- /dev/null +++ b/.changeset/eighty-phones-peel.md @@ -0,0 +1,5 @@ +--- +'@backstage/config-loader': minor +--- + +Add "path" to `TransformFunc` context diff --git a/packages/config-loader/api-report.md b/packages/config-loader/api-report.md index 9f55a27bf7..66572dd960 100644 --- a/packages/config-loader/api-report.md +++ b/packages/config-loader/api-report.md @@ -274,6 +274,7 @@ export type TransformFunc = ( value: T, context: { visibility: ConfigVisibility; + path: string; }, ) => T | undefined; ``` diff --git a/packages/config-loader/src/schema/filtering.ts b/packages/config-loader/src/schema/filtering.ts index 1b7bbf43c1..9c1c2fbda5 100644 --- a/packages/config-loader/src/schema/filtering.ts +++ b/packages/config-loader/src/schema/filtering.ts @@ -68,7 +68,7 @@ export function filterByVisibility( if (typeof jsonVal !== 'object') { if (isVisible) { if (transformFunc) { - return transformFunc(jsonVal, { visibility }); + return transformFunc(jsonVal, { visibility, path: filterPath }); } return jsonVal; } diff --git a/packages/config-loader/src/schema/types.ts b/packages/config-loader/src/schema/types.ts index dc8da64038..8f679c93fc 100644 --- a/packages/config-loader/src/schema/types.ts +++ b/packages/config-loader/src/schema/types.ts @@ -105,11 +105,22 @@ export type ValidationFunc = (configs: AppConfig[]) => ValidationResult; /** * A function used to transform primitive configuration values. * + * The "path" in the context is a JQ-style path to the current value from + * within the original object passed to filterByVisibility(). + * For example, "field.list[2]" would refer to: + * \{ + * field: [ + * "foo", + * "bar", + * "baz" -- this one + * ] + * \} + * * @public */ export type TransformFunc = ( value: T, - context: { visibility: ConfigVisibility }, + context: { visibility: ConfigVisibility; path: string }, ) => T | undefined; /**