🐛 Allow importing file without any token but with themes or sets (#6796)

This commit is contained in:
Andrés Moya 2025-06-27 11:32:14 +02:00 committed by GitHub
parent 3010abbf64
commit 5c4fd97541
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1212,7 +1212,8 @@ Will return a value that matches this schema:
"Searches through decoded token file and returns:
- `:json-format/legacy` when first node satisfies `legacy-node?` predicate
- `:json-format/dtcg` when first node satisfies `dtcg-node?` predicate
- `nil` if neither combination is found"
- If neither combination is found, return dtcg format by default (we assume that
the file does not contain any token, so the format is irrelevan)."
([decoded-json]
(get-json-format decoded-json legacy-node? dtcg-node?))
([decoded-json legacy-node? dtcg-node?]
@ -1230,9 +1231,10 @@ Will return a value that matches this schema:
(check-node node)
(when (branch? node)
(mapcat walk (children node))))))]
(->> (walk decoded-json)
(d/nilv (->> (walk decoded-json)
(filter some?)
first)))) ;; TODO: throw error if format cannot be determined
first)
:json-format/dtcg))))
(defn- legacy-json->dtcg-json
"Converts a decoded json file in legacy format into DTCG format."
@ -1291,9 +1293,17 @@ Will return a value that matches this schema:
[set-name decoded-json-tokens]
(assert (map? decoded-json-tokens) "expected a plain clojure map for `decoded-json-tokens`")
(assert (= (get-json-format decoded-json-tokens) :json-format/dtcg) "expected a dtcg format for `decoded-json-tokens`")
(let [set-name (normalize-set-name set-name)
tokens (flatten-nested-tokens-json decoded-json-tokens "")]
(when (empty? tokens)
(throw (ex-info "the file doesn't contain any tokens"
{:error/code :error.import/invalid-json-data})))
(-> (make-tokens-lib)
(add-set (make-token-set :name (normalize-set-name set-name)
:tokens (flatten-nested-tokens-json decoded-json-tokens "")))))
(add-set (make-token-set :name set-name
:tokens tokens)))))
(defn- parse-single-set-legacy-json
"Parse a decoded json file with a single set of tokens in legacy format into a TokensLib."
@ -1385,6 +1395,10 @@ Will return a value that matches this schema:
library
active-theme-names)]
(when (and (empty? sets) (empty? themes))
(throw (ex-info "the file doesn't contain any tokens"
{:error/code :error.import/invalid-json-data})))
library))
(defn- parse-multi-set-legacy-json