@malloy-publisher/sdk 0.0.382-dev → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +115 -95
- package/TREE_SHAKING.md +16 -11
- package/bunfig.toml +7 -0
- package/dist/ServerProvider-Cn_LhKda.cjs.js +1 -0
- package/dist/ServerProvider-sfabtriY.es.js +4076 -0
- package/dist/client/api.d.ts +3773 -1515
- package/dist/client/configuration.d.ts +1 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.es.js +86 -59
- package/dist/components/Connections/EditConnectionDialog.d.ts +2 -1
- package/dist/components/Connections/common.d.ts +8 -2
- package/dist/components/Dashboard/Dashboard.d.ts +62 -0
- package/dist/components/Dashboard/DashboardTile.d.ts +57 -0
- package/dist/components/Dashboard/index.d.ts +2 -0
- package/dist/components/DataAppViewer/DataAppViewer.d.ts +23 -0
- package/dist/components/DataAppViewer/index.d.ts +1 -0
- package/dist/components/Environment/Environment.d.ts +6 -0
- package/dist/components/Environment/index.d.ts +2 -0
- package/dist/components/Home/AddEnvironmentDialog.d.ts +1 -0
- package/dist/components/Home/DeleteEnvironmentDialog.d.ts +5 -0
- package/dist/components/Home/EditEnvironmentDialog.d.ts +7 -0
- package/dist/components/Home/Home.d.ts +2 -2
- package/dist/components/Materializations/CreateEnvMaterializationDialog.d.ts +14 -0
- package/dist/components/Materializations/CreateMaterializationDialog.d.ts +10 -0
- package/dist/components/Materializations/DeleteMaterializationDialog.d.ts +7 -0
- package/dist/components/Materializations/EnvironmentMaterializations.d.ts +7 -0
- package/dist/components/Materializations/ManifestView.d.ts +13 -0
- package/dist/components/Materializations/MaterializationDetailDialog.d.ts +9 -0
- package/dist/components/Materializations/MaterializationRunsList.d.ts +15 -0
- package/dist/components/Materializations/Materializations.d.ts +7 -0
- package/dist/components/Materializations/ScheduleCard.d.ts +20 -0
- package/dist/components/Materializations/SectionLabel.d.ts +10 -0
- package/dist/components/Materializations/SetScheduleDialog.d.ts +11 -0
- package/dist/components/Materializations/TriggerChip.d.ts +10 -0
- package/dist/components/Materializations/cron.d.ts +23 -0
- package/dist/components/Materializations/index.d.ts +2 -0
- package/dist/components/Materializations/utils.d.ts +46 -0
- package/dist/components/Notebook/Notebook.d.ts +47 -4
- package/dist/components/Notebook/NotebookCell.d.ts +37 -1
- package/dist/components/Notebook/runPlan.d.ts +29 -0
- package/dist/components/Notebook/types.d.ts +10 -0
- package/dist/components/Package/ContentTypeIcon.d.ts +31 -0
- package/dist/components/Package/Package.d.ts +1 -4
- package/dist/components/RenderedResult/RenderedResult.d.ts +7 -1
- package/dist/components/RenderedResult/ResultContainer.d.ts +5 -1
- package/dist/components/RenderedResult/renderLogs.d.ts +6 -0
- package/dist/components/ResultsDialog.d.ts +6 -1
- package/dist/components/ServerProvider.d.ts +28 -4
- package/dist/components/click_helper.d.ts +10 -1
- package/dist/components/drill/index.d.ts +3 -0
- package/dist/components/drill/markDrillableCells.d.ts +55 -0
- package/dist/components/drill/resolveDrill.d.ts +139 -0
- package/dist/components/drill/useDrill.d.ts +63 -0
- package/dist/components/given/GivenInput.d.ts +52 -0
- package/dist/components/given/GivensPanel.d.ts +47 -0
- package/dist/components/given/filterValue.d.ts +97 -0
- package/dist/components/given/index.d.ts +2 -0
- package/dist/components/given/paramCodec.d.ts +60 -0
- package/dist/components/given/utils.d.ts +17 -0
- package/dist/components/highlighter.d.ts +6 -1
- package/dist/components/index.d.ts +7 -2
- package/dist/components/styles.d.ts +47 -0
- package/dist/constants/docLinks.d.ts +12 -0
- package/dist/{core-YNpOLuB1.cjs.js → core-CqRKhtoy.cjs.js} +1 -1
- package/dist/{core-CjeTkq8O.es.js → core-DyU47reh.es.js} +1 -1
- package/dist/github-dark-BKL9xNgN.cjs.js +1 -0
- package/dist/github-dark-DenFmJkN.es.js +4 -0
- package/dist/hooks/givenValue.d.ts +10 -0
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/useDimensionFiltersFromSpec.d.ts +3 -3
- package/dist/hooks/useDimensionFiltersQuery.d.ts +3 -3
- package/dist/hooks/useDimensionalFilterRangeData.d.ts +4 -4
- package/dist/hooks/useGivensState.d.ts +64 -0
- package/dist/hooks/useModelGivens.d.ts +10 -0
- package/dist/hooks/useSuggestOptions.d.ts +78 -0
- package/dist/{index-DRDu9kIV.es.js → index-CAkFE4Ol.es.js} +41278 -28504
- package/dist/index-DD4b_bt0.cjs.js +356 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.es.js +79 -48
- package/dist/malloy-explorer.css +12 -1
- package/dist/sdk.css +1 -1
- package/dist/theme/ThemeContext.d.ts +45 -0
- package/dist/theme/buildMalloyExplicitTheme.d.ts +34 -0
- package/dist/theme/buildTableCssVars.d.ts +41 -0
- package/dist/theme/buildVegaThemeOverride.d.ts +16 -0
- package/dist/theme/defaults.d.ts +7 -0
- package/dist/theme/index.d.ts +8 -0
- package/dist/theme/keys.d.ts +20 -0
- package/dist/theme/readChartAnnotations.d.ts +23 -0
- package/dist/theme/resolveTheme.d.ts +22 -0
- package/dist/theme/types.d.ts +70 -0
- package/dist/utils/dataAppEmbed.d.ts +44 -0
- package/dist/utils/formatting.d.ts +1 -1
- package/dist/utils/malloyString.d.ts +7 -0
- package/dist/utils/parsing.d.ts +2 -2
- package/dist/utils/queryClient.d.ts +5 -0
- package/package.json +19 -8
- package/src/client-entry.ts +3 -0
- package/src/components/AnalyzePackageButton.tsx +9 -3
- package/src/components/ApiErrorDisplay.tsx +3 -0
- package/src/components/Connections/AddConnectionDialog.tsx +132 -25
- package/src/components/Connections/DeleteConnectionDialog.tsx +14 -3
- package/src/components/Connections/EditConnectionDialog.tsx +167 -37
- package/src/components/Connections/common.ts +94 -3
- package/src/components/DOMElement/DOMElement.tsx +3 -0
- package/src/components/DOMElement/index.ts +3 -0
- package/src/components/Dashboard/Dashboard.spec.tsx +216 -0
- package/src/components/Dashboard/Dashboard.tsx +458 -0
- package/src/components/Dashboard/DashboardTile.spec.tsx +81 -0
- package/src/components/Dashboard/DashboardTile.tsx +199 -0
- package/src/components/Dashboard/index.ts +5 -0
- package/src/components/Dashboard/tileGridColumn.spec.ts +35 -0
- package/src/components/DataAppViewer/DataAppViewer.tsx +214 -0
- package/src/components/DataAppViewer/index.ts +4 -0
- package/src/components/{Project → Environment}/About.tsx +11 -4
- package/src/components/Environment/AddPackageDialog.tsx +202 -0
- package/src/components/{Project → Environment}/ConnectionExplorer.tsx +17 -14
- package/src/components/Environment/Connections.tsx +391 -0
- package/src/components/{Project → Environment}/DeletePackageDialog.tsx +8 -4
- package/src/components/{Project → Environment}/EditPackageDialog.tsx +21 -13
- package/src/components/Environment/Environment.tsx +89 -0
- package/src/components/Environment/Packages.tsx +206 -0
- package/src/components/Environment/index.ts +5 -0
- package/src/components/Home/{AddProjectDialog.tsx → AddEnvironmentDialog.tsx} +25 -19
- package/src/components/Home/{DeleteProjectDialog.tsx → DeleteEnvironmentDialog.tsx} +20 -15
- package/src/components/Home/{EditProjectDialog.tsx → EditEnvironmentDialog.tsx} +35 -28
- package/src/components/Home/Home.tsx +386 -408
- package/src/components/Home/index.ts +3 -0
- package/src/components/Loading.tsx +3 -0
- package/src/components/Materializations/CreateEnvMaterializationDialog.tsx +179 -0
- package/src/components/Materializations/CreateMaterializationDialog.tsx +112 -0
- package/src/components/Materializations/DeleteMaterializationDialog.tsx +109 -0
- package/src/components/Materializations/EnvironmentMaterializations.tsx +267 -0
- package/src/components/Materializations/ManifestView.tsx +87 -0
- package/src/components/Materializations/MaterializationDetailDialog.tsx +259 -0
- package/src/components/Materializations/MaterializationRunsList.tsx +256 -0
- package/src/components/Materializations/Materializations.tsx +355 -0
- package/src/components/Materializations/ScheduleCard.tsx +231 -0
- package/src/components/Materializations/SectionLabel.tsx +30 -0
- package/src/components/Materializations/SetScheduleDialog.tsx +162 -0
- package/src/components/Materializations/TriggerChip.tsx +44 -0
- package/src/components/Materializations/cron.ts +124 -0
- package/src/components/Materializations/index.ts +5 -0
- package/src/components/Materializations/utils.ts +145 -0
- package/src/components/Model/Model.tsx +44 -2
- package/src/components/Model/ModelCell.tsx +14 -5
- package/src/components/Model/ModelExplorer.tsx +3 -0
- package/src/components/Model/ModelExplorerDialog.tsx +4 -1
- package/src/components/Model/SourcesExplorer.tsx +9 -6
- package/src/components/Model/index.ts +3 -0
- package/src/components/Model/useModelData.ts +6 -3
- package/src/components/Notebook/Notebook.spec.tsx +88 -0
- package/src/components/Notebook/Notebook.tsx +476 -339
- package/src/components/Notebook/NotebookCell.tsx +264 -25
- package/src/components/Notebook/index.ts +3 -0
- package/src/components/Notebook/runPlan.spec.ts +155 -0
- package/src/components/Notebook/runPlan.ts +67 -0
- package/src/components/Notebook/types.ts +14 -0
- package/src/components/Package/ContentTypeIcon.spec.ts +91 -0
- package/src/components/Package/ContentTypeIcon.tsx +177 -0
- package/src/components/Package/FileTreeView.tsx +3 -0
- package/src/components/Package/Models.tsx +7 -4
- package/src/components/Package/Package.spec.tsx +113 -0
- package/src/components/Package/Package.tsx +692 -59
- package/src/components/Package/index.ts +3 -0
- package/src/components/QueryResult/QueryResult.tsx +17 -8
- package/src/components/QueryResult/index.ts +3 -0
- package/src/components/RenderedResult/RenderedResult.tsx +725 -67
- package/src/components/RenderedResult/ResultContainer.tsx +71 -2
- package/src/components/RenderedResult/index.ts +3 -0
- package/src/components/RenderedResult/renderLogs.spec.ts +58 -0
- package/src/components/RenderedResult/renderLogs.ts +43 -0
- package/src/components/ResultsDialog.tsx +13 -1
- package/src/components/ServerProvider.tsx +175 -51
- package/src/components/Workbook/BrowserWorkbookStorage.ts +3 -0
- package/src/components/Workbook/EditableMalloyCell.tsx +3 -0
- package/src/components/Workbook/ModelPicker.tsx +7 -4
- package/src/components/Workbook/MutableCell.tsx +10 -5
- package/src/components/Workbook/Workbook.tsx +8 -5
- package/src/components/Workbook/WorkbookList.tsx +3 -0
- package/src/components/Workbook/WorkbookManager.ts +3 -0
- package/src/components/Workbook/WorkbookStorage.ts +3 -0
- package/src/components/Workbook/WorkbookStorageProvider.tsx +3 -0
- package/src/components/Workbook/index.ts +3 -0
- package/src/components/click_helper.ts +17 -1
- package/src/components/drill/index.ts +28 -0
- package/src/components/drill/markDrillableCells.dom.spec.ts +313 -0
- package/src/components/drill/markDrillableCells.spec.ts +109 -0
- package/src/components/drill/markDrillableCells.ts +314 -0
- package/src/components/drill/resolveDrill.spec.ts +449 -0
- package/src/components/drill/resolveDrill.ts +375 -0
- package/src/components/drill/useDrill.tsx +203 -0
- package/src/components/filter/DimensionFilter.tsx +3 -0
- package/src/components/filter/index.ts +3 -0
- package/src/components/filter/utils.ts +16 -5
- package/src/components/given/GivenInput.spec.tsx +689 -0
- package/src/components/given/GivenInput.tsx +801 -0
- package/src/components/given/GivensPanel.spec.tsx +175 -0
- package/src/components/given/GivensPanel.tsx +195 -0
- package/src/components/given/filterValue.spec.ts +364 -0
- package/src/components/given/filterValue.ts +207 -0
- package/src/components/given/index.ts +9 -0
- package/src/components/given/paramCodec.spec.ts +857 -0
- package/src/components/given/paramCodec.ts +483 -0
- package/src/components/given/utils.spec.ts +60 -0
- package/src/components/given/utils.ts +79 -0
- package/src/components/highlighter.ts +22 -5
- package/src/components/index.ts +10 -2
- package/src/components/styles.ts +82 -33
- package/src/constants/docLinks.spec.ts +54 -0
- package/src/constants/docLinks.ts +46 -0
- package/src/hooks/givenValue.ts +13 -0
- package/src/hooks/index.ts +13 -0
- package/src/hooks/useDimensionFilters.ts +4 -7
- package/src/hooks/useDimensionFiltersFromSpec.ts +8 -5
- package/src/hooks/useDimensionFiltersQuery.ts +10 -13
- package/src/hooks/useDimensionalFilterRangeData.ts +11 -14
- package/src/hooks/useGivensState.spec.ts +449 -0
- package/src/hooks/useGivensState.ts +340 -0
- package/src/hooks/useModelGivens.ts +29 -0
- package/src/hooks/useQueryWithApiError.ts +3 -0
- package/src/hooks/useRawQueryData.ts +6 -3
- package/src/hooks/useSuggestOptions.dom.spec.ts +91 -0
- package/src/hooks/useSuggestOptions.spec.ts +112 -0
- package/src/hooks/useSuggestOptions.ts +268 -0
- package/src/index.ts +10 -1
- package/src/react-app-env.d.ts +3 -0
- package/src/theme/ThemeContext.tsx +86 -0
- package/src/theme/buildMalloyExplicitTheme.ts +61 -0
- package/src/theme/buildTableCssVars.spec.ts +86 -0
- package/src/theme/buildTableCssVars.ts +98 -0
- package/src/theme/buildVegaThemeOverride.spec.ts +56 -0
- package/src/theme/buildVegaThemeOverride.ts +47 -0
- package/src/theme/defaults.ts +78 -0
- package/src/theme/index.ts +11 -0
- package/src/theme/keys.ts +32 -0
- package/src/theme/readChartAnnotations.spec.ts +58 -0
- package/src/theme/readChartAnnotations.ts +69 -0
- package/src/theme/resolveTheme.spec.ts +160 -0
- package/src/theme/resolveTheme.ts +121 -0
- package/src/theme/types.ts +79 -0
- package/src/utils/dataAppEmbed.spec.ts +63 -0
- package/src/utils/dataAppEmbed.ts +84 -0
- package/src/utils/formatting.spec.ts +25 -21
- package/src/utils/formatting.ts +11 -7
- package/src/utils/malloyString.spec.ts +24 -0
- package/src/utils/malloyString.ts +12 -0
- package/src/utils/parsing.spec.ts +26 -19
- package/src/utils/parsing.ts +11 -6
- package/src/utils/queryClient.ts +17 -0
- package/test/README.md +185 -0
- package/test/serverProvider.tsx +72 -0
- package/test/setup.ts +29 -0
- package/tsconfig.json +12 -1
- package/tsconfig.spec.json +22 -0
- package/vite.config.ts +3 -0
- package/dist/ServerProvider-lOQXDlqB.cjs.js +0 -1
- package/dist/ServerProvider-on-8FH5Z.es.js +0 -3060
- package/dist/components/Home/AddProjectDialog.d.ts +0 -1
- package/dist/components/Home/DeleteProjectDialog.d.ts +0 -5
- package/dist/components/Home/EditProjectDialog.d.ts +0 -7
- package/dist/components/Package/Config.d.ts +0 -5
- package/dist/components/Package/Databases.d.ts +0 -5
- package/dist/components/Package/Notebooks.d.ts +0 -6
- package/dist/components/Project/Project.d.ts +0 -6
- package/dist/components/Project/index.d.ts +0 -2
- package/dist/index-BOLBP6_i.cjs.js +0 -233
- package/src/components/Package/Config.tsx +0 -97
- package/src/components/Package/Connections.tsx +0 -373
- package/src/components/Package/Databases.tsx +0 -228
- package/src/components/Package/Notebooks.tsx +0 -77
- package/src/components/Project/AddPackageDialog.tsx +0 -159
- package/src/components/Project/Packages.tsx +0 -191
- package/src/components/Project/Project.tsx +0 -52
- package/src/components/Project/index.ts +0 -2
- /package/dist/components/{Project → Environment}/About.d.ts +0 -0
- /package/dist/components/{Project → Environment}/AddPackageDialog.d.ts +0 -0
- /package/dist/components/{Project → Environment}/ConnectionExplorer.d.ts +0 -0
- /package/dist/components/{Package → Environment}/Connections.d.ts +0 -0
- /package/dist/components/{Project → Environment}/DeletePackageDialog.d.ts +0 -0
- /package/dist/components/{Project → Environment}/EditPackageDialog.d.ts +0 -0
- /package/dist/components/{Project → Environment}/Packages.d.ts +0 -0
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Between a given's value and the two wire forms it takes: a URL query
|
|
6
|
+
* parameter, and the `givens` map on a query request.
|
|
7
|
+
*
|
|
8
|
+
* Given state is URL-addressable so a filtered view is a link you can send,
|
|
9
|
+
* and so a drill can arrive somewhere with its filter already applied. A URL
|
|
10
|
+
* carries strings, while a given is typed, so a value makes a round trip
|
|
11
|
+
* through here on the way out to the address bar and back.
|
|
12
|
+
*
|
|
13
|
+
* The declared type is what decides the reading: `"100"` is the number 100 for
|
|
14
|
+
* a `number` given and the string `"100"` for a `filter<number>`, whose values
|
|
15
|
+
* are filter syntax rather than numbers.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { GivenValue } from "../../hooks/givenValue";
|
|
19
|
+
import { isFilterType } from "./filterValue";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A given's value as a query-parameter string, or undefined when there is
|
|
23
|
+
* nothing to put in the URL (an unset given, which the server fills from the
|
|
24
|
+
* model default).
|
|
25
|
+
*
|
|
26
|
+
* An empty string is a real value, not an absence: for a `filter<…>` it is the
|
|
27
|
+
* empty filter, i.e. "All", which is meaningfully different from leaving the
|
|
28
|
+
* declared default in place.
|
|
29
|
+
*/
|
|
30
|
+
export function givenToParam(
|
|
31
|
+
value: GivenValue | undefined,
|
|
32
|
+
// Required, not optional: the only thing this decides is a Date's spelling,
|
|
33
|
+
// and a caller that forgot it would silently truncate a timestamp to its
|
|
34
|
+
// date, which is the defect this parameter exists to fix. Better to fail to
|
|
35
|
+
// compile than to lose the time of day again.
|
|
36
|
+
type: string | undefined,
|
|
37
|
+
): string | undefined {
|
|
38
|
+
if (value === undefined || value === null) return undefined;
|
|
39
|
+
// Written in the same spelling the request body uses, so the URL keeps a
|
|
40
|
+
// timestamp's time of day. Encoding every Date as `YYYY-MM-DD` truncated a
|
|
41
|
+
// `timestamp` or `timestamptz` given the moment it round-tripped through the
|
|
42
|
+
// address bar, and the query then re-ran at midnight against a different
|
|
43
|
+
// instant than the reader had chosen.
|
|
44
|
+
if (value instanceof Date) return dateToRequest(value, type);
|
|
45
|
+
// Refused, not joined. `GivenValue` used to admit `string[]` and this joined
|
|
46
|
+
// on `,` with no escaping, which this codec cannot undo: `paramToGiven`
|
|
47
|
+
// never returns an array for any type, so the list came back as one string,
|
|
48
|
+
// and a value CONTAINING a comma ("Ben & Jerry, Inc") could not even be
|
|
49
|
+
// split back to the right count. The type no longer promises arrays; this
|
|
50
|
+
// stays as a guard for a JavaScript caller the types cannot reach, where
|
|
51
|
+
// dropping the parameter is at least visible as "unset" rather than sending
|
|
52
|
+
// a value the reader never chose.
|
|
53
|
+
if (Array.isArray(value)) return undefined;
|
|
54
|
+
return String(value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The three calendar fields a picked day is read from, as dayjs exposes them. */
|
|
58
|
+
export interface PickedDay {
|
|
59
|
+
year(): number;
|
|
60
|
+
/** Zero-based, as `Date` and dayjs both have it. */
|
|
61
|
+
month(): number;
|
|
62
|
+
date(): number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The instant to store for a calendar day the user picked in a date control.
|
|
67
|
+
*
|
|
68
|
+
* Built in UTC from the day's own fields, NOT by taking the instant off the
|
|
69
|
+
* picker. MUI's adapter resolves its default timezone to LOCAL here (this SDK
|
|
70
|
+
* loads dayjs's `utc` plugin but not `timezone`), so the value it hands back is
|
|
71
|
+
* local midnight-ish at the reference time-of-day, while both the read side of
|
|
72
|
+
* the control and {@link dateToRequest} take the UTC day. Those two readings
|
|
73
|
+
* differ by a day for any offset that crosses midnight: in Sydney, clicking
|
|
74
|
+
* 5 January stored 4 January and the field then redrew as the 4th, so the click
|
|
75
|
+
* appeared to jump backwards.
|
|
76
|
+
*/
|
|
77
|
+
export function pickedDayToUtc(day: PickedDay, previous?: Date): Date {
|
|
78
|
+
// The time of day is carried over from the value being replaced, because a
|
|
79
|
+
// DatePicker offers no way to re-enter it. Zeroing it instead silently moved
|
|
80
|
+
// a `timestamp` given's boundary back to midnight every time the user
|
|
81
|
+
// adjusted the day, which is a different filter, not a re-spelling of the
|
|
82
|
+
// same one. Nothing to carry (a fresh pick, or a `date` given) means
|
|
83
|
+
// midnight, which is what a bare calendar day means anyway.
|
|
84
|
+
return new Date(
|
|
85
|
+
Date.UTC(
|
|
86
|
+
day.year(),
|
|
87
|
+
day.month(),
|
|
88
|
+
day.date(),
|
|
89
|
+
previous?.getUTCHours() ?? 0,
|
|
90
|
+
previous?.getUTCMinutes() ?? 0,
|
|
91
|
+
previous?.getUTCSeconds() ?? 0,
|
|
92
|
+
previous?.getUTCMilliseconds() ?? 0,
|
|
93
|
+
),
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The spellings {@link parseTemporal} accepts: an ISO-shaped civil datetime with
|
|
99
|
+
* an optional zone suffix. Deliberately strict, and deliberately the only thing
|
|
100
|
+
* that decides.
|
|
101
|
+
*/
|
|
102
|
+
const TEMPORAL =
|
|
103
|
+
/^(\d{4})-(\d{1,2})-(\d{1,2})(?:[T\s]+(\d{1,2}):(\d{2})(?::(\d{2}))?(?:\.(\d{1,9}))?)?\s*(Z|[+-]\d{2}:?\d{2}|[+-]\d{2}|UTC|GMT)?$/i;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* A value with its offset `+` put back, if the address bar ate one.
|
|
107
|
+
*
|
|
108
|
+
* A `+` in a query string means a SPACE, so an offset survives only when the
|
|
109
|
+
* writer percent-encoded it. `URLSearchParams` hands us `2024-01-05T10:30:00
|
|
110
|
+
* 05:00` for a hand-pasted `+05:00`, and refusing that made the parser work for
|
|
111
|
+
* the western half of the world and not the eastern: `-05:00` arrived intact
|
|
112
|
+
* and `+05:00` did not, with nothing on screen to say the URL had eaten it
|
|
113
|
+
* rather than the grammar rejecting it.
|
|
114
|
+
*
|
|
115
|
+
* Applied only when a full datetime precedes the group, so nothing else can
|
|
116
|
+
* match, and reached only by the three temporal types (`date`, `timestamp`,
|
|
117
|
+
* `timestamptz`), so it cannot touch a `string` or `filter<…>` value. Kept
|
|
118
|
+
* apart from `parseTemporal` so a REFUSED value is still reported back as it was
|
|
119
|
+
* typed, rather than in the mangled space form the URL delivered.
|
|
120
|
+
*/
|
|
121
|
+
function restoreUrlPlus(raw: string): string {
|
|
122
|
+
const trimmed = raw.trim();
|
|
123
|
+
// Whitespace and nothing else: hand back `raw` untouched. Trimming it yields
|
|
124
|
+
// `""`, which has already passed the empty-means-unset check above, so the
|
|
125
|
+
// given was sent as an empty date while its control read as unset.
|
|
126
|
+
//
|
|
127
|
+
// Only that case. An earlier form of this guard returned `raw` whenever no
|
|
128
|
+
// `+` was restored, which stopped trimming ordinary values too: ` 2024-01-05`
|
|
129
|
+
// was then refused while ` 2024-01-05T10:30:00 05:00` still parsed, since the
|
|
130
|
+
// restoring path trimmed and the non-restoring path no longer did. Same
|
|
131
|
+
// leading space, opposite answers, decided by whether the URL had eaten a `+`.
|
|
132
|
+
if (trimmed === "") return raw;
|
|
133
|
+
return trimmed.replace(
|
|
134
|
+
/^(\d{4}-\d{1,2}-\d{1,2}[T\s]+\d{1,2}:\d{2}(?::\d{2})?(?:\.\d{1,9})?)\s+(\d{2}:?\d{2}|\d{2})$/,
|
|
135
|
+
"$1+$2",
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A date or datetime parameter as an instant, or undefined when it is not one.
|
|
141
|
+
*
|
|
142
|
+
* The zone is parsed HERE rather than handed to `new Date`, which is the point
|
|
143
|
+
* of this function and took four attempts to reach. Outside the exact ISO
|
|
144
|
+
* grammar `new Date` is implementation-defined, so delegating to it made the
|
|
145
|
+
* answer depend on the reader's browser. Measured on this machine:
|
|
146
|
+
* `new Date("2024-01-05 10:30:00 UTC")`, which is what BigQuery prints for a
|
|
147
|
+
* TIMESTAMP, gives the right instant in V8 and in Bun and **Invalid Date in
|
|
148
|
+
* Safari's JavaScriptCore**. A unit suite running under Bun cannot see that, so
|
|
149
|
+
* a green test bench would have shipped it.
|
|
150
|
+
*
|
|
151
|
+
* Everything the earlier attempts got wrong is a case here:
|
|
152
|
+
*
|
|
153
|
+
* - **No zone means UTC**, built from the value's own parts. `new Date` reads an
|
|
154
|
+
* offset-less datetime as LOCAL, so one shared link resolved to a different
|
|
155
|
+
* instant per reader and was rewritten to the shifted one on the next report.
|
|
156
|
+
* - **The parts are checked, not just parsed.** `Date.UTC` normalises rather
|
|
157
|
+
* than refusing, so `2024-13-45` arrived as 2025-02-14, silently repairing a
|
|
158
|
+
* mistyped link to a date nobody chose.
|
|
159
|
+
* - **A meridiem is not a zone.** A gate of `\s[A-Z]{2,5}$` matches ` AM`, which
|
|
160
|
+
* put `Dec 25 2024 10:30 AM` back to being read as local time.
|
|
161
|
+
* - **Two-digit offsets count.** `+00` and `-05` are what Postgres and DuckDB
|
|
162
|
+
* print for a TIMESTAMPTZ, and demanding four digits rejected the default
|
|
163
|
+
* output of the two databases Publisher ships with.
|
|
164
|
+
*
|
|
165
|
+
* A spelling this grammar does not cover (`Dec 25 2024`, `12/25/2024`) is passed
|
|
166
|
+
* through, and the server names the given in its error. Narrower than what V8
|
|
167
|
+
* alone would take, on purpose: an answer that depends on the browser is worse
|
|
168
|
+
* than a refusal that does not.
|
|
169
|
+
*/
|
|
170
|
+
function parseTemporal(raw: string, calendarDay = false): Date | undefined {
|
|
171
|
+
// A `+` in a query string means a SPACE, so an offset survives the address
|
|
172
|
+
// bar only when the writer percent-encoded it. `URLSearchParams` hands us
|
|
173
|
+
// `2024-01-05T10:30:00 05:00` for a hand-pasted `+05:00`, and refusing that
|
|
174
|
+
// made the parser work for the western half of the world and not the
|
|
175
|
+
// eastern: `-05:00` arrived intact and `+05:00` did not, with nothing on
|
|
176
|
+
// screen to say the URL had eaten it rather than the grammar. Read back as
|
|
177
|
+
// `+` only when a full datetime precedes it, so nothing else can match.
|
|
178
|
+
const text = restoreUrlPlus(raw);
|
|
179
|
+
if (text === "") return undefined;
|
|
180
|
+
|
|
181
|
+
const m = TEMPORAL.exec(text);
|
|
182
|
+
if (!m) return undefined;
|
|
183
|
+
const [, y, mo, d, h, mi, sec, frac, zone] = m;
|
|
184
|
+
|
|
185
|
+
const parts = {
|
|
186
|
+
year: Number(y),
|
|
187
|
+
month: Number(mo),
|
|
188
|
+
day: Number(d),
|
|
189
|
+
hour: Number(h ?? 0),
|
|
190
|
+
minute: Number(mi ?? 0),
|
|
191
|
+
second: Number(sec ?? 0),
|
|
192
|
+
// Truncated to milliseconds, which is all a `Date` holds. Accepting more
|
|
193
|
+
// digits than that is the point: warehouses print microseconds as a
|
|
194
|
+
// matter of course, and capping the pattern at three meant a perfectly
|
|
195
|
+
// ordinary `...:00.123456` failed to parse at all.
|
|
196
|
+
milli: Number((frac ?? "").slice(0, 3).padEnd(3, "0") || 0),
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const civil = Date.UTC(
|
|
200
|
+
parts.year,
|
|
201
|
+
parts.month - 1,
|
|
202
|
+
parts.day,
|
|
203
|
+
parts.hour,
|
|
204
|
+
parts.minute,
|
|
205
|
+
parts.second,
|
|
206
|
+
parts.milli,
|
|
207
|
+
);
|
|
208
|
+
// Read the parts back off the result. `Date.UTC` normalises rather than
|
|
209
|
+
// refusing, so `2024-13-45` became 2025-02-14 and, because the value is
|
|
210
|
+
// reported back outward, a mistyped link was silently REPAIRED to a date the
|
|
211
|
+
// reader never typed.
|
|
212
|
+
const built = new Date(civil);
|
|
213
|
+
const survived =
|
|
214
|
+
built.getUTCFullYear() === parts.year &&
|
|
215
|
+
built.getUTCMonth() === parts.month - 1 &&
|
|
216
|
+
built.getUTCDate() === parts.day &&
|
|
217
|
+
built.getUTCHours() === parts.hour &&
|
|
218
|
+
built.getUTCMinutes() === parts.minute &&
|
|
219
|
+
built.getUTCSeconds() === parts.second;
|
|
220
|
+
if (!survived) return undefined;
|
|
221
|
+
|
|
222
|
+
// A zone qualifies a time of day, so it is meaningless on a bare date and
|
|
223
|
+
// that combination is refused rather than resolved: `2024-01-05+05`
|
|
224
|
+
// otherwise became 2024-01-04T19:00Z, reported back as the 4th, a day
|
|
225
|
+
// earlier than what was written and silently rewritten into the URL.
|
|
226
|
+
//
|
|
227
|
+
// This catches the literal spelling, NOT the one a URL delivers. A pasted
|
|
228
|
+
// `2024-01-05+05:00` arrives as `2024-01-05 05:00`, which is a well-formed
|
|
229
|
+
// time of day and is read as one; `restoreUrlPlus` deliberately does not
|
|
230
|
+
// fire without a full datetime in front, because there is nothing to
|
|
231
|
+
// distinguish the two readings and guessing wrong is worse than either.
|
|
232
|
+
if (zone !== undefined && h === undefined) return undefined;
|
|
233
|
+
|
|
234
|
+
const offset = zoneOffsetMinutes(zone);
|
|
235
|
+
if (offset === undefined) return undefined;
|
|
236
|
+
const instant = new Date(civil - offset * 60_000);
|
|
237
|
+
|
|
238
|
+
// For a `date` given, and only for a value that reached here CARRYING A TIME
|
|
239
|
+
// (a bare date with a zone was already refused above, for every type), refuse
|
|
240
|
+
// a zone just when applying it MOVES THE DAY. The qualifier is spelled out
|
|
241
|
+
// rather than left implied by position: stating this rule without it reads
|
|
242
|
+
// as "a `date` accepts `Z`", which is false for a bare date.
|
|
243
|
+
//
|
|
244
|
+
// Refusing every zone was the previous attempt and it was far too broad: it
|
|
245
|
+
// rejected `Z`, `UTC` and `+00:00`, so `2024-01-05T00:00:00.000Z`, which is
|
|
246
|
+
// exactly what `Date#toISOString()` emits and therefore what any host or
|
|
247
|
+
// link generator produces, stopped resolving at all. What the day-shift
|
|
248
|
+
// defect actually needs is this comparison, and nothing wider.
|
|
249
|
+
if (
|
|
250
|
+
calendarDay &&
|
|
251
|
+
zone !== undefined &&
|
|
252
|
+
instant.getUTCDate() !== parts.day
|
|
253
|
+
) {
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
return instant;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Minutes east of UTC for a matched zone suffix; absent means the value is
|
|
261
|
+
* naive, so UTC. Undefined for a suffix that is shaped like an offset but is
|
|
262
|
+
* not one.
|
|
263
|
+
*
|
|
264
|
+
* The range check is the same principle as the parts-survived check above:
|
|
265
|
+
* refuse rather than silently produce a different instant. Without it `+99:99`
|
|
266
|
+
* was read as 99 hours and 99 minutes, so `2024-01-05T10:30:00+99:99` resolved
|
|
267
|
+
* to 1 January, four days off, with nothing to say so. Real offsets run from
|
|
268
|
+
* -12:00 to +14:00 (Kiribati is the outer edge) and their minutes are always
|
|
269
|
+
* under 60.
|
|
270
|
+
*/
|
|
271
|
+
function zoneOffsetMinutes(zone: string | undefined): number | undefined {
|
|
272
|
+
if (zone === undefined) return 0;
|
|
273
|
+
const z = zone.toUpperCase();
|
|
274
|
+
if (z === "Z" || z === "UTC" || z === "GMT") return 0;
|
|
275
|
+
const m = /^([+-])(\d{2}):?(\d{2})?$/.exec(z);
|
|
276
|
+
if (!m) return undefined;
|
|
277
|
+
const mins = Number(m[3] ?? 0);
|
|
278
|
+
if (mins > 59) return undefined;
|
|
279
|
+
const minutes = Number(m[2]) * 60 + mins;
|
|
280
|
+
// Not symmetric, whatever the tidier `Math.abs` version would suggest. The
|
|
281
|
+
// real range runs -12:00 (Baker Island) to +14:00 (Kiribati), so a single
|
|
282
|
+
// ±14:00 bound admitted -13:00 and -14:00, which exist nowhere.
|
|
283
|
+
const east = m[1] !== "-";
|
|
284
|
+
if (minutes > (east ? 14 * 60 : 12 * 60)) return undefined;
|
|
285
|
+
return east ? minutes : -minutes;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Whether `""` is a value the user can mean for this type, rather than "unset".
|
|
290
|
+
*
|
|
291
|
+
* A DENYLIST, to agree with `paramToGiven`'s `default` arm. As an allowlist it
|
|
292
|
+
* named the three types that keep `""` and treated everything else as unset,
|
|
293
|
+
* while the switch below passes any unrecognised type through untouched: a
|
|
294
|
+
* deliberate `""` on a type neither knew about was written to the URL and read
|
|
295
|
+
* back as unset. Naming the types that cannot hold an empty value keeps the two
|
|
296
|
+
* in step as new types arrive.
|
|
297
|
+
*/
|
|
298
|
+
function isEmptyStringMeaningful(type: string | undefined): boolean {
|
|
299
|
+
return !EMPTY_MEANS_UNSET.has(type ?? "");
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Types with no empty value, so `?X=` on one of them means "unset". */
|
|
303
|
+
const EMPTY_MEANS_UNSET = new Set([
|
|
304
|
+
"boolean",
|
|
305
|
+
"number",
|
|
306
|
+
"date",
|
|
307
|
+
"timestamp",
|
|
308
|
+
"timestamptz",
|
|
309
|
+
]);
|
|
310
|
+
|
|
311
|
+
/** Read a query-parameter string back as a value of the declared type. */
|
|
312
|
+
export function paramToGiven(
|
|
313
|
+
type: string | undefined,
|
|
314
|
+
raw: string,
|
|
315
|
+
): GivenValue {
|
|
316
|
+
// An empty parameter means UNSET for every type that has no empty value,
|
|
317
|
+
// so it is dropped here rather than carried as `""`. `?FLAG=` was reaching
|
|
318
|
+
// the request as `{"FLAG": ""}`, which no boolean, number or date can be,
|
|
319
|
+
// while the control showed the model default or an empty box: the screen
|
|
320
|
+
// and the request disagreed, and nothing on screen said so. `string` and
|
|
321
|
+
// `filter<...>` are deliberately excluded, because there the empty string is
|
|
322
|
+
// a value a user can mean, and typing it is how they ask for it.
|
|
323
|
+
if (raw === "" && !isEmptyStringMeaningful(type)) return null;
|
|
324
|
+
|
|
325
|
+
// A filter's value IS a string of filter syntax, whatever it filters on, so
|
|
326
|
+
// it never gets coerced to the inner type.
|
|
327
|
+
if (isFilterType(type)) return raw;
|
|
328
|
+
|
|
329
|
+
switch (type) {
|
|
330
|
+
case "number": {
|
|
331
|
+
// `Number("")` is 0, not NaN, so an empty parameter would arrive as a
|
|
332
|
+
// deliberate zero and filter accordingly. Treated as unparseable
|
|
333
|
+
// instead, which is what the pass-through below is for.
|
|
334
|
+
const parsed = raw.trim() === "" ? NaN : Number(raw);
|
|
335
|
+
// A URL is user-editable, so a value that is not a number is passed
|
|
336
|
+
// through rather than turned into NaN; the server rejects it with a
|
|
337
|
+
// message naming the given, which beats a silently empty control.
|
|
338
|
+
return Number.isFinite(parsed) ? parsed : raw;
|
|
339
|
+
}
|
|
340
|
+
case "boolean":
|
|
341
|
+
// Passed through when it is neither, the way the number and date cases
|
|
342
|
+
// deliberately do: a URL is hand-editable, and coercing `?ACTIVE=yes`
|
|
343
|
+
// to `false` sends the OPPOSITE of what was typed with no complaint.
|
|
344
|
+
// The raw text gets an error naming the given instead.
|
|
345
|
+
if (raw === "true") return true;
|
|
346
|
+
if (raw === "false") return false;
|
|
347
|
+
return raw;
|
|
348
|
+
case "date":
|
|
349
|
+
case "timestamp":
|
|
350
|
+
case "timestamptz": {
|
|
351
|
+
// A URL is hand-editable, so a value that is not a date is passed
|
|
352
|
+
// through rather than becoming an Invalid Date; the server rejects it
|
|
353
|
+
// with a message naming the given.
|
|
354
|
+
//
|
|
355
|
+
// A `date` refuses a zone that would MOVE THE DAY, which is why the
|
|
356
|
+
// type reaches here at all. A date is a calendar day, so resolving
|
|
357
|
+
// `2024-01-05T00:30:00+05:30` lands on the 4th, and because the value
|
|
358
|
+
// is reported back outward the reader's address bar was then rewritten
|
|
359
|
+
// to a day they never wrote. On a value that CARRIES A TIME, a zone
|
|
360
|
+
// leaving the day alone reads normally, `Z` and `+00:00` included:
|
|
361
|
+
// refusing every zone was tried and was far too broad, since it
|
|
362
|
+
// rejected `Date#toISOString()` output. A zone on a BARE date is a
|
|
363
|
+
// separate rule and is always refused, for every temporal type, since
|
|
364
|
+
// an offset has no time of day to qualify. Both comparisons live in
|
|
365
|
+
// `parseTemporal`.
|
|
366
|
+
// The RESTORED text on refusal, not `raw`: otherwise a value the URL
|
|
367
|
+
// mangled is reported straight back out in its mangled form, so the
|
|
368
|
+
// reader's address bar loses the `+` they typed.
|
|
369
|
+
const text = restoreUrlPlus(raw);
|
|
370
|
+
return parseTemporal(text, type === "date") ?? text;
|
|
371
|
+
}
|
|
372
|
+
default:
|
|
373
|
+
// No `array<…>` case. `malloyGivenToApi` renders a non-filter given
|
|
374
|
+
// as the bare `type.type`, so `"array<…>"` is a spelling the server
|
|
375
|
+
// cannot produce, and Malloy emits only scalar parameter types for
|
|
376
|
+
// givens anyway. The branch that used to be here split on `,` with no
|
|
377
|
+
// escaping, so it was also the one place a value could be corrupted
|
|
378
|
+
// on the URL round trip.
|
|
379
|
+
return raw;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* The whole control row as query parameters, dropping unset givens so a
|
|
385
|
+
* default-state dashboard has a clean URL.
|
|
386
|
+
*/
|
|
387
|
+
export function givensToParams(
|
|
388
|
+
values: ReadonlyMap<string, GivenValue>,
|
|
389
|
+
declaredTypes: ReadonlyMap<string, string | undefined>,
|
|
390
|
+
): Record<string, string> {
|
|
391
|
+
const params: Record<string, string> = {};
|
|
392
|
+
for (const [name, value] of values) {
|
|
393
|
+
const encoded = givenToParam(value, declaredTypes.get(name));
|
|
394
|
+
if (encoded !== undefined) params[name] = encoded;
|
|
395
|
+
}
|
|
396
|
+
return params;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* A `Date` in the spelling the server accepts for the given's declared type.
|
|
401
|
+
*
|
|
402
|
+
* The three time types take three different forms, and each rejects the other
|
|
403
|
+
* two, so this cannot be one blanket `toISOString()`:
|
|
404
|
+
*
|
|
405
|
+
* - `date` wants a bare `YYYY-MM-DD`, and rejects anything longer.
|
|
406
|
+
* - `timestamp` is naive: an offset or a trailing `Z` is rejected outright,
|
|
407
|
+
* with a message pointing at `timestamptz`.
|
|
408
|
+
* - `timestamptz` is the instant, and is the only one that takes the full ISO
|
|
409
|
+
* string.
|
|
410
|
+
*
|
|
411
|
+
* Everything is read in UTC, matching the controls, which pick dates through
|
|
412
|
+
* dayjs's UTC plugin.
|
|
413
|
+
*/
|
|
414
|
+
function dateToRequest(value: Date, type: string | undefined): string {
|
|
415
|
+
const iso = value.toISOString();
|
|
416
|
+
switch (type) {
|
|
417
|
+
case "timestamptz":
|
|
418
|
+
return iso;
|
|
419
|
+
case "timestamp":
|
|
420
|
+
// Same instant with the zone marker dropped, which is what "naive"
|
|
421
|
+
// means here.
|
|
422
|
+
return iso.slice(0, -1);
|
|
423
|
+
default:
|
|
424
|
+
return iso.slice(0, 10);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Control values as the `givens` map a query request takes.
|
|
430
|
+
*
|
|
431
|
+
* Unlike the URL form this keeps types: a `number` given goes over as a
|
|
432
|
+
* number, because the server type-checks each value against its declaration.
|
|
433
|
+
* Only `Date` needs converting, since JSON has no date, and converting it
|
|
434
|
+
* needs the declared type (see {@link dateToRequest}).
|
|
435
|
+
*
|
|
436
|
+
* `only` narrows the map to a named subset, which is how a composite tile is
|
|
437
|
+
* run with just the givens it references instead of the whole control row.
|
|
438
|
+
*/
|
|
439
|
+
export function givensToRequest(
|
|
440
|
+
values: ReadonlyMap<string, GivenValue>,
|
|
441
|
+
declaredTypes: ReadonlyMap<string, string | undefined>,
|
|
442
|
+
only?: readonly string[],
|
|
443
|
+
): Record<string, unknown> {
|
|
444
|
+
const names = only ? new Set(only) : undefined;
|
|
445
|
+
const request: Record<string, unknown> = {};
|
|
446
|
+
for (const [name, value] of values) {
|
|
447
|
+
if (names && !names.has(name)) continue;
|
|
448
|
+
if (value === null || value === undefined) continue;
|
|
449
|
+
// Arrays are refused HERE too, not just in `givenToParam`. `GivenValue`
|
|
450
|
+
// no longer admits one, but the two functions are the URL side and the
|
|
451
|
+
// REQUEST side of the same rule, and fixing only the URL side left an
|
|
452
|
+
// array from a JavaScript caller filtering every query while being
|
|
453
|
+
// invisible in the address bar: the two disagreed about the same value,
|
|
454
|
+
// which is the drift this pair exists to avoid.
|
|
455
|
+
if (Array.isArray(value)) continue;
|
|
456
|
+
request[name] =
|
|
457
|
+
value instanceof Date
|
|
458
|
+
? dateToRequest(value, declaredTypes.get(name))
|
|
459
|
+
: value;
|
|
460
|
+
}
|
|
461
|
+
return request;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Read query parameters into control values, keeping only the names the
|
|
466
|
+
* dashboard actually declares.
|
|
467
|
+
*
|
|
468
|
+
* A URL can name anything; binding a given the model does not surface fails the
|
|
469
|
+
* query outright, so an unrecognized parameter is ignored rather than passed
|
|
470
|
+
* along. That also means an unrelated query parameter on the page's URL (a
|
|
471
|
+
* tracking tag, say) cannot break the dashboard.
|
|
472
|
+
*/
|
|
473
|
+
export function paramsToGivens(
|
|
474
|
+
params: Record<string, string>,
|
|
475
|
+
declaredTypes: ReadonlyMap<string, string | undefined>,
|
|
476
|
+
): Map<string, GivenValue> {
|
|
477
|
+
const values = new Map<string, GivenValue>();
|
|
478
|
+
for (const [name, raw] of Object.entries(params)) {
|
|
479
|
+
if (!declaredTypes.has(name)) continue;
|
|
480
|
+
values.set(name, paramToGiven(declaredTypes.get(name), raw));
|
|
481
|
+
}
|
|
482
|
+
return values;
|
|
483
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import { renderGivenDefault } from "./utils";
|
|
6
|
+
|
|
7
|
+
describe("renderGivenDefault", () => {
|
|
8
|
+
it("unquotes a single-quoted string literal", () => {
|
|
9
|
+
expect(renderGivenDefault("string", "'WN'")).toBe("WN");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("unquotes a double-quoted string literal", () => {
|
|
13
|
+
// Double-quoted strings are valid Malloy (DQ_STRING); the server forwards
|
|
14
|
+
// the raw source literal, so the UI must strip either quote form.
|
|
15
|
+
expect(renderGivenDefault("string", '"WN"')).toBe("WN");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("decodes JSON-style backslash escapes (Malloy's form, not doubled quotes)", () => {
|
|
19
|
+
expect(renderGivenDefault("string", "'O\\'Hare'")).toBe("O'Hare");
|
|
20
|
+
expect(renderGivenDefault("string", '"say \\"hi\\""')).toBe('say "hi"');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("drops the @ sigil from a date literal", () => {
|
|
24
|
+
expect(renderGivenDefault("date", "@2024-01-01")).toBe("2024-01-01");
|
|
25
|
+
expect(renderGivenDefault("timestamp", "@2024-01-01 12:00:00")).toBe(
|
|
26
|
+
"2024-01-01 12:00:00",
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("unwraps a single-value filter literal but leaves a compound one verbatim", () => {
|
|
31
|
+
expect(renderGivenDefault("filter<string>", "f'WN'")).toBe("WN");
|
|
32
|
+
// A compound filter expression is not a plain string — keep it intact
|
|
33
|
+
// instead of mangling the outer quotes.
|
|
34
|
+
expect(renderGivenDefault("filter<string>", "f'WN','AA'")).toBe(
|
|
35
|
+
"'WN','AA'",
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("shows numbers and booleans verbatim", () => {
|
|
40
|
+
expect(renderGivenDefault("number", "2003")).toBe("2003");
|
|
41
|
+
expect(renderGivenDefault("boolean", "true")).toBe("true");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("returns undefined when there is no default", () => {
|
|
45
|
+
expect(renderGivenDefault("string", undefined)).toBeUndefined();
|
|
46
|
+
expect(renderGivenDefault("string", "")).toBeUndefined();
|
|
47
|
+
expect(renderGivenDefault("string", " ")).toBeUndefined();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("distinguishes an explicit empty-string default from no default", () => {
|
|
51
|
+
// `given: x :: string is ''` -> literal "''" -> "" (present, not undefined),
|
|
52
|
+
// so the caller can show it as "(empty)" rather than suppress it.
|
|
53
|
+
expect(renderGivenDefault("string", "''")).toBe("");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("leaves a non-quoted value untouched", () => {
|
|
57
|
+
// Defensive: a malformed/already-rendered default isn't mangled.
|
|
58
|
+
expect(renderGivenDefault("string", "WN")).toBe("WN");
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Render a given's `default` (a raw Malloy source literal) for display in the
|
|
6
|
+
* input as placeholder / helper text. Returns undefined when there is no
|
|
7
|
+
* default to show.
|
|
8
|
+
*
|
|
9
|
+
* The server surfaces `Given.default` exactly as written in the model — one
|
|
10
|
+
* literal per type, e.g. `'WN'` / `"WN"` (string), `2003` (number), `true`
|
|
11
|
+
* (boolean), `@2024-01-01` (date), `f'WN'` (filter). For display we unquote
|
|
12
|
+
* string literals, drop the date `@` sigil, and unwrap single-value filter
|
|
13
|
+
* literals so the user sees `WN` / `2024-01-01` rather than the raw source form.
|
|
14
|
+
* Non-string scalars (number, boolean) and anything we don't special-case are
|
|
15
|
+
* shown verbatim.
|
|
16
|
+
*
|
|
17
|
+
* This only affects what the user *sees* — the value stays empty, so leaving
|
|
18
|
+
* the field blank still means "use the model default" (the server fills it in).
|
|
19
|
+
*/
|
|
20
|
+
export function renderGivenDefault(
|
|
21
|
+
type: string | undefined,
|
|
22
|
+
rawDefault: string | undefined,
|
|
23
|
+
): string | undefined {
|
|
24
|
+
if (!rawDefault) return undefined;
|
|
25
|
+
const literal = rawDefault.trim();
|
|
26
|
+
if (!literal) return undefined;
|
|
27
|
+
|
|
28
|
+
const givenType = type ?? "string";
|
|
29
|
+
|
|
30
|
+
if (
|
|
31
|
+
givenType === "date" ||
|
|
32
|
+
givenType === "timestamp" ||
|
|
33
|
+
givenType === "timestamptz"
|
|
34
|
+
) {
|
|
35
|
+
// `@2024-01-01` -> `2024-01-01`
|
|
36
|
+
return literal.replace(/^@/, "");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (givenType.startsWith("filter<")) {
|
|
40
|
+
// Drop the filter-literal `f` prefix. A filter value is an expression, not
|
|
41
|
+
// a plain string, so only unquote when it's a single simple quoted literal
|
|
42
|
+
// (`f'WN'` -> `WN`); leave a compound expression (`f'WN','AA'`) verbatim
|
|
43
|
+
// rather than mangling it by stripping the outermost quotes.
|
|
44
|
+
const expr = literal.replace(/^f/, "");
|
|
45
|
+
const single = expr.match(/^'([^'\\]*)'$/) ?? expr.match(/^"([^"\\]*)"$/);
|
|
46
|
+
return single ? single[1] : expr;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (givenType === "string") {
|
|
50
|
+
return unquoteStringLiteral(literal);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// number, boolean, array, or unknown: show the literal as authored.
|
|
54
|
+
return literal;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Strip the surrounding quotes from a Malloy string literal — single (`'WN'`)
|
|
59
|
+
* or double (`"WN"`), both valid Malloy string forms — and decode its escapes.
|
|
60
|
+
* Malloy strings use JSON-style backslash escapes (`\'`, `\"`, `\\`, `\n`),
|
|
61
|
+
* not SQL-style doubled quotes (see ParseUtil.parseString in @malloydata).
|
|
62
|
+
* Leaves a non-quoted input untouched.
|
|
63
|
+
*/
|
|
64
|
+
function unquoteStringLiteral(literal: string): string {
|
|
65
|
+
const quote = literal[0];
|
|
66
|
+
if (
|
|
67
|
+
literal.length >= 2 &&
|
|
68
|
+
(quote === "'" || quote === '"') &&
|
|
69
|
+
literal[literal.length - 1] === quote
|
|
70
|
+
) {
|
|
71
|
+
return literal.slice(1, -1).replace(/\\(["'\\ntr])/g, (_match, c) => {
|
|
72
|
+
if (c === "n") return "\n";
|
|
73
|
+
if (c === "t") return "\t";
|
|
74
|
+
if (c === "r") return "\r";
|
|
75
|
+
return c; // \" \' \\ -> the bare character
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return literal;
|
|
79
|
+
}
|