JsonMap and JsonableArray

This commit is contained in:
2023-03-28 03:47:37 -06:00
parent 12db1cffd6
commit b32ffe7ec9
13 changed files with 134 additions and 18 deletions

View File

@@ -1,4 +1,4 @@
-lib kiss
-cp src
--main kiss_tools.Main
--main test.Main
--interp

View File

@@ -1,5 +1,4 @@
{
"main": "kiss_tools.Main",
"name": "kiss-tools",
"description": "Tools for Kiss programs, written in Kiss",
"classPath": "src/",

19
src/kiss_tools/JsonMap.hx Normal file
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,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"))

13
src/test/Main.hx Normal file
View File

@@ -0,0 +1,13 @@
package test;
import kiss.Kiss;
import kiss.Prelude;
import kiss.List;
import sys.io.File;
import sys.FileSystem;
import kiss_tools.JsonMap;
import kiss_tools.JsonableArray;
@:build(kiss.Kiss.build())
class Main {}

38
src/test/Main.kiss Normal file
View File

@@ -0,0 +1,38 @@
// Test FuzzyJson
(loadFrom "kiss-tools" "src/kiss_tools/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"))
// Test JsonMap<JsonableArray>
(var TEST_JSON_FILE "testJsonFile.json")
(File.saveContent TEST_JSON_FILE (File.getContent "test/map-start.json"))
(let [jsonMap (new JsonMap<JsonableArray<TestJsonable>> TEST_JSON_FILE (new JsonableArray<TestJsonable>> [] (new TestJsonable "")))
a (jsonMap.get "a")
aString (a.elements.toString)
b (jsonMap.get "b")
bString (b.elements.toString)]
(assert (contains aString "abc"))
(assert (contains aString "def"))
(assert (contains bString "ghi"))
(assert (contains bString "jkl"))
(a.elements.push (new TestJsonable "mno"))
(jsonMap.put "a" a)
(let [c (jsonMap.get "c")]
(assertEquals 0 c.elements.length)))
// Uncomment this line when expanding the test:
// (File.saveContent "test/map-end.json" (File.getContent TEST_JSON_FILE))
(assertEquals (File.getContent "test/map-end.json") (File.getContent TEST_JSON_FILE))
(FileSystem.deleteFile TEST_JSON_FILE)

View File

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

View File

@@ -0,0 +1,5 @@
(defNew [&prop :String s])
(method parse [:String s] (new TestJsonable s))
(method stringify [] s)
(method toString [] s)

5
test/map-end.json Normal file
View File

@@ -0,0 +1,5 @@
{
"c": "[]",
"a": "[\"abc\",\"def\",\"mno\"]",
"b": "[\"ghi\",\"jkl\"]"
}

4
test/map-start.json Normal file
View File

@@ -0,0 +1,4 @@
{
"a": "[\"abc\", \"def\"]",
"b": "[\"ghi\", \"jkl\"]"
}