Add unknown token type reporting

This commit is contained in:
Andrey Fedorov 2025-05-13 11:52:43 +02:00 committed by Andrés Moya
parent 0cb6e0dee2
commit d9f98008f4
3 changed files with 81 additions and 29 deletions

View file

@ -826,6 +826,27 @@
(map-indexed (fn [index item]
(assoc item :index index))))))
(defn get-tokens-of-unknown-type
"Recursively search the tokens for unknown types"
[tokens token-path dctg?]
(let [type-key (if dctg? "$type" "type")]
(reduce-kv
(fn [unknown-tokens k v]
(let [child-path (if (empty? token-path)
(name k)
(str token-path "." k))]
(if (and (map? v)
(not (contains? v type-key)))
(let [nested-unknown-tokens (get-tokens-of-unknown-type v child-path dctg?)]
(merge unknown-tokens nested-unknown-tokens))
(let [token-type-str (get v type-key)
token-type (cto/dtcg-token-type->token-type token-type-str)]
(if (and (not (some? token-type)) (some? token-type-str))
(assoc unknown-tokens child-path token-type-str)
unknown-tokens)))))
{}
tokens)))
(defn flatten-nested-tokens-json
"Recursively flatten the dtcg token structure, joining keys with '.'."
[tokens token-path]

View file

@ -13,9 +13,12 @@
[app.common.schema :as sm]
[app.common.transit :as t]
[app.common.types.tokens-lib :as ctob]
[app.main.data.notifications :as ntf]
[app.main.data.tinycolor :as tinycolor]
[app.main.data.workspace.tokens.errors :as wte]
[app.main.data.workspace.tokens.warnings :as wtw]
[app.main.store :as st]
[app.util.i18n :refer [tr]]
[app.util.time :as dt]
[beicon.v2.core :as rx]
[cuerdas.core :as str]
@ -81,8 +84,7 @@
(let [parsed-value (cft/parse-token-value value)
out-of-scope (not (<= 0 (:value parsed-value) 1))
references (seq (ctob/find-token-value-references value))]
(cond
(and parsed-value (not out-of-scope))
(cond (and parsed-value (not out-of-scope))
parsed-value
references
@ -167,11 +169,13 @@
(parse-sd-token-numeric-value value))
output-token (cond (:errors parsed-token-value)
(merge origin-token parsed-token-value)
(:warnings parsed-token-value)
(assoc origin-token
:resolved-value (:value parsed-token-value)
:warnings (:warnings parsed-token-value)
:unit (:unit parsed-token-value))
:else
(assoc origin-token
:resolved-value (:value parsed-token-value)
@ -280,6 +284,19 @@
(when name-error?
(wte/error-ex-info :error.import/invalid-token-name (:value schema-error) err))))
(defn- group-by-value [m]
(reduce (fn [acc [k v]]
(update acc v conj k)) {} m))
(defn- tokens-of-unknown-type-warning [unknown-tokens]
(let [type->tokens (group-by-value unknown-tokens)]
(ntf/show {:content (tr "workspace.token.unknown-token-type")
:detail (->> (for [[token-type tokens] type->tokens]
(tr "workspace.token.unknown-token-type-section" token-type (count tokens)))
(str/join "\n"))
:type :toast
:level :info})))
(defn process-json-stream
([data-stream]
(process-json-stream nil data-stream))
@ -293,7 +310,12 @@
(throw (wte/error-ex-info :error.import/json-parse-error data e))))))
(rx/map (fn [json-data]
(let [single-set? (ctob/single-set? json-data)
json-format (ctob/get-json-format json-data)]
json-format (ctob/get-json-format json-data)
unknown-tokens (ctob/get-tokens-of-unknown-type
json-data
""
(= json-format :json-format/dtcg))]
{:tokens-lib
(try
(cond
(and single-set?
@ -313,8 +335,11 @@
(catch js/Error e
(let [err (or (name-error e)
(wte/error-ex-info :error.import/invalid-json-data json-data e))]
(throw err)))))))
(rx/mapcat (fn [tokens-lib]
(throw err))))
:unknown-tokens unknown-tokens})))
(rx/mapcat (fn [{:keys [tokens-lib unknown-tokens]}]
(when unknown-tokens
(st/emit! (tokens-of-unknown-type-warning unknown-tokens)))
(try
(-> (ctob/get-all-tokens tokens-lib)
(resolve-tokens-with-errors+)

View file

@ -7433,3 +7433,9 @@ msgstr "The following files have errors:"
msgid "dashboard.import.import-error.message2"
msgstr "Files with errors will not be uploaded."
msgid "workspace.token.unknown-token-type"
msgstr "Skipped tokens"
msgid "workspace.token.unknown-token-type-section"
msgstr "Type '%s' is not supported (%s)\n"