diff --git a/projects/kiss-tools/build.hxml b/projects/kiss-tools/build.hxml new file mode 100644 index 00000000..dfff9aa6 --- /dev/null +++ b/projects/kiss-tools/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/projects/kiss-tools/src/kiss_tools/FuzzyJson.kiss b/projects/kiss-tools/src/kiss_tools/FuzzyJson.kiss new file mode 100644 index 00000000..9f88eafd --- /dev/null +++ b/projects/kiss-tools/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/projects/kiss-tools/src/kiss_tools/Main.hx b/projects/kiss-tools/src/kiss_tools/Main.hx new file mode 100644 index 00000000..a56a8431 --- /dev/null +++ b/projects/kiss-tools/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/projects/kiss-tools/src/kiss_tools/Main.kiss b/projects/kiss-tools/src/kiss_tools/Main.kiss new file mode 100644 index 00000000..02bf594c --- /dev/null +++ b/projects/kiss-tools/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/projects/kiss-tools/test.sh b/projects/kiss-tools/test.sh new file mode 100644 index 00000000..0ee8ae95 --- /dev/null +++ b/projects/kiss-tools/test.sh @@ -0,0 +1,3 @@ +#! /bin/bash + +haxe build.hxml \ No newline at end of file diff --git a/projects/kiss-tools/test/fuzzy.json b/projects/kiss-tools/test/fuzzy.json new file mode 100644 index 00000000..51b1af2f --- /dev/null +++ b/projects/kiss-tools/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/projects/kiss-tools/test/fuzzy2.json b/projects/kiss-tools/test/fuzzy2.json new file mode 100644 index 00000000..36dc81d8 --- /dev/null +++ b/projects/kiss-tools/test/fuzzy2.json @@ -0,0 +1,3 @@ +{ + "rangie": "is so cute he gets a duplicate entry" +} \ No newline at end of file