takeFuzzyJson

This commit is contained in:
2022-08-05 18:57:22 +00:00
parent 5ce27c9bc7
commit 5f4f612cfa
2 changed files with 17 additions and 8 deletions

View File

@@ -16,13 +16,18 @@
(_loadFuzzyJson (eval key) (eval file)) (_loadFuzzyJson (eval key) (eval file))
`{}) `{})
(defMacroFunction _getFuzzyJson [whichJson key] (defMacroFunction _getFuzzyJson [whichJson key take]
(let [json (dictGet fuzzyJsons whichJson) (let [json (dictGet fuzzyJsons whichJson)
bm (FuzzyMapTools.bestMatch json key)] bm (FuzzyMapTools.bestMatch json key)
(dictGet json bm))) val (dictGet json bm)]
(when take (json.remove bm))
val))
(defMacro getFuzzyJson [whichJson key &builder b] (defMacro getFuzzyJson [whichJson key &opt take &builder b]
(let [value (_getFuzzyJson (eval whichJson) (eval key))] (let [value (_getFuzzyJson (eval whichJson) (eval key) take)]
(if !(= null value) (if !(= null value)
`(haxe.Json.parse ,(Json.stringify value)) `(haxe.Json.parse ,(Json.stringify value))
`(throw (+ ,key " has a duplicate definition between multiple files of fuzzy json " ,whichJson))))) (throw (+ (eval key) " has a duplicate definition between multiple files of fuzzy json " (eval whichJson))))))
(defMacro takeFuzzyJson [whichJson key]
`(getFuzzyJson ,whichJson ,key true))

View File

@@ -5,5 +5,9 @@
(loadFuzzyJson "dogs" "test/fuzzy2.json") (loadFuzzyJson "dogs" "test/fuzzy2.json")
(assert (= "is a very good dog" (getFuzzyJson "dogs" "Albort"))) (assert (= "is a very good dog" (getFuzzyJson "dogs" "Albort")))
// duplicate definitions throw an error // takeFuzzyJson removes the match to make following fuzzyJson retrievals save time:
(assertThrows (getFuzzyJson "dogs" "Rangie")) (assert (= "is a very good dog" (takeFuzzyJson "dogs" "Albort")))
// No good match will cause crash at compile time:
(assertThrowsAtCompileTime (getFuzzyJson "dogs" "Albort"))
// duplicate definitions throw an error at compile time:
(assertThrowsAtCompileTime (getFuzzyJson "dogs" "Rangie"))