Expressions: More robust expression check (#65006)

More robust expression check
This commit is contained in:
ismail simsek
2023-03-22 14:02:20 +01:00
committed by GitHub
parent aa857e2a4f
commit 1328878ace
4 changed files with 53 additions and 11 deletions

View File

@@ -2,16 +2,21 @@ import { of } from 'rxjs';
import { BackendSrv, BackendSrvRequest, FetchResponse } from 'src/services';
import {
DataSourceJsonData,
DataQuery,
DataSourceInstanceSettings,
DataQueryRequest,
DataQueryResponseData,
MutableDataFrame,
DataSourceInstanceSettings,
DataSourceJsonData,
DataSourceRef,
MutableDataFrame,
} from '@grafana/data';
import { DataSourceWithBackend, standardStreamOptionsProvider, toStreamingDataResponse } from './DataSourceWithBackend';
import {
DataSourceWithBackend,
isExpressionReference,
standardStreamOptionsProvider,
toStreamingDataResponse,
} from './DataSourceWithBackend';
class MyDataSource extends DataSourceWithBackend<DataQuery, DataSourceJsonData> {
constructor(instanceSettings: DataSourceInstanceSettings<DataSourceJsonData>) {
@@ -239,6 +244,16 @@ describe('DataSourceWithBackend', () => {
url: '/api/datasources/uid/abc/health',
});
});
describe('isExpressionReference', () => {
test('check all possible expression references', () => {
expect(isExpressionReference('__expr__')).toBeTruthy(); // New UID
expect(isExpressionReference('-100')).toBeTruthy(); // Legacy UID
expect(isExpressionReference('Expression')).toBeTruthy(); // Name
expect(isExpressionReference({ type: '__expr__' })).toBeTruthy();
expect(isExpressionReference({ type: '-100' })).toBeTruthy();
});
});
});
function createMockDatasource() {

View File

@@ -47,7 +47,7 @@ export function isExpressionReference(ref?: DataSourceRef | string | null): bool
return false;
}
const v = typeof ref === 'string' ? ref : ref.type;
return v === ExpressionDatasourceRef.type || v === '-100'; // -100 was a legacy accident that should be removed
return v === ExpressionDatasourceRef.type || v === ExpressionDatasourceRef.name || v === '-100'; // -100 was a legacy accident that should be removed
}
export class HealthCheckError extends Error {