@malloy-publisher/sdk 0.12.0 → 0.12.2
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 +79 -6
- package/dist/{RenderedResult-SgDlxzs9.cjs.js → RenderedResult-Cgw4iqev.cjs.js} +1 -1
- package/dist/{RenderedResult-D8bCgThO.es.js → RenderedResult-DEjFM1DL.es.js} +2 -2
- package/dist/ServerProvider-BRQl_cBo.cjs.js +1 -0
- package/dist/{ServerProvider-DBw-thY0.es.js → ServerProvider-RnliA2Ao.es.js} +1084 -723
- package/dist/builder/index.cjs.js +1 -1
- package/dist/builder/index.es.js +3 -3
- package/dist/client/api.d.ts +754 -41
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +120 -110
- package/dist/components/Materializations/MaterializationDetailDialog.d.ts +16 -1
- package/dist/components/Model/ExplorerPage.d.ts +9 -0
- package/dist/components/Model/Model.d.ts +27 -11
- package/dist/components/ModelViewer/AccessChips.d.ts +34 -0
- package/dist/components/ModelViewer/EmptyRow.d.ts +23 -0
- package/dist/components/ModelViewer/Facts.d.ts +15 -0
- package/dist/components/ModelViewer/FieldGroup.d.ts +31 -0
- package/dist/components/ModelViewer/FieldRow.d.ts +69 -0
- package/dist/components/ModelViewer/GivenFacts.d.ts +21 -0
- package/dist/components/ModelViewer/HeaderBar.d.ts +75 -0
- package/dist/components/ModelViewer/InlineDoc.d.ts +13 -0
- package/dist/components/ModelViewer/ModelGivensSection.d.ts +32 -0
- package/dist/components/ModelViewer/ModelSection.d.ts +21 -0
- package/dist/components/ModelViewer/ModelViewer.d.ts +91 -0
- package/dist/components/ModelViewer/QueriesGroup.d.ts +15 -0
- package/dist/components/ModelViewer/RowGlyph.d.ts +27 -0
- package/dist/components/ModelViewer/RunResult.d.ts +46 -0
- package/dist/components/ModelViewer/SampleValues.d.ts +20 -0
- package/dist/components/ModelViewer/SchemaJoin.d.ts +51 -0
- package/dist/components/ModelViewer/SchemaView.d.ts +52 -0
- package/dist/components/ModelViewer/SegmentIcons.d.ts +70 -0
- package/dist/components/ModelViewer/SourcesDropdown.d.ts +53 -0
- package/dist/components/ModelViewer/SourcesNav.d.ts +25 -0
- package/dist/components/ModelViewer/StatusPill.d.ts +56 -0
- package/dist/components/ModelViewer/ValueChip.d.ts +17 -0
- package/dist/components/ModelViewer/ViewRow.d.ts +36 -0
- package/dist/components/ModelViewer/clickableBox.d.ts +29 -0
- package/dist/components/ModelViewer/codeDisplay.d.ts +44 -0
- package/dist/components/ModelViewer/docAnnotations.d.ts +90 -0
- package/dist/components/ModelViewer/explorerModel.d.ts +12 -0
- package/dist/components/ModelViewer/extensions.d.ts +94 -0
- package/dist/components/ModelViewer/fieldCode.d.ts +74 -0
- package/dist/components/ModelViewer/fieldEntities.d.ts +45 -0
- package/dist/components/ModelViewer/fieldExpansion.d.ts +41 -0
- package/dist/components/ModelViewer/givenUsage.d.ts +71 -0
- package/dist/components/ModelViewer/headerCard.d.ts +31 -0
- package/dist/components/ModelViewer/httpStatus.d.ts +4 -0
- package/dist/components/ModelViewer/icons.d.ts +31 -0
- package/dist/components/ModelViewer/index.d.ts +9 -0
- package/dist/components/ModelViewer/joinAccess.d.ts +52 -0
- package/dist/components/ModelViewer/malloyRunText.d.ts +22 -0
- package/dist/components/ModelViewer/malloyText.d.ts +21 -0
- package/dist/components/ModelViewer/modelGivens.d.ts +61 -0
- package/dist/components/ModelViewer/modelGivensContext.d.ts +9 -0
- package/dist/components/ModelViewer/modelHeaderPresentation.d.ts +28 -0
- package/dist/components/ModelViewer/modelTextDeclarations.d.ts +32 -0
- package/dist/components/ModelViewer/modelTextParse.d.ts +27 -0
- package/dist/components/ModelViewer/namedQueryCode.d.ts +12 -0
- package/dist/components/ModelViewer/offSurface.d.ts +54 -0
- package/dist/components/ModelViewer/rowLayers.d.ts +30 -0
- package/dist/components/ModelViewer/runGate.d.ts +49 -0
- package/dist/components/ModelViewer/schemaModel.d.ts +109 -0
- package/dist/components/ModelViewer/sectionOpenCommand.d.ts +45 -0
- package/dist/components/ModelViewer/sourceAccess.d.ts +67 -0
- package/dist/components/ModelViewer/sourceLineage.d.ts +14 -0
- package/dist/components/ModelViewer/sourcePicker.d.ts +66 -0
- package/dist/components/ModelViewer/surface.d.ts +47 -0
- package/dist/components/ModelViewer/text.d.ts +6 -0
- package/dist/components/ModelViewer/theme.d.ts +63 -0
- package/dist/components/ModelViewer/tokens.d.ts +52 -0
- package/dist/components/ModelViewer/ui.d.ts +48 -0
- package/dist/components/ModelViewer/useDimensionSamples.d.ts +76 -0
- package/dist/components/ModelViewer/useModelSources.d.ts +92 -0
- package/dist/components/ModelViewer/useRunQuery.d.ts +115 -0
- package/dist/components/ReportBuilder/loadMalloy.d.ts +4 -0
- package/dist/components/ServerProvider.d.ts +17 -2
- package/dist/components/filter/utils.d.ts +1 -1
- package/dist/components/given/GivenInput.d.ts +7 -1
- package/dist/components/given/GivensPanel.d.ts +3 -1
- package/dist/components/given/filterSummary.d.ts +6 -0
- package/dist/components/given/useGivenUrlParams.d.ts +11 -0
- package/dist/components/index.d.ts +2 -1
- package/dist/components/modelPage.d.ts +36 -0
- package/dist/hooks/useDimensionalFilterRangeData.d.ts +9 -0
- package/dist/hooks/useSuggestOptions.d.ts +26 -2
- package/dist/index.cjs.js +47 -19
- package/dist/index.es.js +14137 -9697
- package/dist/useCompiledDocument-D88_2cGS.cjs.js +143 -0
- package/dist/{useCompiledDocument-CXMPtJkJ.es.js → useCompiledDocument-Dmm6xkom.es.js} +6975 -6893
- package/dist/utils/filterCondition.d.ts +14 -0
- package/dist/utils/malloyLiteral.d.ts +20 -1
- package/package.json +2 -2
- package/src/components/Materializations/MaterializationDetailDialog.tsx +33 -7
- package/src/components/Materializations/Materializations.tsx +47 -4
- package/src/components/Materializations/Materializations.versions.spec.tsx +140 -1
- package/src/components/Model/ExplorerPage.tsx +269 -0
- package/src/components/Model/Model.tsx +66 -250
- package/src/components/Model/ModelExplorer.spec.tsx +64 -1
- package/src/components/ModelViewer/AccessChips.tsx +149 -0
- package/src/components/ModelViewer/EmptyRow.tsx +71 -0
- package/src/components/ModelViewer/Facts.tsx +46 -0
- package/src/components/ModelViewer/FieldGroup.tsx +137 -0
- package/src/components/ModelViewer/FieldRow.tsx +566 -0
- package/src/components/ModelViewer/GivenFacts.tsx +89 -0
- package/src/components/ModelViewer/HeaderBar.tsx +362 -0
- package/src/components/ModelViewer/InlineDoc.tsx +50 -0
- package/src/components/ModelViewer/ModelGivensSection.tsx +142 -0
- package/src/components/ModelViewer/ModelSection.tsx +98 -0
- package/src/components/ModelViewer/ModelViewer.spec.tsx +937 -0
- package/src/components/ModelViewer/ModelViewer.tsx +777 -0
- package/src/components/ModelViewer/QueriesGroup.tsx +107 -0
- package/src/components/ModelViewer/RowGlyph.tsx +138 -0
- package/src/components/ModelViewer/RunResult.tsx +298 -0
- package/src/components/ModelViewer/SampleValues.tsx +97 -0
- package/src/components/ModelViewer/SchemaJoin.tsx +258 -0
- package/src/components/ModelViewer/SchemaView.tsx +222 -0
- package/src/components/ModelViewer/SegmentIcons.tsx +129 -0
- package/src/components/ModelViewer/SourcesDropdown.tsx +265 -0
- package/src/components/ModelViewer/SourcesNav.tsx +75 -0
- package/src/components/ModelViewer/StatusPill.tsx +145 -0
- package/src/components/ModelViewer/ValueChip.tsx +39 -0
- package/src/components/ModelViewer/ViewRow.tsx +274 -0
- package/src/components/ModelViewer/clickableBox.ts +59 -0
- package/src/components/ModelViewer/codeDisplay.spec.ts +176 -0
- package/src/components/ModelViewer/codeDisplay.ts +118 -0
- package/src/components/ModelViewer/docAnnotations.spec.ts +181 -0
- package/src/components/ModelViewer/docAnnotations.ts +244 -0
- package/src/components/ModelViewer/docAnnotations.typeDetail.spec.ts +114 -0
- package/src/components/ModelViewer/explorerModel.spec.ts +43 -0
- package/src/components/ModelViewer/explorerModel.ts +31 -0
- package/src/components/ModelViewer/extensions.tsx +112 -0
- package/src/components/ModelViewer/fieldCode.spec.ts +168 -0
- package/src/components/ModelViewer/fieldCode.ts +118 -0
- package/src/components/ModelViewer/fieldEntities.ts +63 -0
- package/src/components/ModelViewer/fieldExpansion.spec.ts +116 -0
- package/src/components/ModelViewer/fieldExpansion.ts +95 -0
- package/src/components/ModelViewer/givenUsage.spec.ts +304 -0
- package/src/components/ModelViewer/givenUsage.ts +325 -0
- package/src/components/ModelViewer/headerCard.tsx +102 -0
- package/src/components/ModelViewer/httpStatus.ts +15 -0
- package/src/components/ModelViewer/icons.tsx +67 -0
- package/src/components/ModelViewer/index.ts +26 -0
- package/src/components/ModelViewer/joinAccess.spec.ts +95 -0
- package/src/components/ModelViewer/joinAccess.ts +54 -0
- package/src/components/ModelViewer/malloyRunText.spec.ts +48 -0
- package/src/components/ModelViewer/malloyRunText.ts +51 -0
- package/src/components/ModelViewer/malloyText.spec.ts +47 -0
- package/src/components/ModelViewer/malloyText.ts +77 -0
- package/src/components/ModelViewer/modelGivens.ts +228 -0
- package/src/components/ModelViewer/modelGivensContext.ts +13 -0
- package/src/components/ModelViewer/modelHeaderPresentation.spec.ts +31 -0
- package/src/components/ModelViewer/modelHeaderPresentation.ts +41 -0
- package/src/components/ModelViewer/modelTextDeclarations.spec.ts +336 -0
- package/src/components/ModelViewer/modelTextDeclarations.ts +228 -0
- package/src/components/ModelViewer/modelTextParse.ts +112 -0
- package/src/components/ModelViewer/namedQueryCode.spec.ts +58 -0
- package/src/components/ModelViewer/namedQueryCode.ts +26 -0
- package/src/components/ModelViewer/offSurface.tsx +124 -0
- package/src/components/ModelViewer/rowLayers.tsx +104 -0
- package/src/components/ModelViewer/runGate.spec.ts +219 -0
- package/src/components/ModelViewer/runGate.ts +145 -0
- package/src/components/ModelViewer/schemaModel.defaultSource.spec.ts +126 -0
- package/src/components/ModelViewer/schemaModel.queries.spec.ts +37 -0
- package/src/components/ModelViewer/schemaModel.ts +300 -0
- package/src/components/ModelViewer/sectionOpenCommand.tsx +125 -0
- package/src/components/ModelViewer/sourceAccess.spec.ts +130 -0
- package/src/components/ModelViewer/sourceAccess.ts +194 -0
- package/src/components/ModelViewer/sourceLineage.spec.ts +73 -0
- package/src/components/ModelViewer/sourceLineage.ts +38 -0
- package/src/components/ModelViewer/sourcePicker.spec.ts +294 -0
- package/src/components/ModelViewer/sourcePicker.ts +162 -0
- package/src/components/ModelViewer/surface.ts +51 -0
- package/src/components/ModelViewer/text.tsx +33 -0
- package/src/components/ModelViewer/theme.spec.ts +38 -0
- package/src/components/ModelViewer/theme.ts +58 -0
- package/src/components/ModelViewer/tokens.ts +87 -0
- package/src/components/ModelViewer/ui.tsx +201 -0
- package/src/components/ModelViewer/useDimensionSamples.spec.tsx +139 -0
- package/src/components/ModelViewer/useDimensionSamples.ts +220 -0
- package/src/components/ModelViewer/useModelSources.spec.ts +110 -0
- package/src/components/ModelViewer/useModelSources.ts +238 -0
- package/src/components/ModelViewer/useRunQuery.spec.ts +50 -0
- package/src/components/ModelViewer/useRunQuery.ts +223 -0
- package/src/components/Package/Package.agents.spec.tsx +5 -4
- package/src/components/Package/Package.tsx +8 -1
- package/src/components/ReportBuilder/emptyStart.spec.ts +8 -0
- package/src/components/ReportBuilder/loadMalloy.ts +8 -1
- package/src/components/ServerProvider.tsx +29 -3
- package/src/components/filter/utils.ts +4 -60
- package/src/components/given/FilterButton.tsx +4 -1
- package/src/components/given/GivenInput.tsx +8 -0
- package/src/components/given/GivensPanel.tsx +6 -1
- package/src/components/given/filterSummary.spec.ts +31 -1
- package/src/components/given/filterSummary.ts +41 -19
- package/src/components/given/useGivenUrlParams.spec.tsx +32 -0
- package/src/components/given/useGivenUrlParams.ts +49 -14
- package/src/components/index.ts +6 -1
- package/src/components/legacy/malloynb/Notebook.spec.tsx +38 -4
- package/src/components/legacy/malloynb/NotebookCell.tsx +8 -2
- package/src/components/modelPage.spec.ts +79 -0
- package/src/components/modelPage.ts +48 -0
- package/src/hooks/useDimensionFilters.ts +3 -58
- package/src/hooks/useDimensionFiltersQuery.ts +3 -58
- package/src/hooks/useDimensionalFilterRangeData.spec.ts +57 -0
- package/src/hooks/useDimensionalFilterRangeData.ts +8 -63
- package/src/hooks/useSuggestOptions.dom.spec.ts +54 -1
- package/src/hooks/useSuggestOptions.spec.ts +71 -0
- package/src/hooks/useSuggestOptions.ts +102 -12
- package/src/utils/filterCondition.spec.ts +87 -0
- package/src/utils/filterCondition.ts +66 -0
- package/src/utils/malloyLiteral.spec.ts +53 -1
- package/src/utils/malloyLiteral.ts +47 -12
- package/dist/ServerProvider-BHfg1Ed6.cjs.js +0 -1
- package/dist/useCompiledDocument-D7X_K6YU.cjs.js +0 -142
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type * as Malloy from "@malloydata/malloy-interfaces";
|
|
5
|
+
import type { Given } from "../../client";
|
|
6
|
+
import type { GivenValue } from "../../hooks/givenValue";
|
|
7
|
+
import { givensToRequest } from "../given/paramCodec";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A given the model declares, as `CompiledModel.givens` carries it, with the
|
|
11
|
+
* model file that declared it so a row can say where it comes from.
|
|
12
|
+
*/
|
|
13
|
+
export type GivenInfo = Given & {
|
|
14
|
+
/** Always present once shape-checked (the wire type leaves both optional). */
|
|
15
|
+
name: string;
|
|
16
|
+
type: string;
|
|
17
|
+
/** Package-relative path of the model file that declared this given. */
|
|
18
|
+
modelFile: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** One given's value as a query request carries it. */
|
|
22
|
+
export type GivenRequestValue = string | number | boolean;
|
|
23
|
+
|
|
24
|
+
/** The `givens` map a run sends: keys sorted, so the same values always
|
|
25
|
+
* serialize (and cache-key, `givensKeyFor`) the same way. */
|
|
26
|
+
export type GivenRequest = Readonly<Record<string, GivenRequestValue>>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The givens of a compiled model, less the ones its host fills on every query
|
|
30
|
+
* (`hostGivenNames`): a model may declare those (a gate reads `$GROUPS` with no
|
|
31
|
+
* default, as a gate must), but they are never a reader's input, so a control
|
|
32
|
+
* for one would be ignored and a missing value is never missing.
|
|
33
|
+
*
|
|
34
|
+
* Shape-checked rather than trusted, so an older server's body that lacks a
|
|
35
|
+
* field reads as no givens rather than a throw.
|
|
36
|
+
*/
|
|
37
|
+
export function extractCompiledModelGivens(
|
|
38
|
+
modelFile: string,
|
|
39
|
+
body: unknown,
|
|
40
|
+
hostGivenNames: ReadonlySet<string> = new Set(),
|
|
41
|
+
): GivenInfo[] {
|
|
42
|
+
const arr = (body as { givens?: unknown })?.givens;
|
|
43
|
+
if (!Array.isArray(arr)) return [];
|
|
44
|
+
const out: GivenInfo[] = [];
|
|
45
|
+
for (const raw of arr) {
|
|
46
|
+
if (!raw || typeof raw !== "object") continue;
|
|
47
|
+
const r = raw as Record<string, unknown>;
|
|
48
|
+
if (typeof r.name !== "string" || !r.name) continue;
|
|
49
|
+
if (typeof r.type !== "string" || !r.type) continue;
|
|
50
|
+
if (hostGivenNames.has(r.name)) continue;
|
|
51
|
+
const annotations = Array.isArray(r.annotations)
|
|
52
|
+
? r.annotations.filter((a): a is string => typeof a === "string")
|
|
53
|
+
: undefined;
|
|
54
|
+
out.push({
|
|
55
|
+
name: r.name,
|
|
56
|
+
type: r.type,
|
|
57
|
+
modelFile,
|
|
58
|
+
...(annotations && { annotations }),
|
|
59
|
+
...(typeof r.default === "string" && { default: r.default }),
|
|
60
|
+
...(r.required === true && { required: true }),
|
|
61
|
+
...(typeof r.label === "string" && r.label && { label: r.label }),
|
|
62
|
+
...(typeof r.description === "string" &&
|
|
63
|
+
r.description && { description: r.description }),
|
|
64
|
+
...((r.control === "select" || r.control === "multiselect") && {
|
|
65
|
+
control: r.control,
|
|
66
|
+
}),
|
|
67
|
+
...(typeof r.rangeMin === "number" && { rangeMin: r.rangeMin }),
|
|
68
|
+
...(typeof r.rangeMax === "number" && { rangeMax: r.rangeMax }),
|
|
69
|
+
...(r.suggest &&
|
|
70
|
+
typeof r.suggest === "object" && {
|
|
71
|
+
suggest: r.suggest as Given["suggest"],
|
|
72
|
+
}),
|
|
73
|
+
} as GivenInfo);
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Declared type per given name, which decides how a `Date` is sent. */
|
|
79
|
+
export function declaredTypesOf(
|
|
80
|
+
givens: ReadonlyArray<GivenInfo>,
|
|
81
|
+
): Map<string, string> {
|
|
82
|
+
return new Map(givens.map((g) => [g.name, g.type]));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The control row as the `givens` a run sends: only givens the model declares (a
|
|
87
|
+
* value left over for a since-removed given must not reach the server as an
|
|
88
|
+
* undeclared parameter), unset ones dropped, keys sorted. A blank `string` given
|
|
89
|
+
* is unset too: a cleared text box means "use the default". `undefined` when
|
|
90
|
+
* nothing is set, so a run at the defaults sends no `givens` at all.
|
|
91
|
+
*/
|
|
92
|
+
export function givensRequest(
|
|
93
|
+
values: ReadonlyMap<string, GivenValue>,
|
|
94
|
+
declaredTypes: ReadonlyMap<string, string | undefined>,
|
|
95
|
+
): GivenRequest | undefined {
|
|
96
|
+
const declared = new Map(
|
|
97
|
+
Array.from(values).filter(
|
|
98
|
+
([name, value]) =>
|
|
99
|
+
declaredTypes.has(name) &&
|
|
100
|
+
!(value === "" && declaredTypes.get(name) === "string"),
|
|
101
|
+
),
|
|
102
|
+
);
|
|
103
|
+
const request = givensToRequest(declared, declaredTypes) as Record<
|
|
104
|
+
string,
|
|
105
|
+
GivenRequestValue
|
|
106
|
+
>;
|
|
107
|
+
const entries = Object.entries(request).sort(([a], [b]) =>
|
|
108
|
+
a < b ? -1 : a > b ? 1 : 0,
|
|
109
|
+
);
|
|
110
|
+
return entries.length > 0 ? Object.fromEntries(entries) : undefined;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The givens part of a Malloy query's cache key: '' when none are set. */
|
|
114
|
+
export function givensKeyFor(
|
|
115
|
+
givens: Readonly<Record<string, string | number | boolean>> | undefined,
|
|
116
|
+
): string {
|
|
117
|
+
return givens && Object.keys(givens).length > 0
|
|
118
|
+
? JSON.stringify(givens)
|
|
119
|
+
: "";
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Filter metadata parsed from a source's `#(filter)` annotation:
|
|
124
|
+
* `#(filter) [name=NAME] dimension=DIMENSION type=TYPE [implicit] [required]`,
|
|
125
|
+
* where a value may be double-quoted to hold spaces.
|
|
126
|
+
*/
|
|
127
|
+
export interface SourceFilter {
|
|
128
|
+
/** Display name / API parameter key; defaults to the dimension name. */
|
|
129
|
+
name: string;
|
|
130
|
+
/** Name of the dimension the filter targets. */
|
|
131
|
+
dimension: string;
|
|
132
|
+
type: "equal" | "in" | "like" | "greater_than" | "less_than";
|
|
133
|
+
/** If true, don't show in the UI. */
|
|
134
|
+
implicit?: boolean;
|
|
135
|
+
/** If true, the server requires a value. */
|
|
136
|
+
required?: boolean;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Parse the `#(filter) ...` annotations on a source. */
|
|
140
|
+
export function parseSourceFilterAnnotations(
|
|
141
|
+
annotations: Malloy.Annotation[] | undefined,
|
|
142
|
+
): SourceFilter[] {
|
|
143
|
+
if (!annotations) return [];
|
|
144
|
+
const filters: SourceFilter[] = [];
|
|
145
|
+
for (const annotation of annotations) {
|
|
146
|
+
const text = annotation.value?.trim();
|
|
147
|
+
if (!text || !text.startsWith("#(filter)")) continue;
|
|
148
|
+
const parsed = parseFilterBody(text.slice("#(filter)".length).trim());
|
|
149
|
+
if (parsed) filters.push(parsed);
|
|
150
|
+
}
|
|
151
|
+
return filters;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const FILTER_TYPES: ReadonlySet<string> = new Set([
|
|
155
|
+
"equal",
|
|
156
|
+
"in",
|
|
157
|
+
"like",
|
|
158
|
+
"greater_than",
|
|
159
|
+
"less_than",
|
|
160
|
+
]);
|
|
161
|
+
|
|
162
|
+
function parseFilterBody(body: string): SourceFilter | null {
|
|
163
|
+
let name: string | undefined;
|
|
164
|
+
let dimension: string | undefined;
|
|
165
|
+
let type: SourceFilter["type"] | undefined;
|
|
166
|
+
let implicit = false;
|
|
167
|
+
let required = false;
|
|
168
|
+
for (const token of tokenizeFilterBody(body)) {
|
|
169
|
+
if (token === "implicit") {
|
|
170
|
+
implicit = true;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (token === "required") {
|
|
174
|
+
required = true;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
const eq = token.indexOf("=");
|
|
178
|
+
if (eq <= 0) continue;
|
|
179
|
+
const key = token.slice(0, eq).trim();
|
|
180
|
+
let value = token.slice(eq + 1).trim();
|
|
181
|
+
if (value.length >= 2 && value.startsWith('"') && value.endsWith('"')) {
|
|
182
|
+
value = value.slice(1, -1);
|
|
183
|
+
}
|
|
184
|
+
if (key === "name") name = value;
|
|
185
|
+
else if (key === "dimension") dimension = value;
|
|
186
|
+
else if (key === "type" && FILTER_TYPES.has(value)) {
|
|
187
|
+
type = value as SourceFilter["type"];
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (!dimension || !type) return null;
|
|
191
|
+
return { name: name ?? dimension, dimension, type, implicit, required };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Splits on whitespace, keeping a double-quoted run (`key="a b"`) together. */
|
|
195
|
+
function tokenizeFilterBody(body: string): string[] {
|
|
196
|
+
const tokens: string[] = [];
|
|
197
|
+
let current = "";
|
|
198
|
+
let inQuotes = false;
|
|
199
|
+
for (const c of body) {
|
|
200
|
+
if (c === '"') {
|
|
201
|
+
inQuotes = !inQuotes;
|
|
202
|
+
current += c;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (!inQuotes && /\s/.test(c)) {
|
|
206
|
+
if (current) tokens.push(current);
|
|
207
|
+
current = "";
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
current += c;
|
|
211
|
+
}
|
|
212
|
+
if (current) tokens.push(current);
|
|
213
|
+
return tokens;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* The `#(index)` / `#(index_values)` tag, with or without parameters
|
|
218
|
+
* (`#(index freshness.window="24h")`). `\b` keeps `#(indexed)` from matching.
|
|
219
|
+
*/
|
|
220
|
+
const INDEX_ANNOTATION = /^#\(index(_values)?\b/;
|
|
221
|
+
|
|
222
|
+
/** True if the field carries an `#(index_values)` or `#(index)` annotation. */
|
|
223
|
+
export function isFieldIndexed(field: Malloy.FieldInfo): boolean {
|
|
224
|
+
if (!("annotations" in field) || !field.annotations) return false;
|
|
225
|
+
return field.annotations.some((annotation) =>
|
|
226
|
+
INDEX_ANNOTATION.test(annotation.value?.trim() ?? ""),
|
|
227
|
+
);
|
|
228
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { createContext } from "react";
|
|
5
|
+
import type { GivenInfo } from "./modelGivens";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The model's givens a reader can set (router-filled ones already left out), for
|
|
9
|
+
* the rows that read one: each marks itself "Given" and says which, from its own
|
|
10
|
+
* code (givensReadBy). Provided once by the explorer rather than threaded
|
|
11
|
+
* through every section and group.
|
|
12
|
+
*/
|
|
13
|
+
export const ModelGivensContext = createContext<ReadonlyArray<GivenInfo>>([]);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it, test } from "bun:test";
|
|
5
|
+
import { modelSourcesLine, sourceCountLabel } from "./modelHeaderPresentation";
|
|
6
|
+
|
|
7
|
+
describe("modelSourcesLine", () => {
|
|
8
|
+
test("singular for one source", () => {
|
|
9
|
+
expect(modelSourcesLine(1)).toBe("1 source in this model");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("plural otherwise", () => {
|
|
13
|
+
expect(modelSourcesLine(10)).toBe("10 sources in this model");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// A model that compiles but declares no sources: the header still has to say
|
|
17
|
+
// something, and "0 sources in this model" reads like a count that failed to
|
|
18
|
+
// load rather than a fact about the file.
|
|
19
|
+
test("names the empty case in words", () => {
|
|
20
|
+
expect(modelSourcesLine(0)).toBe("No sources in this model");
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe("sourceCountLabel", () => {
|
|
25
|
+
it("counts a model's sources, and the root index.malloy's as public", () => {
|
|
26
|
+
expect(sourceCountLabel(5)).toBe("5 sources");
|
|
27
|
+
expect(sourceCountLabel(1)).toBe("1 source");
|
|
28
|
+
expect(sourceCountLabel(4, true)).toBe("4 public sources");
|
|
29
|
+
expect(sourceCountLabel(0)).toBe("No sources");
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Copy for the model page's header, mirroring the connection page's
|
|
6
|
+
* (connectionStatusPresentation.ts): the status line under the title, and the
|
|
7
|
+
* (?) help text beside it.
|
|
8
|
+
*
|
|
9
|
+
* Separate from the components so it can be asserted directly, and so the
|
|
10
|
+
* explorer modules keep exporting only components — a non-component export
|
|
11
|
+
* breaks Vite Fast Refresh for the whole module.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* What this model holds, in the shape of the connection page's "Indexed 24
|
|
16
|
+
* tables": the count of the thing the page is a browser for.
|
|
17
|
+
*/
|
|
18
|
+
export function modelSourcesLine(sourceCount: number): string {
|
|
19
|
+
if (sourceCount === 0) return "No sources in this model";
|
|
20
|
+
return `${sourceCount} source${sourceCount === 1 ? "" : "s"} in this model`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* What a source is and what picking one does — the two things a first-time
|
|
25
|
+
* reader of the sources list cannot guess from a bare count. Kept free of any
|
|
26
|
+
* chat-scoping clause, because the sources dropdown
|
|
27
|
+
* this labels also appears on the standalone model page, where there is no chat
|
|
28
|
+
* to scope.
|
|
29
|
+
*/
|
|
30
|
+
export const MODEL_SOURCES_HELP =
|
|
31
|
+
"A source is a queryable entry point into the model — pick one to browse its dimensions, measures, views and joins.";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The source picker pill's face, beside the source's title on the model page: "5 sources", or on
|
|
35
|
+
* the package's root `index.malloy` — whose sources are the set the package publishes —
|
|
36
|
+
* "4 public sources".
|
|
37
|
+
*/
|
|
38
|
+
export function sourceCountLabel(sourceCount: number, curated = false): string {
|
|
39
|
+
if (sourceCount === 0) return "No sources";
|
|
40
|
+
return `${sourceCount} ${curated ? "public " : ""}source${sourceCount === 1 ? "" : "s"}`;
|
|
41
|
+
}
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import { viewDeclaration } from "./codeDisplay";
|
|
6
|
+
import * as malloy from "@malloydata/malloy";
|
|
7
|
+
import { fieldDeclarations as declarationsOf } from "./modelTextDeclarations";
|
|
8
|
+
import { parseModelText } from "./modelTextParse";
|
|
9
|
+
|
|
10
|
+
const fieldDeclarations = (text: string | undefined, sourceName: string) =>
|
|
11
|
+
declarationsOf(
|
|
12
|
+
text === undefined ? undefined : parseModelText(malloy, text),
|
|
13
|
+
sourceName,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
// The shapes of a real draft's model (bird_feederwatch's feederwatch.malloy): a
|
|
17
|
+
// source whose header wraps onto a second line, doc and chart tags above each
|
|
18
|
+
// view, joined fields in a group_by, a nested field's own `# percent`, a measure
|
|
19
|
+
// wrapped onto the next line, and a view built on another view.
|
|
20
|
+
const FEEDERWATCH = `##! experimental.givens
|
|
21
|
+
// Project FeederWatch semantic model.
|
|
22
|
+
|
|
23
|
+
#(doc) eBird taxonomy: one row per taxon.
|
|
24
|
+
source: taxonomy is bqtest.table('bird_feederwatch.ebird_taxonomy_2023')
|
|
25
|
+
extend {
|
|
26
|
+
primary_key: SPECIES_CODE
|
|
27
|
+
rename: taxon_category is CATEGORY
|
|
28
|
+
#(doc) English common name of the taxon.
|
|
29
|
+
dimension: common_name is COMMON_NAME
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
source: observations is
|
|
33
|
+
bqtest.table('bird_feederwatch.pfw_observations') extend {
|
|
34
|
+
join_one: species is taxonomy on SPECIES_CODE = species.SPECIES_CODE
|
|
35
|
+
join_one: site is sites
|
|
36
|
+
|
|
37
|
+
dimension: season_label is pick 'early' when month_num < 1
|
|
38
|
+
else 'late'
|
|
39
|
+
# number="#,##0"
|
|
40
|
+
measure: checklist_count is count(SUB_ID)
|
|
41
|
+
#(doc) Average number of full species reported per checklist.
|
|
42
|
+
# number="#,##0.0"
|
|
43
|
+
measure: species_per_checklist is
|
|
44
|
+
count() { where: species.is_true_species } / checklist_count
|
|
45
|
+
|
|
46
|
+
#(doc) Headline figures for whatever filter is applied.
|
|
47
|
+
view: key_figures is {
|
|
48
|
+
aggregate: checklist_count
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#(doc) Species per checklist by number of feeders at the site.
|
|
52
|
+
# bar_chart { y=species_per_checklist }
|
|
53
|
+
view: richness_by_feeders is {
|
|
54
|
+
group_by: site.feeder_count_band, site.feeder_band_order
|
|
55
|
+
aggregate: species_per_checklist, checklist_count, site_count
|
|
56
|
+
order_by: feeder_band_order
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#(doc) Every species' share of checklists per season.
|
|
60
|
+
view: species_share is {
|
|
61
|
+
where: season_end_year >= 1989
|
|
62
|
+
group_by: season, common_name
|
|
63
|
+
aggregate:
|
|
64
|
+
# percent
|
|
65
|
+
season_share is checklist_count / all(checklist_count, season)
|
|
66
|
+
checklist_count
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#(doc) Share of checklists per season for a few familiar feeder birds.
|
|
70
|
+
# line_chart { y=season_share series=common_name }
|
|
71
|
+
view: species_trend is species_share -> {
|
|
72
|
+
where: common_name = 'Dark-eyed Junco' | 'House Finch'
|
|
73
|
+
| 'Northern Cardinal'
|
|
74
|
+
select: season, common_name, season_share
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
`;
|
|
78
|
+
|
|
79
|
+
describe("fieldDeclarations", () => {
|
|
80
|
+
const decls = fieldDeclarations(FEEDERWATCH, "observations");
|
|
81
|
+
|
|
82
|
+
it("cuts a view with doc and chart tags above it, keeping the tags and its whole block", () => {
|
|
83
|
+
expect(decls.get("richness_by_feeders")).toEqual({
|
|
84
|
+
kind: "view",
|
|
85
|
+
code: [
|
|
86
|
+
"#(doc) Species per checklist by number of feeders at the site.",
|
|
87
|
+
"# bar_chart { y=species_per_checklist }",
|
|
88
|
+
"richness_by_feeders is {",
|
|
89
|
+
" group_by: site.feeder_count_band, site.feeder_band_order",
|
|
90
|
+
" aggregate: species_per_checklist, checklist_count, site_count",
|
|
91
|
+
" order_by: feeder_band_order",
|
|
92
|
+
" }",
|
|
93
|
+
].join("\n"),
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("reads as the view's declaration on its Code, tags kept and doc dropped", () => {
|
|
98
|
+
expect(
|
|
99
|
+
viewDeclaration(
|
|
100
|
+
"richness_by_feeders",
|
|
101
|
+
decls.get("richness_by_feeders")!.code,
|
|
102
|
+
),
|
|
103
|
+
).toBe(
|
|
104
|
+
[
|
|
105
|
+
"# bar_chart { y=species_per_checklist }",
|
|
106
|
+
"view: richness_by_feeders is {",
|
|
107
|
+
" group_by: site.feeder_count_band, site.feeder_band_order",
|
|
108
|
+
" aggregate: species_per_checklist, checklist_count, site_count",
|
|
109
|
+
" order_by: feeder_band_order",
|
|
110
|
+
"}",
|
|
111
|
+
].join("\n"),
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("finds every view in the source, of every shape", () => {
|
|
116
|
+
const views = [...decls]
|
|
117
|
+
.filter(([, d]) => d.kind === "view")
|
|
118
|
+
.map(([name]) => name);
|
|
119
|
+
expect(views).toEqual([
|
|
120
|
+
"key_figures",
|
|
121
|
+
"richness_by_feeders",
|
|
122
|
+
"species_share",
|
|
123
|
+
"species_trend",
|
|
124
|
+
]);
|
|
125
|
+
// A nested field's annotation is part of its view, not the start of a new statement.
|
|
126
|
+
expect(decls.get("species_share")?.code).toContain(
|
|
127
|
+
"# percent\n season_share is",
|
|
128
|
+
);
|
|
129
|
+
expect(
|
|
130
|
+
decls.get("species_share")?.code.endsWith("checklist_count\n }"),
|
|
131
|
+
).toBe(true);
|
|
132
|
+
// A view on a view: its pipeline and the where that wraps onto more lines.
|
|
133
|
+
expect(decls.get("species_trend")?.code).toBe(
|
|
134
|
+
[
|
|
135
|
+
"#(doc) Share of checklists per season for a few familiar feeder birds.",
|
|
136
|
+
"# line_chart { y=season_share series=common_name }",
|
|
137
|
+
"species_trend is species_share -> {",
|
|
138
|
+
" where: common_name = 'Dark-eyed Junco' | 'House Finch'",
|
|
139
|
+
" | 'Northern Cardinal'",
|
|
140
|
+
" select: season, common_name, season_share",
|
|
141
|
+
" }",
|
|
142
|
+
].join("\n"),
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("cuts measures and dimensions too, through an expression that wraps", () => {
|
|
147
|
+
expect(decls.get("species_per_checklist")).toEqual({
|
|
148
|
+
kind: "measure",
|
|
149
|
+
code: [
|
|
150
|
+
"#(doc) Average number of full species reported per checklist.",
|
|
151
|
+
'# number="#,##0.0"',
|
|
152
|
+
"species_per_checklist is",
|
|
153
|
+
" count() { where: species.is_true_species } / checklist_count",
|
|
154
|
+
].join("\n"),
|
|
155
|
+
});
|
|
156
|
+
expect(decls.get("checklist_count")?.code).toBe(
|
|
157
|
+
'# number="#,##0"\nchecklist_count is count(SUB_ID)',
|
|
158
|
+
);
|
|
159
|
+
expect(decls.get("season_label")).toEqual({
|
|
160
|
+
kind: "dimension",
|
|
161
|
+
code: "season_label is pick 'early' when month_num < 1\n else 'late'",
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("reads only the source asked for: another source's fields and its renames are not this one's", () => {
|
|
166
|
+
expect(decls.has("common_name")).toBe(false);
|
|
167
|
+
expect(decls.has("species")).toBe(false);
|
|
168
|
+
const taxonomy = fieldDeclarations(FEEDERWATCH, "taxonomy");
|
|
169
|
+
expect([...taxonomy.keys()]).toEqual(["common_name"]);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("reads keyword blocks, comma-separated entries, and the source a source extends", () => {
|
|
173
|
+
const text = `source: base is duckdb.table('t') extend {
|
|
174
|
+
measure:
|
|
175
|
+
# currency
|
|
176
|
+
revenue is sum(amount), order_count is count()
|
|
177
|
+
view:
|
|
178
|
+
by_day is { group_by: order_day aggregate: revenue }
|
|
179
|
+
# bar_chart
|
|
180
|
+
by_week is by_day + { limit: 5 }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
source: narrowed is base extend {
|
|
184
|
+
where: amount > 0
|
|
185
|
+
view: by_week is { group_by: order_week }
|
|
186
|
+
}
|
|
187
|
+
`;
|
|
188
|
+
const base = fieldDeclarations(text, "base");
|
|
189
|
+
expect(base.get("revenue")?.code).toBe(
|
|
190
|
+
"# currency\nrevenue is sum(amount)",
|
|
191
|
+
);
|
|
192
|
+
expect(base.get("order_count")?.code).toBe("order_count is count()");
|
|
193
|
+
expect(base.get("by_day")?.code).toBe(
|
|
194
|
+
"by_day is { group_by: order_day aggregate: revenue }",
|
|
195
|
+
);
|
|
196
|
+
expect(base.get("by_week")?.code).toBe(
|
|
197
|
+
"# bar_chart\nby_week is by_day + { limit: 5 }",
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
const narrowed = fieldDeclarations(text, "narrowed");
|
|
201
|
+
// Its own declaration wins over the one it inherits; the rest come from `base`.
|
|
202
|
+
expect(narrowed.get("by_week")?.code).toBe(
|
|
203
|
+
"by_week is { group_by: order_week }",
|
|
204
|
+
);
|
|
205
|
+
expect(narrowed.get("by_day")?.code).toBe(
|
|
206
|
+
"by_day is { group_by: order_day aggregate: revenue }",
|
|
207
|
+
);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("cuts from the name when the line has trailing whitespace, for every entry on it", () => {
|
|
211
|
+
const text =
|
|
212
|
+
"source: s is duckdb.table('t') extend {\n dimension: foo is bar \n measure: a is count(), b is sum(x) \n}\n";
|
|
213
|
+
const found = fieldDeclarations(text, "s");
|
|
214
|
+
expect(found.get("foo")?.code).toBe("foo is bar");
|
|
215
|
+
expect(found.get("a")?.code).toBe("a is count()");
|
|
216
|
+
expect(found.get("b")?.code).toBe("b is sum(x)");
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("passes a base's fields on through `extend` only, not through a query's `->`", () => {
|
|
220
|
+
const text = `source: a is duckdb.table('t') extend {
|
|
221
|
+
dimension: carrier is upper(code)
|
|
222
|
+
}
|
|
223
|
+
source: b is a -> { group_by: carrier }
|
|
224
|
+
source: c is a extend { }
|
|
225
|
+
`;
|
|
226
|
+
expect(fieldDeclarations(text, "b").get("carrier")).toBeUndefined();
|
|
227
|
+
expect(fieldDeclarations(text, "c").get("carrier")?.code).toBe(
|
|
228
|
+
"carrier is upper(code)",
|
|
229
|
+
);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("leaves out a name declared twice, and has nothing without the text or the source", () => {
|
|
233
|
+
const text = `source: s is duckdb.table('t') extend {
|
|
234
|
+
view: v is { group_by: a }
|
|
235
|
+
view: v is { group_by: b }
|
|
236
|
+
}
|
|
237
|
+
`;
|
|
238
|
+
expect(fieldDeclarations(text, "s").has("v")).toBe(false);
|
|
239
|
+
expect(fieldDeclarations(undefined, "s").size).toBe(0);
|
|
240
|
+
expect(fieldDeclarations(text, "missing").size).toBe(0);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("is not thrown by brackets in strings, comments and tags", () => {
|
|
244
|
+
const text = `source: s is duckdb.table('t') extend {
|
|
245
|
+
# bar_chart { y=x
|
|
246
|
+
view: v is {
|
|
247
|
+
where: label != "}" // a } in a comment
|
|
248
|
+
group_by: label
|
|
249
|
+
}
|
|
250
|
+
view: w is { group_by: label }
|
|
251
|
+
}
|
|
252
|
+
`;
|
|
253
|
+
const decls2 = fieldDeclarations(text, "s");
|
|
254
|
+
expect(decls2.get("v")?.code).toBe(
|
|
255
|
+
'# bar_chart { y=x\nv is {\n where: label != "}" // a } in a comment\n group_by: label\n }',
|
|
256
|
+
);
|
|
257
|
+
expect(decls2.get("w")?.code).toBe("w is { group_by: label }");
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("reads a one-line source, which also inherits every field its base declares", () => {
|
|
261
|
+
const text = `source: base is duckdb.table('t') extend {
|
|
262
|
+
# currency
|
|
263
|
+
dimension: carrier is upper(code)
|
|
264
|
+
measure: a is count()
|
|
265
|
+
}
|
|
266
|
+
#(doc) One line.
|
|
267
|
+
source: one is base extend { measure: c is count() }
|
|
268
|
+
`;
|
|
269
|
+
const one = fieldDeclarations(text, "one");
|
|
270
|
+
// Not the source's own doc line: the entry doesn't start its line.
|
|
271
|
+
expect(one.get("c")).toEqual({ kind: "measure", code: "c is count()" });
|
|
272
|
+
expect(one.get("carrier")).toEqual({
|
|
273
|
+
kind: "dimension",
|
|
274
|
+
code: "# currency\ncarrier is upper(code)",
|
|
275
|
+
});
|
|
276
|
+
expect(one.get("a")?.code).toBe("a is count()");
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("reads a field on the `extend {` line and the ones on the lines after it", () => {
|
|
280
|
+
const text = `source: s is duckdb.table('t') extend { dimension: d is upper(code)
|
|
281
|
+
measure: m is count()
|
|
282
|
+
view: v is { group_by: d }
|
|
283
|
+
}
|
|
284
|
+
`;
|
|
285
|
+
const found = fieldDeclarations(text, "s");
|
|
286
|
+
expect(found.get("d")).toEqual({
|
|
287
|
+
kind: "dimension",
|
|
288
|
+
code: "d is upper(code)",
|
|
289
|
+
});
|
|
290
|
+
expect(found.get("m")).toEqual({ kind: "measure", code: "m is count()" });
|
|
291
|
+
expect(found.get("v")).toEqual({
|
|
292
|
+
kind: "view",
|
|
293
|
+
code: "v is { group_by: d }",
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("takes the kind from a keyword with an access modifier, and leaves out a comment among the tags", () => {
|
|
298
|
+
const text = `source: s is duckdb.table('t') extend {
|
|
299
|
+
dimension:
|
|
300
|
+
d is upper(code)
|
|
301
|
+
public measure:
|
|
302
|
+
#(doc) Orders.
|
|
303
|
+
// a comment between the tags
|
|
304
|
+
# number="#,##0"
|
|
305
|
+
m is count()
|
|
306
|
+
n is sum(x)
|
|
307
|
+
}
|
|
308
|
+
`;
|
|
309
|
+
const found = fieldDeclarations(text, "s");
|
|
310
|
+
expect(found.get("d")?.kind).toBe("dimension");
|
|
311
|
+
expect(found.get("m")).toEqual({
|
|
312
|
+
kind: "measure",
|
|
313
|
+
code: '#(doc) Orders.\n# number="#,##0"\nm is count()',
|
|
314
|
+
});
|
|
315
|
+
expect(found.get("n")).toEqual({ kind: "measure", code: "n is sum(x)" });
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it("reads a file with CRLF line endings", () => {
|
|
319
|
+
const text =
|
|
320
|
+
"source: s is duckdb.table('t') extend {\r\n # currency\r\n measure: m is\r\n sum(x)\r\n}\r\n";
|
|
321
|
+
expect(fieldDeclarations(text, "s").get("m")?.code).toBe(
|
|
322
|
+
"# currency\nm is\n sum(x)",
|
|
323
|
+
);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("still reads the fields of a file mid-edit", () => {
|
|
327
|
+
const text = `source: s is duckdb.table('t') extend {
|
|
328
|
+
dimension: ok is upper(code)
|
|
329
|
+
measure: bad is count(
|
|
330
|
+
}
|
|
331
|
+
`;
|
|
332
|
+
expect(fieldDeclarations(text, "s").get("ok")?.code).toBe(
|
|
333
|
+
"ok is upper(code)",
|
|
334
|
+
);
|
|
335
|
+
});
|
|
336
|
+
});
|