journal/tool/lib/typeLib.jq

89 lines
2.7 KiB
Plaintext
Raw Normal View History

2024-11-07 21:45:22 +00:00
import "testLib" as testLib;
2024-11-07 11:39:39 +00:00
def typecheckingEnabled:
2024-11-07 21:45:22 +00:00
if $ENV["JQ_TYPECHECKING"] != null and (["true", "1", "debug"] | any(index($ENV["JQ_TYPECHECKING"]))) then
2024-11-07 11:39:39 +00:00
true
else false end;
def typecheckingDebug:
2024-11-07 21:45:22 +00:00
if $ENV["JQ_TYPECHECKING"] == "debug" then
2024-11-07 11:39:39 +00:00
true
else false end;
2024-11-07 13:10:18 +00:00
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;
2024-11-07 11:39:39 +00:00
def typeErrorText:
. as $type |
"Type Error Checking '\($type)'";
def typeErrorText($type): $type | typeErrorText;
def typeError($type):
2024-11-07 13:10:18 +00:00
if typecheckingDebug then debug({typeName: $type, value: ., valueType: . | dumpType}) end |
2024-11-07 11:39:39 +00:00
error($type | typeErrorText);
def ensureNull: if typecheckingEnabled and (. | type != "null") then typeError("null") end;
def ensureBool: if typecheckingEnabled and (. | type) != "boolean" then typeError("bool") end;
def ensureString: if typecheckingEnabled and (. | type) != "string" then typeError("string") end;
def ensureArray: if typecheckingEnabled and (. | type) != "array" then typeError("array") end;
def ensureObject: if typecheckingEnabled and (. | type) != "object" then typeError("object") end;
def ensureNumber: if typecheckingEnabled and (. | type) != "number" then typeError("number") end;
def ensureNull($value): $value | ensureNull;
def ensureBool($value): $value | ensureBool;
def ensureString($value): $value | ensureString;
def ensureArray($value): $value | ensureArray;
def ensureObject($value): $value | ensureObject;
def ensureNumber($value): $value | ensureNumber;
def ensureNullOr(ensureType):
if . == null then . else . | ensureType end;
def ensureKey($type; $key): if (. | has($key) | not) then typeError("\($type):\($key)") end;
def ensureKey($value; $type; $key): $value | ensureKey($type; $key);
2024-11-09 00:49:08 +00:00
def ensureIfKey($key; ensureType): if (. | has($key)) then .[$key] | ensureType end;
2024-11-07 13:10:18 +00:00
def ensureWrapError($newType; ensureType):
. as $value |
2024-11-07 21:45:22 +00:00
try (. | ensureType)
catch (
. as $prevError |
if typecheckingDebug then debug({$newType, $prevError}) end |
error(typeErrorText($newType))
);
2024-11-07 13:10:18 +00:00
def ensureWrapError($value; $newType; ensureType):
$value | ensureWrapError($newType; ensureType);
2024-11-07 21:45:22 +00:00
# JQ_TYPECHECKING=true required for tests
2024-11-07 11:39:39 +00:00
def typeLibTests:
2024-11-07 21:45:22 +00:00
testLib::expectPassed(testLib::runTest(
2024-11-07 11:39:39 +00:00
"null is null";
(null | ensureNull);
true;
false
)) |
2024-11-07 21:45:22 +00:00
testLib::expectPassed(testLib::runTest(
2024-11-07 11:39:39 +00:00
"boolean is not null";
(true | ensureNull);
(. == typeErrorText("null"));
true
));