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 <mhentges@spotify.com>
This commit is contained in:
Mitchell Hentges
2023-12-01 16:17:52 +01:00
parent df4a219c52
commit 24f5a8567d
4 changed files with 19 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/config-loader': minor
---
Add "path" to `TransformFunc` context
+1
View File
@@ -274,6 +274,7 @@ export type TransformFunc<T extends number | string | boolean> = (
value: T,
context: {
visibility: ConfigVisibility;
path: string;
},
) => T | undefined;
```
@@ -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;
}
+12 -1
View File
@@ -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<T extends number | string | boolean> = (
value: T,
context: { visibility: ConfigVisibility },
context: { visibility: ConfigVisibility; path: string },
) => T | undefined;
/**