From 5f4f612cfae0979bb8a6b875be39a2d45a5ea598 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 5 Aug 2022 18:57:22 +0000 Subject: [PATCH] takeFuzzyJson --- src/kiss_tools/FuzzyJson.kiss | 17 +++++++++++------ src/kiss_tools/Main.kiss | 8 ++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/kiss_tools/FuzzyJson.kiss b/src/kiss_tools/FuzzyJson.kiss index 9f88eaf..6672092 100644 --- a/src/kiss_tools/FuzzyJson.kiss +++ b/src/kiss_tools/FuzzyJson.kiss @@ -16,13 +16,18 @@ (_loadFuzzyJson (eval key) (eval file)) `{}) -(defMacroFunction _getFuzzyJson [whichJson key] +(defMacroFunction _getFuzzyJson [whichJson key take] (let [json (dictGet fuzzyJsons whichJson) - bm (FuzzyMapTools.bestMatch json key)] - (dictGet json bm))) + bm (FuzzyMapTools.bestMatch json key) + val (dictGet json bm)] + (when take (json.remove bm)) + val)) -(defMacro getFuzzyJson [whichJson key &builder b] - (let [value (_getFuzzyJson (eval whichJson) (eval key))] +(defMacro getFuzzyJson [whichJson key &opt take &builder b] + (let [value (_getFuzzyJson (eval whichJson) (eval key) take)] (if !(= null value) `(haxe.Json.parse ,(Json.stringify value)) - `(throw (+ ,key " has a duplicate definition between multiple files of fuzzy json " ,whichJson))))) \ No newline at end of file + (throw (+ (eval key) " has a duplicate definition between multiple files of fuzzy json " (eval whichJson)))))) + +(defMacro takeFuzzyJson [whichJson key] + `(getFuzzyJson ,whichJson ,key true)) \ No newline at end of file diff --git a/src/kiss_tools/Main.kiss b/src/kiss_tools/Main.kiss index 02bf594..65eb5ea 100644 --- a/src/kiss_tools/Main.kiss +++ b/src/kiss_tools/Main.kiss @@ -5,5 +5,9 @@ (loadFuzzyJson "dogs" "test/fuzzy2.json") (assert (= "is a very good dog" (getFuzzyJson "dogs" "Albort"))) -// duplicate definitions throw an error -(assertThrows (getFuzzyJson "dogs" "Rangie")) \ No newline at end of file +// takeFuzzyJson removes the match to make following fuzzyJson retrievals save time: +(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")) \ No newline at end of file