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:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/config-loader': minor
|
||||
---
|
||||
|
||||
Add "path" to `TransformFunc` context
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user