JsonMap and JsonableArray

This commit is contained in:
2023-03-28 03:47:37 -06:00
parent 4c41781ccb
commit 20e49d9569
13 changed files with 134 additions and 18 deletions

View File

@@ -0,0 +1,19 @@
// TODO bug in kiss-vscode new class: it takes the first line of the currently open file,
// instead of intelligently picking a package declaration
package kiss_tools;
import kiss.Prelude;
import kiss.List;
import haxe.ds.Map;
import haxe.Json;
import haxe.DynamicAccess;
import sys.io.File;
typedef Jsonable<T> = {
function stringify():String;
function parse(s:String):T;
}
@:build(kiss.Kiss.build())
class JsonMap<T:Jsonable<T>> {}

View File

@@ -0,0 +1,25 @@
(method _parseFrom [:String representation]
(let [:DynamicAccess<String> json (Json.parse representation)]
(doFor =>key representation json
(dictSet m key (defaultVal.parse representation)))))
(method stringify []
(Json.stringify
(for =>k v m =>k (v.stringify))
null
"\t"))
(defNew [&prop :String jsonPath &prop :T defaultVal]
[:Map<String,T> m (new Map)]
(_parseFrom (File.getContent jsonPath)))
(method put [:String key :T value]
(dictSet m key value)
(File.saveContent jsonPath (stringify)))
(method get [:String key]
(unless (m.exists key)
(put key defaultVal))
(dictGet m key))

View File

@@ -0,0 +1,10 @@
package kiss_tools;
import kiss.Prelude;
import kiss.List;
import kiss_tools.JsonMap;
import haxe.Json;
import haxe.DynamicAccess;
@:build(kiss.Kiss.build())
class JsonableArray<T:Jsonable<T>> {}

View File

@@ -0,0 +1,12 @@
(defNew [&prop :Array<T> elements
&prop :T defaultVal])
(method parse [:String representation]
(let [:Array<String> arr (Json.parse representation)]
(new JsonableArray<T>
(for elem arr
(defaultVal.parse elem))
defaultVal)))
(method stringify []
(Json.stringify (for elem elements (elem.stringify))))

View File

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

View File

@@ -1,13 +0,0 @@
// Test FuzzyJson
(load "FuzzyJson.kiss")
(loadFuzzyJson "dogs" "test/fuzzy.json")
(loadFuzzyJson "dogs" "test/fuzzy2.json")
(assert (= "is a very good dog" (getFuzzyJson "dogs" "Albort")))
// 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"))