Templating: Optionally save interpolated expressions when replacing variables in a string (#65411)

* Testing a refactor

* update

* Update interface, test interoplations map

* Refactoring

* Add more explicit comment about new behavior

* Update packages/grafana-runtime/src/services/templateSrv.ts

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>

---------

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
This commit is contained in:
Piotr Jamróz
2023-03-28 16:19:27 +02:00
committed by GitHub
parent 0cff917f2a
commit b11186f946
3 changed files with 197 additions and 63 deletions

View File

@@ -1,5 +1,23 @@
import { ScopedVars, TimeRange, TypedVariableModel } from '@grafana/data';
/**
* Can be used to gain more information about an interpolation operation
*/
export interface VariableInterpolation {
/** The full matched expression including, example: ${varName.field:regex} */
match: string;
/** In the expression ${varName.field:regex} variableName is varName */
variableName: string;
/** In the expression ${varName.fields[0].name:regex} the fieldPath is fields[0].name */
fieldPath?: string;
/** In the expression ${varName:regex} the regex part is the format */
format?: string;
/** The formatted value of the variable expresion. Will equal match when variable not found or scopedVar was undefined or null **/
value: string;
// When value === match this will be true, meaning the variable was not found
found?: boolean;
}
/**
* Via the TemplateSrv consumers get access to all the available template variables
* that can be used within the current active dashboard.
@@ -15,8 +33,19 @@ export interface TemplateSrv {
/**
* Replace the values within the target string. See also {@link InterpolateFunction}
*
* Note: interpolations array is being mutated by replace function by adding information about variables that
* have been interpolated during replacement. Variables that were specified in the target but not found in
* the list of available variables are also added to the array. See {@link VariableInterpolation} for more details.
*
* @param {VariableInterpolation[]} interpolations an optional map that is updated with interpolated variables
*/
replace(target?: string, scopedVars?: ScopedVars, format?: string | Function): string;
replace(
target?: string,
scopedVars?: ScopedVars,
format?: string | Function,
interpolations?: VariableInterpolation[]
): string;
/**
* Checks if a target contains template variables.