From 87e114d76d77a78423ba8ca676986cfe92f60cc8 Mon Sep 17 00:00:00 2001 From: chaos Date: Thu, 7 Nov 2024 13:10:18 +0000 Subject: [PATCH] update --- tool/tests.jq | 12 ++++++++++-- tool/typeLib.jq | 28 +++++++++++++++++++++++++++- tool/types.jq | 26 +++++++++++++++++++++----- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/tool/tests.jq b/tool/tests.jq index e351d5c..e073081 100644 --- a/tool/tests.jq +++ b/tool/tests.jq @@ -1,4 +1,4 @@ -import "./testdata/tests_export" as $exportData; +import "./testdata/tests_export" as $exportDataArray; include "journalUtils"; include "testLib"; import "typeLib" as typeLib; @@ -7,10 +7,18 @@ def journalUtilsTests: expectPassed(runTest( "invalid input to experienceByTitle"; ( - {} | experienceByTitle("Test") + null | experienceByTitle("Test") ); . == "experienceByTitle takes a array of experiences as input"; true + )) | + expectPassed(runTest( + "experience not found"; + ( + $exportDataArray[0].experiences | experienceByTitle("Test") + ); + . == null; + false )); def testsMain: diff --git a/tool/typeLib.jq b/tool/typeLib.jq index dd5e80a..ad4163d 100644 --- a/tool/typeLib.jq +++ b/tool/typeLib.jq @@ -10,13 +10,31 @@ def typecheckingDebug: true else false end; +def dumpType: + if (. | type) == "object" then + . | [to_entries | sort_by(.key)[] | {"\(.key)": (.value|dumpType)}] | add + elif (. | type) == "array" then + [.[] | dumpType] as $array | + if ($array | length > 0) then + if ($array | all(select(. == $array[0]))) then + [$array[0]] + else + $array + end + else + "array:empty/unknown" + end + else + . | type + end; + def typeErrorText: . as $type | "Type Error Checking '\($type)'"; def typeErrorText($type): $type | typeErrorText; def typeError($type): - if typecheckingDebug then debug({$type, value: .}) end | + if typecheckingDebug then debug({typeName: $type, value: ., valueType: . | dumpType}) end | error($type | typeErrorText); def ensureNull: if typecheckingEnabled and (. | type != "null") then typeError("null") end; @@ -39,6 +57,14 @@ def ensureNullOr(ensureType): def ensureKey($type; $key): if (. | has($key) | not) then typeError("\($type):\($key)") end; def ensureKey($value; $type; $key): $value | ensureKey($type; $key); +def ensureWrapError($newType; ensureType): + . as $value | + try (. | ensureType) catch (error(typeErrorText($newType))); + +def ensureWrapError($value; $newType; ensureType): + $value | ensureWrapError($newType; ensureType); + + # TYPECHECKING=1 required for tests def typeLibTests: expectPassed(runTest( diff --git a/tool/types.jq b/tool/types.jq index 6025b95..680f223 100644 --- a/tool/types.jq +++ b/tool/types.jq @@ -1,17 +1,33 @@ import "typeLib" as typeLib; +#["DEBUG:",{"creationDate":"number","ingestions":[{"administrationRoute":"string","consumerName":"null","creationDate":"number","customUnitId":"null","dose":"number","estimatedDoseStandardDeviation":"null","isDoseAnEstimate":"boolean","notes":"string","stomachFullness":"null","substanceName":"string","time":"number","units":"string"}],"isFavorite":"boolean","location":"null","ratings":"array:empty/unknown","sortDate":"number","text":"string","timedNotes":"array:empty/unknown","title":"string"}] + def ensureExperience: . as $experience | typeLib::ensureObject | - + + debug(. | typeLib::dumpType) | + $experience | typeLib::ensureKey("experience"; "title") | - .title | typeLib::ensureString | + .title | typeLib::ensureWrapError("experience:title"; typeLib::ensureString) | $experience | typeLib::ensureKey("experience"; "text") | - .text | typeLib::ensureString - + .text | typeLib::ensureWrapError("experience:text"; typeLib::ensureString) | + + $experience | + typeLib::ensureKey("experience"; "creationDate") | + .creationDate | typeLib::ensureWrapError("experience:creationDate"; typeLib::ensureNumber) | + + $experience | + typeLib::ensureKey("experience"; "sortDate") | + .sortDate | typeLib::ensureWrapError("experience:sortDate"; typeLib::ensureNumber) | + + $experience | + typeLib::ensureKey("experience"; "isFavorite") | + .isFavorite | typeLib::ensureWrapError("experience:isFavorite"; typeLib::ensureBool) + ; def ensureExportData: @@ -22,5 +38,5 @@ def ensureExportData: typeLib::ensureKey("exportData"; "customUnits") | typeLib::ensureKey("exportData"; "experiences") | typeLib::ensureKey("exportData"; "substanceCompanions") | - (.experiences[] | ensureExperience) + (reduce .experiences[] as $experience (null; $experience | ensureExperience)) end; \ No newline at end of file