The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Render data app schema metadata blocks on one line (#86066)
Timofey Kachalove57ae6f7024d
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/javascript.clj b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/javascript.cljindex 112457391e3..a89ed2efbd6 100644--- a/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/javascript.clj+++ b/enterprise/backend/src/metabase_enterprise/data_apps/generate/schema/javascript.clj@@ -45,10 +45,9 @@Rendering rules: arrays whose items are all literals print on one line;`:call` arguments always print inline; object keys print bare when they are- valid JavaScript identifiers and quoted otherwise. A metadata block whose- values are all scalars prints on one line, so a field costs a reader one line- of the file; one with a nested array or object prints as- `JSON.stringify(x, null, 2)` would, one array item per line. [[render-js]]+ valid JavaScript identifiers and quoted otherwise. A metadata block prints on+ one line, nested arrays and objects included, so an entry's metadata is the+ line before its properties and a reader can find it with `grep`. [[render-js]]prints a `:module`; [[Module]] is the Malli schema for the whole grammar.The printer is deliberately option-free and policy-free: decisions about@@ -149,23 +148,22 @@[nil item]))(defn- metadata-json- "Renders `metadata` as JSON: on one line when every value is a scalar, otherwise as `JSON.stringify(x, null, 2)`- would, with each line after the first indented by `indent`."- [metadata indent]- (if (not-any? coll? (vals metadata))- (str "{ "- (str/join ", " (for [[k v] metadata]- (str (json/encode (u/qualified-name k)) ": " (json/encode v))))- " }")- (str/replace (json/encode metadata {:pretty {:indent-arrays? true :object-field-value-separator ": "}})- "\n" (str "\n" (spaces indent)))))+ "Renders `value` as JSON on one line, nested arrays and objects included."+ [value]+ (cond+ (map? value) (str "{ "+ (str/join ", " (for [[k v] value]+ (str (json/encode (u/qualified-name k)) ": " (metadata-json v))))+ " }")+ (sequential? value) (str "[ " (str/join ", " (map metadata-json value)) " ]")+ :else (json/encode value)))(defn- metadata-block"Returns a `/* metadata: {...} */` block. A `*/` can only occur inside a JSON string, where it is written as theequivalent `*\\/`, so the block always ends where it should."[indent metadata](str (spaces indent) "/* metadata: "- (str/replace (metadata-json metadata indent) "*/" "*\\/")+ (str/replace (metadata-json metadata) "*/" "*\\/")" */"))(declare render-expression)diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/generate/schema/javascript_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schema/javascript_test.cljnew file mode 100644index 00000000000..a565ba6aad7--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/data_apps/generate/schema/javascript_test.clj@@ -0,0 +1,20 @@+(ns metabase-enterprise.data-apps.generate.schema.javascript-test+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.data-apps.generate.schema.javascript :as schemas.javascript]))++(deftest metadata-with-nested-values-renders-on-one-line-test+ (testing "an entry's metadata is the one line before its properties, so a reader can grep for it"+ (is (= (str "const x = {\n"+ " revenue: {\n"+ " /* metadata: { \"description\": \"Paid totals\", \"filters\": [ \"Status is paid\", \"Total is greater than 10\" ], \"sourceTable\": { \"databaseName\": \"Sample\", \"tableName\": \"ORDERS\" } } */\n"+ " type: \"metric\"\n"+ " }\n"+ "} as const;\n")+ (schemas.javascript/render-js+ [:module [:const "x" [:obj ["revenue"+ {:metadata (array-map "description" "Paid totals"+ "filters" ["Status is paid" "Total is greater than 10"]+ "sourceTable" (array-map :databaseName "Sample"+ :tableName "ORDERS"))}+ [:obj ["type" [:lit "metric"]]]]]]])))))