mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Templating: A way to support object syntax for global vars (#21634)
This commit is contained in:
parent
935d447c6a
commit
92ef8644c5
@ -31,6 +31,14 @@ describe('templateSrv', () => {
|
||||
expect(target).toBe('Server1 nested');
|
||||
});
|
||||
|
||||
it('built in vars should support objects', () => {
|
||||
_templateSrv.setGlobalVariable('__dashboard', {
|
||||
value: { name: 'hello' },
|
||||
});
|
||||
const target = _templateSrv.replace('${__dashboard.name}');
|
||||
expect(target).toBe('hello');
|
||||
});
|
||||
|
||||
it('scoped vars should support objects with propert names with dot', () => {
|
||||
const target = _templateSrv.replace('${series.name} ${series.nested["field.with.dot"]}', {
|
||||
series: { value: { name: 'Server1', nested: { 'field.with.dot': 'nested' } } },
|
||||
|
@ -195,6 +195,15 @@ export class TemplateSrv {
|
||||
this.grafanaVariables[name] = value;
|
||||
}
|
||||
|
||||
setGlobalVariable(name: string, variable: any) {
|
||||
this.index = {
|
||||
...this.index,
|
||||
[name]: {
|
||||
current: variable,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
getVariableName(expression: string) {
|
||||
this.regex.lastIndex = 0;
|
||||
const match = this.regex.exec(expression);
|
||||
@ -295,6 +304,15 @@ export class TemplateSrv {
|
||||
}
|
||||
}
|
||||
|
||||
if (fieldPath) {
|
||||
const fieldValue = this.getVariableValue(variableName, fieldPath, {
|
||||
[variableName]: { value: value, text: '' },
|
||||
});
|
||||
if (fieldValue !== null && fieldValue !== undefined) {
|
||||
return this.formatValue(fieldValue, fmt, variable);
|
||||
}
|
||||
}
|
||||
|
||||
const res = this.formatValue(value, fmt, variable);
|
||||
return res;
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user