从1.5版本开始,jq数据处理语言有一个库模块系统。模块由可选元数据和一组函数组成。例如
module { name: "util", version: "1.0.0" };
def digitsum: tostring|split("")|map(tonumber)|add;
存储为文件util.jq
可以像这样使用:
$ echo '789' | jq -L. 'include "util"; digitsum'
24
模块可以使用其他模块,并且依赖关系由 modulemeta
指令跟踪,但如何表达和检查模块编号的最低版本?例如:
module {
name: "math",
version: "0.1.0",
};
include "util"; # TODO: require at least version 1.0.0!
def digitroot:
(.|digitsum) as $sum |
if $sum<10 then $sum else $sum|digitroot end;
jq 中对模块的支持目前(2019 年 6 月(仍然非常少,尽管在 github 上有一个 jq 的模块管理系统: https://github.com/joelpurra/jqnpm
不使用这样的外部模块管理系统,jq本身可以做什么? 扩展给定的示例,下面说明了支持版本要求的一种方法。请注意math
模块元数据中名为 dependencies
的附加键。(目前,此键不能deps
命名,因为 jq 会覆盖它。
文件
依赖.jq
# Recursively check specified version constraints
module { name: "dependencies", version: "0.0.2" };
# parents of a module as defined by its .deps
def parents:
. as $in
| if type == "array" then map(parents) | add
else modulemeta | .deps | map(.relpath)
end ;
# ancestors of a single module or an array of modules.
# The array of "ancestors" of a module includes itself.
def ancestors:
# input and $visited should be arrays of strings
def ancestors($visited):
. as $in
| ($in - $visited) as $new
| if $new == [] then $visited
else $new | parents | ancestors($visited + $new | unique)
end;
if type == "array" then . else [.] end
| ancestors([]) ;
def versionsort:
def parse:
sub("(?<a>(alpha|beta|gamma))"; "(.a).")
| [splits("[-.]")]
| map(tonumber? // .) ;
sort_by(parse);
# Input: a module name
# Emit empty if the constraints for the given module are satisfied, otherwise raise an error
def dependencies($version):
def le($y): (. == $y) or ([.,$y] | . == versionsort);
modulemeta
| .version as $mv
| if (($mv == null) or ($version | le($mv))) then empty
else ("module (.name) dependencies version ($version) vs ($mv)" | error)
end ;
# Input: a module name or array of module names
# Check the module-version dependencies in .dependencies, proceeding up the chain as defined by .deps
def dependencies:
def check:
modulemeta
| select(has("dependencies"))
| all( .dependencies | to_entries[];
.key as $m | .value as $v | ($m | dependencies($v) ))
| empty;
ancestors[] | check;
util.jq
module { name: "util", version: "1.0.0" };
def digitsum: tostring|split("")|map(tonumber)|add;
math.jq
module {
name: "math",
version: "0.1.0",
dependencies: {"util": "1.0.0"} };
include "util" ;
def digitroot:
digitsum as $sum
| if $sum<10 then $sum
else $sum|digitroot
end;
调用
jq -n -L . '
include "dependencies";
include "math";
"math" | dependencies,
(123|digitroot) '