From 5ce27c9bc7dc93ae9ab593deba4a22a80e6bf50f Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 4 Aug 2022 21:12:54 +0000 Subject: [PATCH] FuzzyJson basic feature --- build.hxml | 5 +++++ src/kiss_tools/FuzzyJson.kiss | 28 ++++++++++++++++++++++++++++ src/kiss_tools/Main.hx | 8 ++++++++ src/kiss_tools/Main.kiss | 9 +++++++++ test.sh | 3 +++ test/fuzzy.json | 5 +++++ test/fuzzy2.json | 3 +++ 7 files changed, 61 insertions(+) create mode 100644 build.hxml create mode 100644 src/kiss_tools/FuzzyJson.kiss create mode 100644 src/kiss_tools/Main.hx create mode 100644 src/kiss_tools/Main.kiss create mode 100644 test.sh create mode 100644 test/fuzzy.json create mode 100644 test/fuzzy2.json diff --git a/build.hxml b/build.hxml new file mode 100644 index 0000000..dfff9aa --- /dev/null +++ b/build.hxml @@ -0,0 +1,5 @@ +-lib kiss +-cp src +--macro kiss.Kiss.setup() +--main kiss_tools.Main +--interp \ No newline at end of file diff --git a/src/kiss_tools/FuzzyJson.kiss b/src/kiss_tools/FuzzyJson.kiss new file mode 100644 index 0000000..9f88eaf --- /dev/null +++ b/src/kiss_tools/FuzzyJson.kiss @@ -0,0 +1,28 @@ +(defMacroVar fuzzyJsons (new StringMap)) + +(defMacroFunction _loadFuzzyJson [key file] + (unless (fuzzyJsons.exists key) + (dictSet fuzzyJsons key (new StringMap))) + (let [map (dictGet fuzzyJsons key) + json (Json.parse (File.getContent file))] + (doFor key (Reflect.fields json) + (if (map.exists key) + // mark the key to throw an error if accessed, + // because it is a duplicate + (dictSet map key null) + (dictSet map key (Reflect.field json key)))))) + +(defMacro loadFuzzyJson [key file] + (_loadFuzzyJson (eval key) (eval file)) + `{}) + +(defMacroFunction _getFuzzyJson [whichJson key] + (let [json (dictGet fuzzyJsons whichJson) + bm (FuzzyMapTools.bestMatch json key)] + (dictGet json bm))) + +(defMacro getFuzzyJson [whichJson key &builder b] + (let [value (_getFuzzyJson (eval whichJson) (eval key))] + (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 diff --git a/src/kiss_tools/Main.hx b/src/kiss_tools/Main.hx new file mode 100644 index 0000000..a56a843 --- /dev/null +++ b/src/kiss_tools/Main.hx @@ -0,0 +1,8 @@ +package kiss_tools; + +import kiss.Kiss; +import kiss.Prelude; +import kiss.List; + +@:build(kiss.Kiss.build()) +class Main {} diff --git a/src/kiss_tools/Main.kiss b/src/kiss_tools/Main.kiss new file mode 100644 index 0000000..02bf594 --- /dev/null +++ b/src/kiss_tools/Main.kiss @@ -0,0 +1,9 @@ +// Test FuzzyJson +(load "FuzzyJson.kiss") + +(loadFuzzyJson "dogs" "test/fuzzy.json") +(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 diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..0ee8ae9 --- /dev/null +++ b/test.sh @@ -0,0 +1,3 @@ +#! /bin/bash + +haxe build.hxml \ No newline at end of file diff --git a/test/fuzzy.json b/test/fuzzy.json new file mode 100644 index 0000000..51b1af2 --- /dev/null +++ b/test/fuzzy.json @@ -0,0 +1,5 @@ +{ + "albert": "is a very good dog", + "ruuuuuby": "is the bestest doggy", + "rangie": "is also my favorite" +} \ No newline at end of file diff --git a/test/fuzzy2.json b/test/fuzzy2.json new file mode 100644 index 0000000..36dc81d --- /dev/null +++ b/test/fuzzy2.json @@ -0,0 +1,3 @@ +{ + "rangie": "is so cute he gets a duplicate entry" +} \ No newline at end of file