FuzzyJson basic feature

This commit is contained in:
2022-08-04 21:12:54 +00:00
parent 2fa71f6cf9
commit e6a3d59e3e
7 changed files with 61 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
-lib kiss
-cp src
--macro kiss.Kiss.setup()
--main kiss_tools.Main
--interp

View 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)))))

View File

@@ -0,0 +1,8 @@
package kiss_tools;
import kiss.Kiss;
import kiss.Prelude;
import kiss.List;
@:build(kiss.Kiss.build())
class Main {}

View 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"))

View File

@@ -0,0 +1,3 @@
#! /bin/bash
haxe build.hxml

View File

@@ -0,0 +1,5 @@
{
"albert": "is a very good dog",
"ruuuuuby": "is the bestest doggy",
"rangie": "is also my favorite"
}

View File

@@ -0,0 +1,3 @@
{
"rangie": "is so cute he gets a duplicate entry"
}