Fix: allow variables in nested objects (#35684)

* Fix: allow variables in nested objects

* Changes from review
This commit is contained in:
Travis Patterson 2021-06-16 00:36:25 -06:00 committed by GitHub
parent 182b03aa8f
commit 2e58ad5fd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -1,4 +1,11 @@
import { ensureStringValues, findTemplateVarChanges, getCurrentText, getVariableRefresh, isAllVariable } from './utils';
import {
containsVariable,
ensureStringValues,
findTemplateVarChanges,
getCurrentText,
getVariableRefresh,
isAllVariable,
} from './utils';
import { VariableRefresh } from './types';
import { UrlQueryMap } from '@grafana/data';
@ -174,3 +181,16 @@ describe('ensureStringValues', () => {
expect(ensureStringValues(value)).toEqual(expected);
});
});
describe('containsVariable', () => {
it.each`
value | expected
${''} | ${false}
${'$var'} | ${true}
${{ thing1: '${var}' }} | ${true}
${{ thing1: ['1', '${var}'] }} | ${true}
${{ thing1: { thing2: '${var}' } }} | ${true}
`('when called with value:$value then result should be:$expected', ({ value, expected }) => {
expect(containsVariable(value, 'var')).toEqual(expected);
});
});

View File

@ -1,4 +1,4 @@
import { isString, isArray, isEqual } from 'lodash';
import { isArray, isEqual } from 'lodash';
import { ScopedVars, UrlQueryMap, VariableType } from '@grafana/data';
import { getTemplateSrv } from '@grafana/runtime';
@ -6,6 +6,7 @@ import { ALL_VARIABLE_TEXT, ALL_VARIABLE_VALUE } from './state/types';
import { QueryVariableModel, VariableModel, VariableRefresh } from './types';
import { getTimeSrv } from '../dashboard/services/TimeSrv';
import { variableAdapters } from './adapters';
import { safeStringifyValue } from 'app/core/utils/explore';
/*
* This regex matches 3 types of variable reference with an optional format specifier
@ -51,7 +52,7 @@ export const getSearchFilterScopedVar = (args: {
export function containsVariable(...args: any[]) {
const variableName = args[args.length - 1];
args[0] = isString(args[0]) ? args[0] : Object['values'](args[0]).join(' ');
args[0] = typeof args[0] === 'string' ? args[0] : safeStringifyValue(args[0]);
const variableString = args.slice(0, -1).join(' ');
const matches = variableString.match(variableRegex);
const isMatchingVariable =