This commit is contained in:
chaos 2024-11-10 13:00:43 +00:00
parent aa714f405f
commit cd06938720
8 changed files with 250 additions and 14 deletions

8
run.sh
View file

@ -7,11 +7,11 @@ TOOL_DIR="$(cd -- "$(dirname -- "$0")" && pwd)/tool"
cd "${WORKING_DIRECTORY}" cd "${WORKING_DIRECTORY}"
JQ=${JQ:-jq} JQ=${JQ:-jq}
export JQ_FLAVOR=${JQ_FLAVOR:-"$(basename "${JQ}")"} export JQ_FLAVOUR=${JQ_FLAVOUR:-"$(basename "${JQ}")"}
run() { run() {
if [ -d "${TOOL_DIR}/lib/stubs/${JQ_FLAVOR}" ]; then if [ -d "${TOOL_DIR}/lib/stubs/${JQ_FLAVOUR}" ]; then
STUBS_DIR="${TOOL_DIR}/lib/stubs/${JQ_FLAVOR}" STUBS_DIR="${TOOL_DIR}/lib/stubs/${JQ_FLAVOUR}"
else else
STUBS_DIR="${TOOL_DIR}/lib/stubs/jq" STUBS_DIR="${TOOL_DIR}/lib/stubs/jq"
fi fi
@ -19,7 +19,7 @@ run() {
FILES_ARGS=() FILES_ARGS=()
if [ "${EXPORT_FILE:-}" != "" ] && [ -f "${EXPORT_FILE}" ]; then if [ "${EXPORT_FILE:-}" != "" ] && [ -f "${EXPORT_FILE}" ]; then
FILES_ARGS+=(--arg exportFileName "${EXPORT_FILE:-}") FILES_ARGS+=(--arg exportFileName "${EXPORT_FILE:-}")
if [ "${JQ_FLAVOR}" != "gojq-extended" ]; then if [ "${JQ_FLAVOUR}" != "gojq-extended" ]; then
FILES_ARGS+=(--slurpfile exportFile "${EXPORT_FILE}") FILES_ARGS+=(--slurpfile exportFile "${EXPORT_FILE}")
fi fi
else else

View file

@ -4,18 +4,18 @@ SCRIPT_DIR="$(cd -- "$(dirname -- "$0")" && pwd)"
cd "${SCRIPT_DIR}/tool" || return cd "${SCRIPT_DIR}/tool" || return
JQ=${JQ:-jq} JQ=${JQ:-jq}
export JQ_FLAVOR=${JQ_FLAVOR:-"$(basename "${JQ}")"} export JQ_FLAVOUR=${JQ_FLAVOUR:-"$(basename "${JQ}")"}
export JQ_TYPECHECKING=1 export JQ_TYPECHECKING=1
if [ ! -d "./lib/stubs/${JQ_FLAVOR}" ]; then if [ ! -d "./lib/stubs/${JQ_FLAVOUR}" ]; then
STUBS_DIR="./lib/stubs/${JQ_FLAVOR}" STUBS_DIR="./lib/stubs/${JQ_FLAVOUR}"
else else
STUBS_DIR="./lib/stubs/jq" STUBS_DIR="./lib/stubs/jq"
fi fi
runTests() { runTests() {
echo "Running Tests with JQ=${JQ}" echo "Running Tests with JQ=${JQ}"
export JQ_FLAVOR=${JQ_FLAVOR:-"$(basename "${JQ}")"} export JQ_FLAVOUR=${JQ_FLAVOUR:-"$(basename "${JQ}")"}
${JQ} -nr \ ${JQ} -nr \
-L "$(realpath .)" -L "$(realpath ./lib)" -L "$(realpath "${STUBS_DIR}")" \ -L "$(realpath .)" -L "$(realpath ./lib)" -L "$(realpath "${STUBS_DIR}")" \
"include \"tests\"; testsMain" "include \"tests\"; testsMain"
@ -24,5 +24,5 @@ runTests() {
runTests runTests
if command -v gojq 2>/dev/null >/dev/null; then if command -v gojq 2>/dev/null >/dev/null; then
JQ=gojq JQ_FLAVOR=gojq runTests JQ=gojq JQ_FLAVOUR=gojq runTests
fi fi

110
tool/lib/JSONLib.jq Normal file
View file

@ -0,0 +1,110 @@
import "ansiLib" as ansiLib;
def colorEscapes:
def parseJQColours:
(. | split(":")) |
{
"null": .[0],
"false": .[1],
"true": .[2],
"number": .[3],
"string": .[4],
"array": .[5],
"object": .[6],
"objectKey": .[7],
};
def parseGOJQColours:
(. | split(":")) |
{
"null": .[0],
"false": .[1],
"true": .[2],
"number": .[3],
"string": .[4],
"objectKey": .[5],
"array": .[6],
"object": .[7],
};
def defaultColours:
if ($ENV["JQ_FLAVOUR"] | tostring | startswith("gojq")) then
"90:33:33:36:32:34;1" | parseGOJQColours
else
"0;90:0;39:0;39:0;39:0;32:1;39:1;39:1;34" | parseJQColours
end;
try (
if $ENV["JQ_COLORS"] != null then
$ENV["JQ_COLORS"] | parseJQColours
elif $ENV["GOJQ_COLORS"] != null then
$ENV["GOJQ_COLORS"] | parseGOJQColours
else
defaultColours
end
) catch (
defaultColours
);
def encodeJSONValue($withColour; $escapes; $indent; $currentDepth):
def colourText($kind; $escapes):
. as $text |
ansiLib::CSR + $escapes[$kind] + "m" + $text + ansiLib::CSR + "0m";
def maybeColourText($withColour; $kind; $escapes): if $withColour then colourText($kind; $escapes) end;
def maybeIndent($indent; $currentDepth):
if $indent > 0 then [range($indent * $currentDepth) | " "] | join("") else "" end;
def maybeNewline($indent):
if $indent > 0 then "\n" else "" end;
def maybeSpace($indent):
if $indent > 0 then " " else "" end;
. as $value |
(. | type) as $valueType |
if $valueType == "null" then
"null" | maybeColourText($withColour; "null"; $escapes)
elif $valueType == "boolean" then
if $value then
"true" | maybeColourText($withColour; "true"; $escapes)
else
"false" | maybeColourText($withColour; "false"; $escapes)
end
elif $valueType == "number" then
$value | tostring | maybeColourText($withColour; "number"; $escapes)
elif $valueType == "string" then
$value | tojson | maybeColourText($withColour; "string"; $escapes)
elif $valueType == "array" then
($value | length) as $numValues |
("[" | maybeColourText($withColour; "array"; $escapes)) +
if $numValues > 0 then maybeNewline($indent) else "" end +
([
$value[] |
maybeIndent($indent; $currentDepth) +
encodeJSONValue($withColour; $escapes; $indent; $currentDepth + 1)
] | join("," + maybeNewline($indent))) +
if $numValues > 0 then maybeNewline($indent) else "" end +
if $numValues > 0 then maybeIndent($indent; $currentDepth-1) else "" end +
("]" | maybeColourText($withColour; "array"; $escapes))
elif $valueType == "object" then
($value | keys | length) as $numKeys |
("{" | maybeColourText($withColour; "object"; $escapes)) +
if $numKeys > 0 then maybeNewline($indent) else "" end +
([
$value | keys | sort[] |
. as $objectKey |
$value[$objectKey] as $objectValue |
maybeIndent($indent; $currentDepth) +
($objectKey | tojson | maybeColourText($withColour; "objectKey"; $escapes)) +
":" + maybeSpace($indent) +
($objectValue | encodeJSONValue($withColour; $escapes; $indent; $currentDepth + 1))
] | join("," + maybeNewline($indent))) +
if $numKeys > 0 then maybeNewline($indent) else "" end +
if $numKeys > 0 then maybeIndent($indent; $currentDepth-1) else "" end +
("}" | maybeColourText($withColour; "object"; $escapes))
end;
def encodeJSON($withColour; $indent):
. | encodeJSONValue($withColour; colorEscapes; $indent // 0; 1);

24
tool/lib/ansiLib.jq Normal file
View file

@ -0,0 +1,24 @@
def supportsColour:
$ENV["JQ_SUPPORTS_COLOR"] == "1" or $ENV["JQ_SUPPORTS_COLOUR"] == "1" ;
def CSR:
"\u001b[";
def resetColours:
CSR + "0m";
def colourText($colourCode):
. as $text |
if supportsColour | not then
$text
else
CSR + ($colourCode | tostring) + "m" + $text + resetColours
end;
def colourText($colourCode; $mode):
. as $text |
if supportsColour | not then
$text
else
CSR + ($mode | tostring) + ";" + ($colourCode | tostring) + "m" + $text + resetColours
end;

View file

@ -1,7 +1,7 @@
include "stubs"; include "stubs";
def checkSupported: def checkSupported:
$ENV.JQ_FLAVOR == "gojq-extended"; $ENV.JQ_FLAVOUR == "gojq-extended";
def readFile($filename): _readFile($filename); def readFile($filename): _readFile($filename);
def writeFileString($filename): _writeFileString($filename); def writeFileString($filename): _writeFileString($filename);

View file

@ -3,7 +3,7 @@ import "typeLib" as typeLib;
def ensureAdministrationRoute: def ensureAdministrationRoute:
. as $administrationRoute | . as $administrationRoute |
if typeLib::typecheckingEnabled then if typeLib::typecheckingEnabled then
[ if [
"ORAL", "ORAL",
"SUBLINGUAL", "SUBLINGUAL",
"BUCCAL", "BUCCAL",
@ -15,7 +15,9 @@ def ensureAdministrationRoute:
"INTRAVENOUS", "INTRAVENOUS",
"SMOKED", "SMOKED",
"INHALED" "INHALED"
] | any(index($administrationRoute)) ] | any(index($administrationRoute)) | not then
typeLib::typeErrorText("administrationRoute")
end
end; end;
def ensureIngestion: def ensureIngestion:

99
tool/lib/logLib.jq Normal file
View file

@ -0,0 +1,99 @@
import "typeLib" as typeLib;
import "ansiLib" as ansiLib;
import "JSONLib" as JSONLib;
def logLevels:
[
"trace",
"debug",
"info",
"warn",
"error",
"fatal"
];
def isValidLogLevel($logLevel):
logLevels | any(index($logLevel));
def ensureLogLevel:
. as $logLevel |
if typeLib::typecheckingEnabled then
if isValidLogLevel($logLevel) | not then typeLib::typeErrorText("logLevel") end
end;
def maxLogLevel:
($ENV["JQ_LOGLEVEL"] // "info") as $maxLogLevel |
if isValidLogLevel($maxLogLevel) | not then
"info"
else $maxLogLevel end;
def shouldLog($logLevel):
maxLogLevel as $maxLogLevel |
logLevels as $logLevels |
reduce range($logLevels | length) as $logLevelIndex ({};
.[$logLevels[$logLevelIndex]] = $logLevelIndex
) as $logLevelIndexes |
$logLevelIndexes[$maxLogLevel] <= $logLevelIndexes[$logLevel];
def ansiColourByName:
. as $name |
{
"red": "31",
"yellow": "33",
"blue": "36",
"gray": "37"
}[$name];
def levelColour:
. as $logLevel |
{
"trace": "yellow",
"debug": "gray",
"info": "blue",
"warn": "yellow",
"error": "red",
"fatal": "red"
}[$logLevel] | ansiColourByName;
def log($logLevel; $msg; $data):
. as $input |
($logLevel | ensureLogLevel) |
($msg | typeLib::ensureNullOr(typeLib::ensureString)) |
if shouldLog($logLevel) then
($logLevel | levelColour) as $logLevelColour |
($logLevel | ascii_upcase | ansiLib::colourText($logLevelColour)) as $logLevelText |
($msg | ansiLib::colourText("gray" | ansiColourByName)) as $msgText |
if $data != null then
$data | JSONLib::encodeJSON(true; 2)
else "" end | . as $dataText |
"\($logLevelText) \($msgText) \($dataText)\n" |
if $logLevel == "fatal" then halt_error(1) else stderr end
end |
$input;
def trace($msg; $data): log("trace"; $msg; $data);
def debug($msg; $data): log("debug"; $msg; $data);
def info($msg; $data): log("info"; $msg; $data);
def warn($msg; $data): log("warn"; $msg; $data);
def error($msg; $data): log("error"; $msg; $data);
def fatal($msg; $data): log("fatal"; $msg; $data);
def trace($msg): trace($msg; null);
def debug($msg): debug($msg; null);
def info($msg): info($msg; null);
def warn($msg): warn($msg; null);
def error($msg): error($msg; null);
def fatal($msg): fatal($msg; null);
def logTest:
([
"trace",
"debug",
"info",
"warn",
"error"
][] | log(.; "example text \(.)"; {})) | empty;

View file

@ -1,8 +1,8 @@
import "testLib" as testLib; import "testLib" as testLib;
def typecheckingEnabled: def typecheckingEnabled:
if $ENV["JQ_TYPECHECKING"] != null and (["true", "1", "debug"] | any(index($ENV["JQ_TYPECHECKING"]))) then if $ENV["JQ_TYPECHECKING"] != null then
true ["true", "1", "debug"] | any(index($ENV["JQ_TYPECHECKING"]))
else false end; else false end;
def typecheckingDebug: def typecheckingDebug:
@ -31,6 +31,7 @@ def dumpType:
def typeErrorText: def typeErrorText:
. as $type | . as $type |
"Type Error Checking '\($type)'"; "Type Error Checking '\($type)'";
def typeErrorText($type): $type | typeErrorText; def typeErrorText($type): $type | typeErrorText;
def typeError($type): def typeError($type):