FuzzyJson basic feature
This commit is contained in:
5
build.hxml
Normal file
5
build.hxml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
-lib kiss
|
||||||
|
-cp src
|
||||||
|
--macro kiss.Kiss.setup()
|
||||||
|
--main kiss_tools.Main
|
||||||
|
--interp
|
28
src/kiss_tools/FuzzyJson.kiss
Normal file
28
src/kiss_tools/FuzzyJson.kiss
Normal file
@@ -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)))))
|
8
src/kiss_tools/Main.hx
Normal file
8
src/kiss_tools/Main.hx
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package kiss_tools;
|
||||||
|
|
||||||
|
import kiss.Kiss;
|
||||||
|
import kiss.Prelude;
|
||||||
|
import kiss.List;
|
||||||
|
|
||||||
|
@:build(kiss.Kiss.build())
|
||||||
|
class Main {}
|
9
src/kiss_tools/Main.kiss
Normal file
9
src/kiss_tools/Main.kiss
Normal file
@@ -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"))
|
5
test/fuzzy.json
Normal file
5
test/fuzzy.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"albert": "is a very good dog",
|
||||||
|
"ruuuuuby": "is the bestest doggy",
|
||||||
|
"rangie": "is also my favorite"
|
||||||
|
}
|
3
test/fuzzy2.json
Normal file
3
test/fuzzy2.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"rangie": "is so cute he gets a duplicate entry"
|
||||||
|
}
|
Reference in New Issue
Block a user