* default parsers support null
This commit is contained in:
AsafMah 2024-01-07 16:37:12 +02:00 коммит произвёл GitHub
Родитель 94efa7c688
Коммит 1d449c27cb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 36 добавлений и 11 удалений

Просмотреть файл

@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- When working with storage resources, use success/failure statistics and retry mechanism to improve ingestion stability
### Changed
- [BREAKING] - The default converters for DateTime and TimeSpan will now return null if the value is null or an empty string. This is to align with the behavior of the service.
## [5.2.3] - 2023-11-07
### Fixed

Просмотреть файл

@ -10,10 +10,10 @@ export enum WellKnownDataSet {
QueryProperties = "QueryProperties",
}
type DateTimeParser = (value: string) => any;
type TimeSpanParser = (value: string) => any;
type DateTimeParser = (value: string | null) => any;
type TimeSpanParser = (value: string | null) => any;
const defaultDatetimeParser: DateTimeParser = (t: string) => new Date(t);
const defaultDatetimeParser: DateTimeParser = (t: string | null) => (t ? new Date(t) : null);
const defaultTimespanParser: TimeSpanParser = parseKustoTimestampToMillis;
export interface Table {

Просмотреть файл

@ -8,9 +8,9 @@ export function toMilliseconds(hours: number, minutes: number, seconds: number)
// Format: [+|-]d.hh:mm:ss[.fffffff]
const TimespanRegex = /^(-?)(?:(\d+).)?(\d{2}):(\d{2}):(\d{2}(\.\d+)?$)/;
export function parseKustoTimestampToMillis(t: string | null): number {
if (t == null) {
return 0;
export function parseKustoTimestampToMillis(t: string | null): number | null {
if (t == null || t === "") {
return null;
}
const match = TimespanRegex.exec(t);
if (match) {

Просмотреть файл

@ -69,7 +69,7 @@ describe("KustoResultRow", () => {
for (let index = 0; index < inputColumns.length; index++) {
const currentActual = asJson[inputColumns[index].name as string];
if (typeof currentActual === "object") {
assert.strictEqual((currentActual as object).toString(), expectedValues[index].toString());
assert.strictEqual((currentActual as object).toString(), expectedValues[index]?.toString());
} else {
assert.strictEqual(currentActual, expectedValues[index]);
}
@ -83,8 +83,8 @@ describe("KustoResultRow", () => {
const actual = new KustoResultRow(
reverseOrderColumns.map((c, i) => new KustoResultColumn(c, rawColumns.length - i - 1)),
inputValues,
(t) => t + "-date",
(t) => t + "-time"
(t) => (t || "") + "-date",
(t) => (t || "") + "-time"
);
const asJson = actual.toJSON();
@ -100,6 +100,27 @@ describe("KustoResultRow", () => {
}
});
it.concurrent("default parsers nulls", () => {
const dates = ["2016-06-06T15:35:00Z", "", null];
const times = ["1.02:03:04.0050006", "", null];
const columns = [
new KustoResultColumn({ ColumnName: "date", ColumnType: "datetime" }, 0),
new KustoResultColumn({ ColumnName: "time", ColumnType: "timespan" }, 1),
];
const actual = [
new KustoResultRow(columns, [dates[0], times[0]]),
new KustoResultRow(columns, [dates[1], times[1]]),
new KustoResultRow(columns, [dates[2], times[2]]),
];
assert.strictEqual((actual[0].date as Date).toString(), new Date(dates[0]!).toString());
assert.strictEqual(actual[0].time, 93784005.0006);
assert.strictEqual(actual[1].date, null);
assert.strictEqual(actual[1].time, null);
assert.strictEqual(actual[2].date, null);
});
it.concurrent("mismatching data - less data than columns", () => {
const inputValues = ["2016-06-06T15:35:00Z", "foo", 101, 3.14, false, "1.02:03:04.567"];
@ -246,8 +267,8 @@ describe("KustoResultTable", () => {
});
it.concurrent("iterate over rows with custom parsers", () => {
const actual = new KustoResultTable(v2Response[2]);
const dateParser = (t: string) => t + "-date";
const timeParser = (t: string) => t + "-time";
const dateParser = (t: string | null) => (t || "") + "-date";
const timeParser = (t: string | null) => (t || "") + "-time";
actual.dateTimeParser = dateParser;
actual.timeSpanParser = timeParser;