@malloy-publisher/sdk 0.12.1 → 0.12.3

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.
Files changed (208) hide show
  1. package/README.md +79 -6
  2. package/dist/{RenderedResult-DrNhQ2_9.cjs.js → RenderedResult-C2arX5Zz.cjs.js} +1 -1
  3. package/dist/{RenderedResult-AyM-RQaA.es.js → RenderedResult-DqPvGZgL.es.js} +2 -2
  4. package/dist/ServerProvider-CqL6xzta.cjs.js +1 -0
  5. package/dist/{ServerProvider-a9Ki0QXO.es.js → ServerProvider-DRbZ6tLP.es.js} +1213 -1187
  6. package/dist/builder/index.cjs.js +1 -1
  7. package/dist/builder/index.es.js +3 -3
  8. package/dist/client/api.d.ts +43 -13
  9. package/dist/client/index.cjs.js +1 -1
  10. package/dist/client/index.es.js +28 -27
  11. package/dist/components/Model/ExplorerPage.d.ts +9 -0
  12. package/dist/components/Model/Model.d.ts +27 -11
  13. package/dist/components/ModelViewer/AccessChips.d.ts +34 -0
  14. package/dist/components/ModelViewer/EmptyRow.d.ts +23 -0
  15. package/dist/components/ModelViewer/Facts.d.ts +15 -0
  16. package/dist/components/ModelViewer/FieldGroup.d.ts +31 -0
  17. package/dist/components/ModelViewer/FieldRow.d.ts +69 -0
  18. package/dist/components/ModelViewer/GivenFacts.d.ts +21 -0
  19. package/dist/components/ModelViewer/HeaderBar.d.ts +75 -0
  20. package/dist/components/ModelViewer/InlineDoc.d.ts +13 -0
  21. package/dist/components/ModelViewer/ModelGivensSection.d.ts +32 -0
  22. package/dist/components/ModelViewer/ModelSection.d.ts +21 -0
  23. package/dist/components/ModelViewer/ModelViewer.d.ts +91 -0
  24. package/dist/components/ModelViewer/QueriesGroup.d.ts +15 -0
  25. package/dist/components/ModelViewer/RowGlyph.d.ts +27 -0
  26. package/dist/components/ModelViewer/RunResult.d.ts +46 -0
  27. package/dist/components/ModelViewer/SampleValues.d.ts +20 -0
  28. package/dist/components/ModelViewer/SchemaJoin.d.ts +51 -0
  29. package/dist/components/ModelViewer/SchemaView.d.ts +52 -0
  30. package/dist/components/ModelViewer/SegmentIcons.d.ts +70 -0
  31. package/dist/components/ModelViewer/SourcesDropdown.d.ts +53 -0
  32. package/dist/components/ModelViewer/SourcesNav.d.ts +25 -0
  33. package/dist/components/ModelViewer/StatusPill.d.ts +56 -0
  34. package/dist/components/ModelViewer/ValueChip.d.ts +17 -0
  35. package/dist/components/ModelViewer/ViewRow.d.ts +36 -0
  36. package/dist/components/ModelViewer/clickableBox.d.ts +29 -0
  37. package/dist/components/ModelViewer/codeDisplay.d.ts +44 -0
  38. package/dist/components/ModelViewer/docAnnotations.d.ts +90 -0
  39. package/dist/components/ModelViewer/explorerModel.d.ts +12 -0
  40. package/dist/components/ModelViewer/extensions.d.ts +94 -0
  41. package/dist/components/ModelViewer/fieldCode.d.ts +74 -0
  42. package/dist/components/ModelViewer/fieldEntities.d.ts +45 -0
  43. package/dist/components/ModelViewer/fieldExpansion.d.ts +41 -0
  44. package/dist/components/ModelViewer/givenUsage.d.ts +71 -0
  45. package/dist/components/ModelViewer/headerCard.d.ts +31 -0
  46. package/dist/components/ModelViewer/httpStatus.d.ts +4 -0
  47. package/dist/components/ModelViewer/icons.d.ts +31 -0
  48. package/dist/components/ModelViewer/index.d.ts +9 -0
  49. package/dist/components/ModelViewer/joinAccess.d.ts +52 -0
  50. package/dist/components/ModelViewer/malloyRunText.d.ts +22 -0
  51. package/dist/components/ModelViewer/malloyText.d.ts +21 -0
  52. package/dist/components/ModelViewer/modelGivens.d.ts +61 -0
  53. package/dist/components/ModelViewer/modelGivensContext.d.ts +9 -0
  54. package/dist/components/ModelViewer/modelHeaderPresentation.d.ts +28 -0
  55. package/dist/components/ModelViewer/modelTextDeclarations.d.ts +32 -0
  56. package/dist/components/ModelViewer/modelTextParse.d.ts +27 -0
  57. package/dist/components/ModelViewer/namedQueryCode.d.ts +12 -0
  58. package/dist/components/ModelViewer/offSurface.d.ts +54 -0
  59. package/dist/components/ModelViewer/rowLayers.d.ts +30 -0
  60. package/dist/components/ModelViewer/runGate.d.ts +49 -0
  61. package/dist/components/ModelViewer/schemaModel.d.ts +109 -0
  62. package/dist/components/ModelViewer/sectionOpenCommand.d.ts +45 -0
  63. package/dist/components/ModelViewer/sourceAccess.d.ts +67 -0
  64. package/dist/components/ModelViewer/sourceLineage.d.ts +14 -0
  65. package/dist/components/ModelViewer/sourcePicker.d.ts +66 -0
  66. package/dist/components/ModelViewer/surface.d.ts +47 -0
  67. package/dist/components/ModelViewer/text.d.ts +6 -0
  68. package/dist/components/ModelViewer/theme.d.ts +63 -0
  69. package/dist/components/ModelViewer/tokens.d.ts +52 -0
  70. package/dist/components/ModelViewer/ui.d.ts +48 -0
  71. package/dist/components/ModelViewer/useDimensionSamples.d.ts +76 -0
  72. package/dist/components/ModelViewer/useModelSources.d.ts +92 -0
  73. package/dist/components/ModelViewer/useRunQuery.d.ts +115 -0
  74. package/dist/components/ReportBuilder/loadMalloy.d.ts +4 -0
  75. package/dist/components/ServerProvider.d.ts +15 -1
  76. package/dist/components/filter/utils.d.ts +1 -1
  77. package/dist/components/given/GivenInput.d.ts +7 -1
  78. package/dist/components/given/GivensPanel.d.ts +3 -1
  79. package/dist/components/given/filterSummary.d.ts +6 -0
  80. package/dist/components/given/useGivenUrlParams.d.ts +11 -0
  81. package/dist/components/index.d.ts +2 -1
  82. package/dist/components/modelPage.d.ts +36 -0
  83. package/dist/hooks/useDimensionalFilterRangeData.d.ts +9 -0
  84. package/dist/hooks/useSuggestOptions.d.ts +26 -2
  85. package/dist/index.cjs.js +47 -19
  86. package/dist/index.es.js +14281 -9879
  87. package/dist/useCompiledDocument--Z7vKZL5.cjs.js +143 -0
  88. package/dist/{useCompiledDocument-Utj2u8CG.es.js → useCompiledDocument-BnT2QpG0.es.js} +6975 -6893
  89. package/dist/utils/filterCondition.d.ts +14 -0
  90. package/dist/utils/malloyLiteral.d.ts +20 -1
  91. package/package.json +2 -2
  92. package/src/components/Model/ExplorerPage.tsx +269 -0
  93. package/src/components/Model/Model.tsx +66 -250
  94. package/src/components/Model/ModelExplorer.spec.tsx +64 -1
  95. package/src/components/ModelViewer/AccessChips.tsx +149 -0
  96. package/src/components/ModelViewer/EmptyRow.tsx +71 -0
  97. package/src/components/ModelViewer/Facts.tsx +46 -0
  98. package/src/components/ModelViewer/FieldGroup.tsx +137 -0
  99. package/src/components/ModelViewer/FieldRow.tsx +566 -0
  100. package/src/components/ModelViewer/GivenFacts.tsx +89 -0
  101. package/src/components/ModelViewer/HeaderBar.tsx +362 -0
  102. package/src/components/ModelViewer/InlineDoc.tsx +50 -0
  103. package/src/components/ModelViewer/ModelGivensSection.tsx +142 -0
  104. package/src/components/ModelViewer/ModelSection.tsx +98 -0
  105. package/src/components/ModelViewer/ModelViewer.spec.tsx +937 -0
  106. package/src/components/ModelViewer/ModelViewer.tsx +777 -0
  107. package/src/components/ModelViewer/QueriesGroup.tsx +107 -0
  108. package/src/components/ModelViewer/RowGlyph.tsx +138 -0
  109. package/src/components/ModelViewer/RunResult.tsx +298 -0
  110. package/src/components/ModelViewer/SampleValues.tsx +97 -0
  111. package/src/components/ModelViewer/SchemaJoin.tsx +258 -0
  112. package/src/components/ModelViewer/SchemaView.tsx +222 -0
  113. package/src/components/ModelViewer/SegmentIcons.tsx +129 -0
  114. package/src/components/ModelViewer/SourcesDropdown.tsx +265 -0
  115. package/src/components/ModelViewer/SourcesNav.tsx +75 -0
  116. package/src/components/ModelViewer/StatusPill.tsx +145 -0
  117. package/src/components/ModelViewer/ValueChip.tsx +39 -0
  118. package/src/components/ModelViewer/ViewRow.tsx +274 -0
  119. package/src/components/ModelViewer/clickableBox.ts +59 -0
  120. package/src/components/ModelViewer/codeDisplay.spec.ts +176 -0
  121. package/src/components/ModelViewer/codeDisplay.ts +118 -0
  122. package/src/components/ModelViewer/docAnnotations.spec.ts +181 -0
  123. package/src/components/ModelViewer/docAnnotations.ts +244 -0
  124. package/src/components/ModelViewer/docAnnotations.typeDetail.spec.ts +114 -0
  125. package/src/components/ModelViewer/explorerModel.spec.ts +43 -0
  126. package/src/components/ModelViewer/explorerModel.ts +31 -0
  127. package/src/components/ModelViewer/extensions.tsx +112 -0
  128. package/src/components/ModelViewer/fieldCode.spec.ts +168 -0
  129. package/src/components/ModelViewer/fieldCode.ts +118 -0
  130. package/src/components/ModelViewer/fieldEntities.ts +63 -0
  131. package/src/components/ModelViewer/fieldExpansion.spec.ts +116 -0
  132. package/src/components/ModelViewer/fieldExpansion.ts +95 -0
  133. package/src/components/ModelViewer/givenUsage.spec.ts +304 -0
  134. package/src/components/ModelViewer/givenUsage.ts +325 -0
  135. package/src/components/ModelViewer/headerCard.tsx +102 -0
  136. package/src/components/ModelViewer/httpStatus.ts +15 -0
  137. package/src/components/ModelViewer/icons.tsx +67 -0
  138. package/src/components/ModelViewer/index.ts +26 -0
  139. package/src/components/ModelViewer/joinAccess.spec.ts +95 -0
  140. package/src/components/ModelViewer/joinAccess.ts +54 -0
  141. package/src/components/ModelViewer/malloyRunText.spec.ts +48 -0
  142. package/src/components/ModelViewer/malloyRunText.ts +51 -0
  143. package/src/components/ModelViewer/malloyText.spec.ts +47 -0
  144. package/src/components/ModelViewer/malloyText.ts +77 -0
  145. package/src/components/ModelViewer/modelGivens.ts +228 -0
  146. package/src/components/ModelViewer/modelGivensContext.ts +13 -0
  147. package/src/components/ModelViewer/modelHeaderPresentation.spec.ts +31 -0
  148. package/src/components/ModelViewer/modelHeaderPresentation.ts +41 -0
  149. package/src/components/ModelViewer/modelTextDeclarations.spec.ts +336 -0
  150. package/src/components/ModelViewer/modelTextDeclarations.ts +228 -0
  151. package/src/components/ModelViewer/modelTextParse.ts +112 -0
  152. package/src/components/ModelViewer/namedQueryCode.spec.ts +58 -0
  153. package/src/components/ModelViewer/namedQueryCode.ts +26 -0
  154. package/src/components/ModelViewer/offSurface.tsx +124 -0
  155. package/src/components/ModelViewer/rowLayers.tsx +104 -0
  156. package/src/components/ModelViewer/runGate.spec.ts +219 -0
  157. package/src/components/ModelViewer/runGate.ts +145 -0
  158. package/src/components/ModelViewer/schemaModel.defaultSource.spec.ts +126 -0
  159. package/src/components/ModelViewer/schemaModel.queries.spec.ts +37 -0
  160. package/src/components/ModelViewer/schemaModel.ts +300 -0
  161. package/src/components/ModelViewer/sectionOpenCommand.tsx +125 -0
  162. package/src/components/ModelViewer/sourceAccess.spec.ts +130 -0
  163. package/src/components/ModelViewer/sourceAccess.ts +194 -0
  164. package/src/components/ModelViewer/sourceLineage.spec.ts +73 -0
  165. package/src/components/ModelViewer/sourceLineage.ts +38 -0
  166. package/src/components/ModelViewer/sourcePicker.spec.ts +294 -0
  167. package/src/components/ModelViewer/sourcePicker.ts +162 -0
  168. package/src/components/ModelViewer/surface.ts +51 -0
  169. package/src/components/ModelViewer/text.tsx +33 -0
  170. package/src/components/ModelViewer/theme.spec.ts +38 -0
  171. package/src/components/ModelViewer/theme.ts +58 -0
  172. package/src/components/ModelViewer/tokens.ts +87 -0
  173. package/src/components/ModelViewer/ui.tsx +201 -0
  174. package/src/components/ModelViewer/useDimensionSamples.spec.tsx +139 -0
  175. package/src/components/ModelViewer/useDimensionSamples.ts +220 -0
  176. package/src/components/ModelViewer/useModelSources.spec.ts +110 -0
  177. package/src/components/ModelViewer/useModelSources.ts +238 -0
  178. package/src/components/ModelViewer/useRunQuery.spec.ts +50 -0
  179. package/src/components/ModelViewer/useRunQuery.ts +223 -0
  180. package/src/components/ReportBuilder/emptyStart.spec.ts +8 -0
  181. package/src/components/ReportBuilder/loadMalloy.ts +8 -1
  182. package/src/components/ServerProvider.tsx +27 -3
  183. package/src/components/filter/utils.ts +4 -60
  184. package/src/components/given/FilterButton.tsx +4 -1
  185. package/src/components/given/GivenInput.tsx +8 -0
  186. package/src/components/given/GivensPanel.tsx +6 -1
  187. package/src/components/given/filterSummary.spec.ts +31 -1
  188. package/src/components/given/filterSummary.ts +41 -19
  189. package/src/components/given/useGivenUrlParams.spec.tsx +32 -0
  190. package/src/components/given/useGivenUrlParams.ts +49 -14
  191. package/src/components/index.ts +6 -1
  192. package/src/components/legacy/malloynb/Notebook.spec.tsx +38 -4
  193. package/src/components/legacy/malloynb/NotebookCell.tsx +8 -2
  194. package/src/components/modelPage.spec.ts +79 -0
  195. package/src/components/modelPage.ts +48 -0
  196. package/src/hooks/useDimensionFilters.ts +3 -58
  197. package/src/hooks/useDimensionFiltersQuery.ts +3 -58
  198. package/src/hooks/useDimensionalFilterRangeData.spec.ts +57 -0
  199. package/src/hooks/useDimensionalFilterRangeData.ts +8 -63
  200. package/src/hooks/useSuggestOptions.dom.spec.ts +54 -1
  201. package/src/hooks/useSuggestOptions.spec.ts +71 -0
  202. package/src/hooks/useSuggestOptions.ts +102 -12
  203. package/src/utils/filterCondition.spec.ts +87 -0
  204. package/src/utils/filterCondition.ts +66 -0
  205. package/src/utils/malloyLiteral.spec.ts +53 -1
  206. package/src/utils/malloyLiteral.ts +47 -12
  207. package/dist/ServerProvider-BvD7PYZ7.cjs.js +0 -1
  208. package/dist/useCompiledDocument-CRhFBKv_.cjs.js +0 -142
