在创建一个代表其实际级别的结构时,我面临一个挑战。当我使用递归功能时,我会将所有子级都置于一个级别之下。
{
"id": "foo-structure",
"type": "pipe",
"source": {
"type": "dataset",
"dataset": "parent-children-relations"
},
"transform": {
"type": "dtl",
"rules": {
"default": [
[
"copy",
"*"
],
[
"add",
"children",
[
"apply-hops",
"foo",
{
"datasets": [
"parent-children-relations pcr"
],
"where": [
[
"eq",
"_S.foo",
"pcr.bar"
]
],
"recurse": true,
"max_depth": 10,
"exclude_root": true
}
]
]
],
"foo": [
[
"copy",
"*"
]
]
}
}
} 像这样:
"top-level": [ {"child1"}, {"child2"}, {"child1.2"}]
孩子1.2是孩子1的孩子。
所需结果如下:
"top-level": [{"child1":[ {"child1.2"}]}, {"child2"}]
我尝试了不使用递归的方法,只是使用新规则跳回到了相同的数据集,并且它可以工作,但是感觉好像有更好的解决方案。现在看起来像这样:
{
"_id": "foo-structure",
"type": "pipe",
"source": {
"type": "dataset",
"dataset": "parent-children-relations"
},
"transform": {
"type": "dtl",
"rules": {
"default": [
["copy", "*"],
["add", "children-members",
["apply-hops", "bar", {
"datasets": ["global-children-members gcm"],
"where": [
["eq", "_S.id", "gcm.id"]
]
}]
],
["add", "children",
["apply-hops", "foo", {
"datasets": ["parent-children-relations pcr"],
"where": [
["eq", "_S.foo", "pcr.bar"]
]
}]
]
],
"bar": [
["copy", "*"]
],
"child": [
["if",
["-is-not-null", "_S.foo"],
["add", "children",
["apply-hops", "child1", {
"datasets": ["parent-children-relations pcr"],
"where": [
["eq", "_S.foo", "pcr.bar""]
]
}]
]
],
["add", "children-member"
["apply-hops", "bar", {
"datasets": ["global-children-members gcm"],
"where": [
["eq", "_S.id", "gcm.id"]
]
}]
]
],
"child1": [
["if",
["-is-not-null", "_S.foo"],
["add", "children",
["apply-hops", "child2", {
"datasets": ["parent-children-relations pcr"],
"where": [
["eq", "_S.foo", "pcr.bar""]
]
}]
]
],
["add", "children-member"
["apply-hops", "bar", {
"datasets": ["global-children-members gcm"],
"where": [
["eq", "_S.id", "gcm.id"]
]
}]
]
],
"child2": [
["if",
["-is-not-null", "_S.foo"],
["add", "children",
["apply-hops", "child3", {
"datasets": ["parent-children-relations pcr"],
"where": [
["eq", "_S.foo", "pcr.bar""]
]
}]
]
],
["add", "children-member"
["apply-hops", "bar", {
"datasets": ["global-children-members gcm"],
"where": [
["eq", "_S.id", "gcm.id"]
]
}]
]
],
"child3": [
["if",
["-is-not-null", "_S.foo"],
["add", "children",
["apply-hops", "child4", {
"datasets": ["parent-children-relations pcr"],
"where": [
["eq", "_S.foo", "pcr.bar""]
]
}]
]
],
["add", "children-member"
["apply-hops", "bar", {
"datasets": ["global-children-members gcm"],
"where": [
["eq", "_S.id", "gcm.id"]
]
}]
]
],
"child4": [
["copy", "*"],
["if",
["-is-not-null", "_S.foo"],
["add", "children",
["apply-hops", "child5", {
"datasets": ["parent-children-relations pcr"],
"where": [
["eq", "_S.foo", "pcr.bar""]
]
}]
]
],
["add", "children-member"
["apply-hops", "bar", {
"datasets": ["global-children-members"],
"where": [
["eq", "_S.id", "gcm.id"]
]
}]
]
],
"child5": [
["if",
["-is-not-null", "_S.foo"],
["comment", "children",
["apply-hops", "child5", {
"datasets": ["parent-children-relations pcr"],
"where": [
["eq", "_S.foo", "pcr.bar""]
]
}]
]
],
["add", "children-member"
["apply-hops", "bar", {
"datasets": ["global-children-members gcm"],
"where": [
["eq", "_S.id", "gcm.id"]
]
}]
]
],
"foo": [
["add", "children-member"
["apply-hops", "bar", {
"datasets": ["global-children-members gcm"],
"where": [
["eq", "_S.id", "gcm.id"]
]
}]
],
["add", "children",
["dict", "children",
["apply-hops", "child", {
"datasets": ["parent-children-relations pcr"],
"where": [
["eq", "_S.foo", "pcr.bar""]
]
}]
]
]
]
}
} }
问题就变成了如何编写适用于层次结构中n级的递归函数。
谢谢!