From b32ffe7ec91f15b52602dfc5fe1d9fa765de1799 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 28 Mar 2023 03:47:37 -0600 Subject: [PATCH] JsonMap and JsonableArray --- build.hxml | 2 +- haxelib.json | 1 - src/kiss_tools/JsonMap.hx | 19 ++++++++++ src/kiss_tools/JsonMap.kiss | 25 ++++++++++++ src/kiss_tools/JsonableArray.hx | 10 +++++ src/kiss_tools/JsonableArray.kiss | 12 ++++++ src/kiss_tools/Main.kiss | 13 ------- src/test/Main.hx | 13 +++++++ src/test/Main.kiss | 38 +++++++++++++++++++ .../Main.hx => test/TestJsonable.hx} | 5 +-- src/test/TestJsonable.kiss | 5 +++ test/map-end.json | 5 +++ test/map-start.json | 4 ++ 13 files changed, 134 insertions(+), 18 deletions(-) create mode 100644 src/kiss_tools/JsonMap.hx create mode 100644 src/kiss_tools/JsonMap.kiss create mode 100644 src/kiss_tools/JsonableArray.hx create mode 100644 src/kiss_tools/JsonableArray.kiss delete mode 100644 src/kiss_tools/Main.kiss create mode 100644 src/test/Main.hx create mode 100644 src/test/Main.kiss rename src/{kiss_tools/Main.hx => test/TestJsonable.hx} (56%) create mode 100644 src/test/TestJsonable.kiss create mode 100644 test/map-end.json create mode 100644 test/map-start.json diff --git a/build.hxml b/build.hxml index 8e03df2..dbdb1f1 100644 --- a/build.hxml +++ b/build.hxml @@ -1,4 +1,4 @@ -lib kiss -cp src ---main kiss_tools.Main +--main test.Main --interp \ No newline at end of file diff --git a/haxelib.json b/haxelib.json index f7bcf16..e2dca34 100644 --- a/haxelib.json +++ b/haxelib.json @@ -1,5 +1,4 @@ { - "main": "kiss_tools.Main", "name": "kiss-tools", "description": "Tools for Kiss programs, written in Kiss", "classPath": "src/", diff --git a/src/kiss_tools/JsonMap.hx b/src/kiss_tools/JsonMap.hx new file mode 100644 index 0000000..bbb4c08 --- /dev/null +++ b/src/kiss_tools/JsonMap.hx @@ -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 = { + function stringify():String; + function parse(s:String):T; +} + +@:build(kiss.Kiss.build()) +class JsonMap> {} diff --git a/src/kiss_tools/JsonMap.kiss b/src/kiss_tools/JsonMap.kiss new file mode 100644 index 0000000..4279372 --- /dev/null +++ b/src/kiss_tools/JsonMap.kiss @@ -0,0 +1,25 @@ +(method _parseFrom [:String representation] + (let [:DynamicAccess 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 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)) + diff --git a/src/kiss_tools/JsonableArray.hx b/src/kiss_tools/JsonableArray.hx new file mode 100644 index 0000000..8c773d2 --- /dev/null +++ b/src/kiss_tools/JsonableArray.hx @@ -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> {} diff --git a/src/kiss_tools/JsonableArray.kiss b/src/kiss_tools/JsonableArray.kiss new file mode 100644 index 0000000..165f851 --- /dev/null +++ b/src/kiss_tools/JsonableArray.kiss @@ -0,0 +1,12 @@ +(defNew [&prop :Array elements + &prop :T defaultVal]) + +(method parse [:String representation] + (let [:Array arr (Json.parse representation)] + (new JsonableArray + (for elem arr + (defaultVal.parse elem)) + defaultVal))) + +(method stringify [] + (Json.stringify (for elem elements (elem.stringify)))) \ No newline at end of file diff --git a/src/kiss_tools/Main.kiss b/src/kiss_tools/Main.kiss deleted file mode 100644 index 65eb5ea..0000000 --- a/src/kiss_tools/Main.kiss +++ /dev/null @@ -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")) \ No newline at end of file diff --git a/src/test/Main.hx b/src/test/Main.hx new file mode 100644 index 0000000..a846f96 --- /dev/null +++ b/src/test/Main.hx @@ -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 {} diff --git a/src/test/Main.kiss b/src/test/Main.kiss new file mode 100644 index 0000000..810d169 --- /dev/null +++ b/src/test/Main.kiss @@ -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 +(var TEST_JSON_FILE "testJsonFile.json") + +(File.saveContent TEST_JSON_FILE (File.getContent "test/map-start.json")) +(let [jsonMap (new JsonMap> TEST_JSON_FILE (new JsonableArray> [] (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) \ No newline at end of file diff --git a/src/kiss_tools/Main.hx b/src/test/TestJsonable.hx similarity index 56% rename from src/kiss_tools/Main.hx rename to src/test/TestJsonable.hx index a56a843..bdf51b3 100644 --- a/src/kiss_tools/Main.hx +++ b/src/test/TestJsonable.hx @@ -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 {} diff --git a/src/test/TestJsonable.kiss b/src/test/TestJsonable.kiss new file mode 100644 index 0000000..a921707 --- /dev/null +++ b/src/test/TestJsonable.kiss @@ -0,0 +1,5 @@ +(defNew [&prop :String s]) + +(method parse [:String s] (new TestJsonable s)) +(method stringify [] s) +(method toString [] s) \ No newline at end of file diff --git a/test/map-end.json b/test/map-end.json new file mode 100644 index 0000000..940df6c --- /dev/null +++ b/test/map-end.json @@ -0,0 +1,5 @@ +{ + "c": "[]", + "a": "[\"abc\",\"def\",\"mno\"]", + "b": "[\"ghi\",\"jkl\"]" +} \ No newline at end of file diff --git a/test/map-start.json b/test/map-start.json new file mode 100644 index 0000000..c16cd2b --- /dev/null +++ b/test/map-start.json @@ -0,0 +1,4 @@ +{ + "a": "[\"abc\", \"def\"]", + "b": "[\"ghi\", \"jkl\"]" +} \ No newline at end of file