@@ -0,0 +1,181 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import {
6
+ getTagLines,
7
+ hasPersistTag,
8
+ hasPreaggregateTag,
9
+ getDocParts,
10
+ ownSourceDoc,
11
+ tagNames,
12
+ } from "./docAnnotations";
13
+
14
+ describe("tagNames", () => {
15
+ it("names a tag without its parameters", () => {
16
+ expect(
17
+ tagNames(["line_chart { y=season_share series=common_name }"]),
18
+ ).toEqual(["line_chart"]);
19
+ expect(tagNames(["bar_chart { y=species_per_checklist }"])).toEqual([
20
+ "bar_chart",
21
+ ]);
22
+ expect(tagNames(["bar_chart"])).toEqual(["bar_chart"]);
23
+ });
24
+
25
+ it("leaves out valued settings and removed tags, and names each tag once", () => {
26
+ expect(tagNames(["bar_chart size=lg", "size=sm"])).toEqual(["bar_chart"]);
27
+ expect(tagNames(["-bar_chart line_chart", "line_chart"])).toEqual([
28
+ "line_chart",
29
+ ]);
30
+ expect(tagNames(['label="Revenue" currency=euro'])).toEqual([]);
31
+ expect(
32
+ tagNames(["dashboard { table.max_height=none }", "percent"]),
33
+ ).toEqual(["dashboard", "percent"]);
34
+ });
35
+
36
+ it("names the chart of the `viz=` spelling, and nothing else valued", () => {
37
+ expect(
38
+ tagNames(["viz=bar { title='Flights' subtitle='Top 10' }"]),
39
+ ).toEqual(["bar"]);
40
+ expect(tagNames(["viz=line size=lg"])).toEqual(["line"]);
41
+ });
42
+
43
+ it("names a line Malloy can't parse by its first word", () => {
44
+ expect(tagNames(["line_chart { y="])).toEqual(["line_chart"]);
45
+ expect(tagNames(["size= {"])).toEqual([]);
46
+ });
47
+ });
48
+
49
+ describe("getTagLines", () => {
50
+ it("returns renderer/behavior tags with the # prefix stripped", () => {
51
+ expect(
52
+ getTagLines([{ value: "# bar_chart" }, { value: "# size=lg" }]),
53
+ ).toEqual(["bar_chart", "size=lg"]);
54
+ });
55
+
56
+ it("excludes doc lines — those belong to getDocString", () => {
57
+ expect(
58
+ getTagLines([
59
+ { value: '#" Top carriers by flight count.' },
60
+ { value: '#(doc) "Carrier docs"' },
61
+ { value: "# line_chart" },
62
+ ]),
63
+ ).toEqual(["line_chart"]);
64
+ });
65
+
66
+ it("excludes namespaced system tags — (malloy)/(index) metadata is not a renderer tag", () => {
67
+ expect(getTagLines([{ value: "#(index)" }])).toEqual([]);
68
+ expect(
69
+ getTagLines([{ value: '#(malloy) drill_filters = [{ code = "x" }]' }]),
70
+ ).toEqual([]);
71
+ expect(getTagLines([{ value: "#@ persist refresh=7d" }])).toEqual([
72
+ "persist refresh=7d",
73
+ ]);
74
+ });
75
+
76
+ it("returns [] for missing or empty annotations", () => {
77
+ expect(getTagLines(undefined)).toEqual([]);
78
+ expect(getTagLines([{ value: " " }])).toEqual([]);
79
+ });
80
+ });
81
+
82
+ describe("hasPersistTag", () => {
83
+ it("detects the #@ persist source tagline (with or without params)", () => {
84
+ expect(hasPersistTag([{ value: "#@ persist" }])).toBe(true);
85
+ expect(
86
+ hasPersistTag([{ value: "#@ persist refresh=7d name=orders_t" }]),
87
+ ).toBe(true);
88
+ });
89
+
90
+ it("does not fire on docs, renderer tags, or persist-prefixed words", () => {
91
+ expect(hasPersistTag([{ value: '#" persisted data notes' }])).toBe(false);
92
+ expect(hasPersistTag([{ value: "# bar_chart" }])).toBe(false);
93
+ expect(hasPersistTag([{ value: "#@ persistence_mode" }])).toBe(false);
94
+ expect(hasPersistTag(undefined)).toBe(false);
95
+ });
96
+ });
97
+
98
+ describe("hasPreaggregateTag", () => {
99
+ it("detects the #@ preaggregate measure tagline, which always carries a grain", () => {
100
+ expect(
101
+ hasPreaggregateTag([
102
+ { value: '#@ preaggregate grain="category_grain"' },
103
+ ]),
104
+ ).toBe(true);
105
+ expect(
106
+ hasPreaggregateTag([
107
+ { value: '#@ preaggregate grain="a, b" refresh=1d' },
108
+ ]),
109
+ ).toBe(true);
110
+ });
111
+
112
+ it("does not fire on docs, renderer tags, or the sibling persistence verb", () => {
113
+ expect(
114
+ hasPreaggregateTag([{ value: '#" preaggregate this later' }]),
115
+ ).toBe(false);
116
+ expect(hasPreaggregateTag([{ value: "# bar_chart" }])).toBe(false);
117
+ expect(
118
+ hasPreaggregateTag([{ value: "#@ preaggregated_elsewhere" }]),
119
+ ).toBe(false);
120
+ // The two verbs are different declarations on different grains; neither
121
+ // may be read as the other.
122
+ expect(hasPreaggregateTag([{ value: "#@ persist" }])).toBe(false);
123
+ expect(hasPersistTag([{ value: '#@ preaggregate grain="c"' }])).toBe(
124
+ false,
125
+ );
126
+ expect(hasPreaggregateTag(undefined)).toBe(false);
127
+ });
128
+ });
129
+
130
+ describe("ownSourceDoc", () => {
131
+ // A source that extends another carries the base's doc ahead of its own, as
132
+ // `order_items` over an unjoined base does in the ecommerce sample.
133
+ const base = {
134
+ name: "order_items_base",
135
+ docParts: ["Order line items, unjoined: one row per item sold."],
136
+ };
137
+ const joined = {
138
+ name: "order_items",
139
+ docParts: [
140
+ ...base.docParts,
141
+ "Order line items joined to inventory, product and customer.",
142
+ ],
143
+ };
144
+
145
+ it("drops the doc a source inherited from the source it extends", () => {
146
+ expect(ownSourceDoc(joined, base)).toBe(
147
+ "Order line items joined to inventory, product and customer.",
148
+ );
149
+ });
150
+
151
+ it("keeps a doc that merely starts like an unrelated source's: only its base's is inherited", () => {
152
+ // Shared boilerplate between two sources that don't extend each other.
153
+ expect(ownSourceDoc(joined, undefined)).toBe(joined.docParts.join("\n"));
154
+ });
155
+
156
+ it("keeps an inherited doc when the source has none of its own", () => {
157
+ const bare = {
158
+ name: "order_items_w_synergy",
159
+ docParts: [...base.docParts],
160
+ };
161
+ expect(ownSourceDoc(bare, base)).toBe(
162
+ "Order line items, unjoined: one row per item sold.",
163
+ );
164
+ });
165
+
166
+ it("keeps everything when its base's doc isn't what it starts with", () => {
167
+ expect(ownSourceDoc(joined, { docParts: ["Something else."] })).toBe(
168
+ joined.docParts.join("\n"),
169
+ );
170
+ });
171
+
172
+ it("reads each doc annotation as a part", () => {
173
+ expect(
174
+ getDocParts([
175
+ { value: '#(doc) "One."\n' },
176
+ { value: "# bar_chart\n" },
177
+ { value: '#" Two.' },
178
+ ]),
179
+ ).toEqual(["One.", "Two."]);
180
+ });
181
+ });
@@ -0,0 +1,244 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import type * as Malloy from "@malloydata/malloy-interfaces";
5
+ import { parseAnnotation } from "@malloydata/malloy-tag";
6
+
7
+ /**
8
+ * Extract human-readable documentation from a list of Malloy annotations.
9
+ *
10
+ * Supports both forms a model author can use:
11
+ * - `#" markdown doc comment` lines (Malloy doc comments)
12
+ * - `#(doc) "text"` tag-style annotations
13
+ *
14
+ * Multiple matching lines are joined with newlines. Returns undefined when
15
+ * there is no documentation, so callers can `doc ?? fallback`.
16
+ */
17
+ export function getDocString(
18
+ annotations: Malloy.Annotation[] | undefined,
19
+ ): string | undefined {
20
+ const doc = getDocParts(annotations).join("\n").trim();
21
+ return doc || undefined;
22
+ }
23
+
24
+ /** Each doc annotation's text, in order — what `getDocString` joins. */
25
+ export function getDocParts(
26
+ annotations: Malloy.Annotation[] | undefined,
27
+ ): string[] {
28
+ if (!annotations) return [];
29
+
30
+ const parts: string[] = [];
31
+ for (const annotation of annotations) {
32
+ const text = annotation.value?.trim();
33
+ if (!text) continue;
34
+
35
+ if (text.startsWith('#"')) {
36
+ parts.push(text.slice(2).trim());
37
+ } else if (text.startsWith("#(doc)")) {
38
+ let body = text.slice("#(doc)".length).trim();
39
+ if (body.length >= 2 && body.startsWith('"') && body.endsWith('"')) {
40
+ body = body.slice(1, -1);
41
+ }
42
+ parts.push(body);
43
+ }
44
+ }
45
+ return parts.filter((p) => p.trim() !== "");
46
+ }
47
+
48
+ /**
49
+ * A model file's own doc: its `##"` and `##(doc)` lines (a model's annotations
50
+ * take the double hash), read as `getDocString` reads a source's. Other `##`
51
+ * lines (`##! experimental…`, model tags) are not prose and are left out.
52
+ */
53
+ export function getModelDocString(
54
+ annotations: Malloy.Annotation[] | undefined,
55
+ ): string | undefined {
56
+ const single = (annotations ?? [])
57
+ .map((a) => ({ ...a, value: a.value?.trim() ?? "" }))
58
+ .filter((a) => a.value.startsWith('##"') || a.value.startsWith("##(doc)"))
59
+ .map((a) => ({ ...a, value: a.value.slice(1) }));
60
+ return getDocString(single);
61
+ }
62
+
63
+ /**
64
+ * A source's own doc, without what it inherited. A source that `extend`s another
65
+ * carries the base's doc annotations ahead of its own (the compiled source has no
66
+ * mark saying which are which), so a header printing all of them repeats the base
67
+ * source's description above the one written for this source. `base` is the source
68
+ * this one extends, read from the model's text (sourceLineage's `baseSourceName`);
69
+ * its whole doc, as a leading run of this one's, is what was inherited. Without a
70
+ * known base nothing is dropped: two unrelated sources can share boilerplate. A
71
+ * source with no doc of its own keeps the inherited one — it still describes it.
72
+ */
73
+ export function ownSourceDoc(
74
+ source: { docParts?: string[] },
75
+ base: { docParts?: string[] } | undefined,
76
+ ): string | undefined {
77
+ const parts = source.docParts ?? [];
78
+ const inherited = base?.docParts ?? [];
79
+ const isInherited =
80
+ inherited.length > 0 &&
81
+ inherited.length < parts.length &&
82
+ inherited.every((p, i) => p === parts[i]);
83
+ const doc = (isInherited ? parts.slice(inherited.length) : parts)
84
+ .join("\n")
85
+ .trim();
86
+ return doc || undefined;
87
+ }
88
+
89
+ /**
90
+ * The non-doc annotation lines — renderer/behavior tags like `# bar_chart`,
91
+ * `# size=lg`, or `#(index)` — normalized for chip display: the `#` prefix
92
+ * (and any `#(malloy)`-style parens wrapper) stripped, whitespace trimmed.
93
+ * Doc lines (`#"`, `#(doc)`) are excluded (they're `getDocString`'s). Returns
94
+ * [] when there are none.
95
+ */
96
+ export function getTagLines(
97
+ annotations: Malloy.Annotation[] | undefined,
98
+ ): string[] {
99
+ if (!annotations) return [];
100
+ const tags: string[] = [];
101
+ for (const annotation of annotations) {
102
+ const text = annotation.value?.trim();
103
+ if (!text) continue;
104
+ if (text.startsWith('#"') || text.startsWith("#(doc)")) continue;
105
+ const stripped = text.replace(/^#+@?\s*/, "").trim();
106
+ // Namespaced tags — `#(malloy) drill_filters=…`, `#(index)` — are
107
+ // compiler/system metadata, not author-facing renderer tags; they read
108
+ // as noise on a chip, so only plain `# bar_chart`-style tags surface.
109
+ if (stripped && !stripped.startsWith("(")) tags.push(stripped);
110
+ }
111
+ return tags;
112
+ }
113
+
114
+ /**
115
+ * What a view's tag lines (getTagLines) make it, as names for its row: the chart
116
+ * type and any other bare tag (`line_chart`, `dashboard`, `percent`), each once.
117
+ * A tag's parameters (`{ y=checklist_count }`) and valued settings (`size=lg`)
118
+ * are left to the view's code, where they are read in full (except `viz=bar`, the
119
+ * chart type's other spelling, which names `bar`): on the row they
120
+ * crowded its name off the line. A removed tag (`-bar_chart`) names nothing.
121
+ *
122
+ * Parsed by Malloy's own tag parser; a line it can't parse names its first word,
123
+ * cut at any `{` or `=`.
124
+ */
125
+ export function tagNames(tagLines: string[]): string[] {
126
+ const names = new Set<string>();
127
+ for (const line of tagLines) {
128
+ const { tag, log } = parseAnnotation([`# ${line}`]);
129
+ if (log.length > 0) {
130
+ const first = /^[A-Za-z_][\w.]*/.exec(line.trim())?.[0];
131
+ if (first && !/^\s*[\w.]+\s*=/.test(line)) names.add(first);
132
+ continue;
133
+ }
134
+ for (const [name, property] of tag.entries()) {
135
+ if (property.deleted) continue;
136
+ if (property.eq === undefined) names.add(name);
137
+ // `# viz=bar { … }` is `# bar_chart` spelled with a value: name the chart.
138
+ else if (name === "viz" && typeof property.eq === "string")
139
+ names.add(property.eq);
140
+ }
141
+ }
142
+ return [...names];
143
+ }
144
+
145
+ /**
146
+ * Whether a source's annotations carry a Malloy Persistence `persist` tag
147
+ * (`#@ persist …`) — the marker that this source is served from a
148
+ * materialized table rather than computed live.
149
+ */
150
+ export function hasPersistTag(
151
+ annotations: Malloy.Annotation[] | undefined,
152
+ ): boolean {
153
+ return hasPersistenceTag(annotations, "persist");
154
+ }
155
+
156
+ /**
157
+ * Whether a measure's annotations carry a Malloy Persistence `preaggregate` tag
158
+ * (`#@ preaggregate grain="…"`) — the marker that this measure is rolled up
159
+ * into a table grouped at that grain.
160
+ *
161
+ * <p>The measure-level counterpart to {@link hasPersistTag}. Like it, this
162
+ * reports what the model DECLARES, which is true whether or not a rollup has
163
+ * been built — the built artifact is a binding, and the two are shown
164
+ * differently on purpose (see {@link import("./DeclarationPill").DeclarationPill}).
165
+ */
166
+ export function hasPreaggregateTag(
167
+ annotations: Malloy.Annotation[] | undefined,
168
+ ): boolean {
169
+ return hasPersistenceTag(annotations, "preaggregate");
170
+ }
171
+
172
+ /** Shared matcher for the `#@ <verb>` persistence annotations. */
173
+ function hasPersistenceTag(
174
+ annotations: Malloy.Annotation[] | undefined,
175
+ verb: string,
176
+ ): boolean {
177
+ const re = new RegExp(`^#@?\\s*${verb}\\b`);
178
+ return (annotations ?? []).some((a) => re.test(a.value?.trim() ?? ""));
179
+ }
180
+
181
+ /**
182
+ * A field's type as a reader needs it, with the detail the kind alone drops:
183
+ * a time field's timeframe ("date · month"), a number's subtype
184
+ * ("number · integer"), an array's element type ("array<string>"), a record's
185
+ * field count, a native column's SQL type.
186
+ */
187
+ export function atomicTypeDetail(
188
+ type: Malloy.AtomicType | undefined,
189
+ ): string | undefined {
190
+ const kind = atomicTypeLabel(type);
191
+ if (!type || !kind) return kind;
192
+ switch (type.kind) {
193
+ case "number_type":
194
+ return type.subtype ? `${kind} · ${type.subtype}` : kind;
195
+ case "date_type":
196
+ case "timestamp_type":
197
+ case "timestamptz_type":
198
+ return type.timeframe ? `${kind} · ${type.timeframe}` : kind;
199
+ case "sql_native_type":
200
+ return type.sql_type ? `sql native · ${type.sql_type}` : "sql native";
201
+ case "array_type":
202
+ return `array<${atomicTypeDetail(type.element_type) ?? "unknown"}>`;
203
+ case "record_type": {
204
+ const n = type.fields.length;
205
+ return `record · ${n} ${n === 1 ? "field" : "fields"}`;
206
+ }
207
+ default:
208
+ return kind;
209
+ }
210
+ }
211
+
212
+ /** A record's fields, one per line ("name string"), for its type chip's tooltip;
213
+ * undefined for any other type. */
214
+ export function recordFieldsTip(
215
+ type: Malloy.AtomicType | undefined,
216
+ ): string | undefined {
217
+ if (type?.kind !== "record_type") return undefined;
218
+ return type.fields
219
+ .map((f) => `${f.name} ${atomicTypeDetail(f.type) ?? ""}`.trim())
220
+ .join("\n");
221
+ }
222
+
223
+ /**
224
+ * A field's author-facing tags (`# label="Revenue"`, `# currency`, `# percent`,
225
+ * `# number="0.0%"`) as `getTagLines` reads them, less the persistence
226
+ * tags (`#@ preaggregate …`), which the row states as its own pill.
227
+ */
228
+ export function getFieldTagLines(
229
+ annotations: Malloy.Annotation[] | undefined,
230
+ ): string[] {
231
+ const persistence = (annotations ?? []).filter((a) =>
232
+ /^#@/.test(a.value?.trim() ?? ""),
233
+ );
234
+ return getTagLines(
235
+ (annotations ?? []).filter((a) => !persistence.includes(a)),
236
+ );
237
+ }
238
+
239
+ /** Strip the "_type" suffix off an AtomicType kind ("string_type" → "string"). */
240
+ export function atomicTypeLabel(
241
+ type: Malloy.AtomicType | undefined,
242
+ ): string | undefined {
243
+ return type?.kind?.replace(/_type$/, "");
244
+ }
@@ -0,0 +1,114 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import type * as Malloy from "@malloydata/malloy-interfaces";
6
+ import {
7
+ atomicTypeDetail,
8
+ getFieldTagLines,
9
+ getModelDocString,
10
+ recordFieldsTip,
11
+ } from "./docAnnotations";
12
+ import { modelDocOf } from "./useModelSources";
13
+
14
+ // A field's type chip says what the kind alone drops, and its tag pills are the
15
+ // author's display tags, never the platform's persistence tags.
16
+ const t = (type: unknown) => type as Malloy.AtomicType;
17
+
18
+ describe("atomicTypeDetail", () => {
19
+ it("keeps a plain kind as it is", () => {
20
+ expect(atomicTypeDetail(t({ kind: "string_type" }))).toBe("string");
21
+ expect(atomicTypeDetail(t({ kind: "number_type" }))).toBe("number");
22
+ });
23
+
24
+ it("adds a number's subtype and a time field's timeframe", () => {
25
+ expect(
26
+ atomicTypeDetail(t({ kind: "number_type", subtype: "integer" })),
27
+ ).toBe("number · integer");
28
+ expect(
29
+ atomicTypeDetail(t({ kind: "date_type", timeframe: "month" })),
30
+ ).toBe("date · month");
31
+ expect(
32
+ atomicTypeDetail(t({ kind: "timestamp_type", timeframe: "hour" })),
33
+ ).toBe("timestamp · hour");
34
+ });
35
+
36
+ it("names an array's element type and a native column's SQL type", () => {
37
+ expect(
38
+ atomicTypeDetail(
39
+ t({ kind: "array_type", element_type: { kind: "string_type" } }),
40
+ ),
41
+ ).toBe("array<string>");
42
+ expect(
43
+ atomicTypeDetail(
44
+ t({ kind: "sql_native_type", sql_type: "GEOGRAPHY" }),
45
+ ),
46
+ ).toBe("sql native · GEOGRAPHY");
47
+ });
48
+
49
+ it("counts a record's fields, and lists them for its tooltip", () => {
50
+ const record = t({
51
+ kind: "record_type",
52
+ fields: [
53
+ { name: "city", type: { kind: "string_type" } },
54
+ { name: "zip", type: { kind: "number_type", subtype: "integer" } },
55
+ ],
56
+ });
57
+ expect(atomicTypeDetail(record)).toBe("record · 2 fields");
58
+ expect(recordFieldsTip(record)).toBe("city string\nzip number · integer");
59
+ expect(recordFieldsTip(t({ kind: "string_type" }))).toBeUndefined();
60
+ });
61
+ });
62
+
63
+ describe("getFieldTagLines", () => {
64
+ it("keeps display tags and drops docs, namespaced and persistence tags", () => {
65
+ const annotations = [
66
+ { value: '# label="Revenue"\n' },
67
+ { value: "# currency\n" },
68
+ { value: '#(doc) "Total revenue"\n' },
69
+ { value: "#(index)\n" },
70
+ { value: '#@ preaggregate grain="status"\n' },
71
+ ];
72
+ expect(getFieldTagLines(annotations)).toEqual([
73
+ 'label="Revenue"',
74
+ "currency",
75
+ ]);
76
+ });
77
+ });
78
+
79
+ describe("getModelDocString", () => {
80
+ it("reads a model's ##(doc) and ##\" lines, and leaves other ## lines out", () => {
81
+ const annotations = [
82
+ { value: "##! experimental.persistence\n" },
83
+ {
84
+ value: '##(doc) "Orders, with the rules that govern who reads them."\n',
85
+ },
86
+ { value: '##" Built from public.orders.\n' },
87
+ { value: '#(doc) "a source doc, not the model\'s"\n' },
88
+ ];
89
+ expect(getModelDocString(annotations)).toBe(
90
+ "Orders, with the rules that govern who reads them.\nBuilt from public.orders.",
91
+ );
92
+ expect(
93
+ getModelDocString([{ value: "##! experimental\n" }]),
94
+ ).toBeUndefined();
95
+ });
96
+ });
97
+
98
+ describe("modelDocOf", () => {
99
+ it("prefers modelInfo's annotations, and reads the file's ## lines when it has none", () => {
100
+ const withDoc = JSON.stringify({
101
+ entries: [],
102
+ annotations: [{ value: '##(doc) "From modelInfo"\n' }],
103
+ });
104
+ expect(modelDocOf(withDoc, '##(doc) "From text"')).toBe("From modelInfo");
105
+ const without = JSON.stringify({ entries: [] });
106
+ expect(
107
+ modelDocOf(
108
+ without,
109
+ '##! experimental\n##(doc) "From text"\nsource: x is y',
110
+ ),
111
+ ).toBe("From text");
112
+ expect(modelDocOf(undefined, "source: x is y")).toBeUndefined();
113
+ });
114
+ });
@@ -0,0 +1,43 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import type * as Malloy from "@malloydata/malloy-interfaces";
5
+ import { describe, expect, it } from "bun:test";
6
+ import { requiredFiltersBySource, sourceNameSet } from "./explorerModel";
7
+
8
+ function sourceInfo(
9
+ name: string,
10
+ annotations: string[] = [],
11
+ ): Malloy.SourceInfo {
12
+ return {
13
+ name,
14
+ schema: { fields: [] },
15
+ annotations: annotations.map((value) => ({ value })),
16
+ } as Malloy.SourceInfo;
17
+ }
18
+
19
+ describe("requiredFiltersBySource", () => {
20
+ it("lists each source's required, visible filters and omits sources with none", () => {
21
+ const out = requiredFiltersBySource([
22
+ sourceInfo("orders", [
23
+ "#(filter) name=region dimension=region type=equal required\n",
24
+ "#(filter) name=tenant dimension=tenant type=equal implicit required\n",
25
+ "#(filter) name=status dimension=status type=equal\n",
26
+ ]),
27
+ sourceInfo("plain"),
28
+ ]);
29
+ expect([...out]).toEqual([["orders", ["region"]]]);
30
+ });
31
+ });
32
+
33
+ describe("sourceNameSet", () => {
34
+ it("names every source once", () => {
35
+ const names = sourceNameSet([
36
+ { name: "a" },
37
+ { name: "b" },
38
+ { name: "a" },
39
+ ]);
40
+ expect([...names]).toEqual(["a", "b"]);
41
+ expect(sourceNameSet([]).size).toBe(0);
42
+ });
43
+ });
@@ -0,0 +1,31 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import type * as Malloy from "@malloydata/malloy-interfaces";
5
+ import { parseSourceFilterAnnotations } from "./modelGivens";
6
+
7
+ /**
8
+ * Each source's required filters, by source name, for the sources that declare
9
+ * any. Only the annotations: a required filter is declared there, and the field
10
+ * walk the full metadata does is for filter controls this surface doesn't render.
11
+ */
12
+ export function requiredFiltersBySource(
13
+ sourceInfos: Malloy.SourceInfo[],
14
+ ): Map<string, string[]> {
15
+ const out = new Map<string, string[]>();
16
+ for (const info of sourceInfos) {
17
+ const required = parseSourceFilterAnnotations(info.annotations)
18
+ .filter((f) => f.required && !f.implicit)
19
+ .map((f) => f.name);
20
+ if (required.length > 0) out.set(info.name, required);
21
+ }
22
+ return out;
23
+ }
24
+
25
+ /** The names of the model's sources: what lineage, inherited givens and a
26
+ * same-model join target are resolved against. */
27
+ export function sourceNameSet(
28
+ sources: readonly { name: string }[],
29
+ ): ReadonlySet<string> {
30
+ return new Set(sources.map((s) => s.name));
31
+ }