Skip to content

Commit

Permalink
lib: relax type hints on flatten function
Browse files Browse the repository at this point in the history
The documented type signature of the flatten function is <list<dyn>> ->
<list<dyn>>, but the implementation was <list<typeV>> -> <list<typeV>>.
This has an impact on uses of flatten where the input is a nested set of
lists, the exact intended use-case. In this situation, the type checker
is told that the returned value is a list of lists..., resulting in
rejection of valid programs. There is a work-around that can be used,
but instead, just make the implementation match the documentation and
intention.
  • Loading branch information
efd6 committed Jul 19, 2024
1 parent eba27f3 commit f538a8d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ func (collectionsLib) CompileOptions() []cel.EnvOption {
cel.Function("flatten",
cel.MemberOverload(
"list_flatten",
[]*cel.Type{listV},
listV,
[]*cel.Type{listDyn},
listDyn,
cel.UnaryBinding(catch(flatten)),
),
),
Expand Down
87 changes: 87 additions & 0 deletions testdata/flatten_types.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
mito -use collections -data state.json src.cel
! stderr .
cmp stdout want.txt

-- state.json --
{
"aye": {
"value": 1
},
"bee": {
"value": "two"
},
"one": [
{
"value": 1
},
{
"value": "two"
}
],
"two": [
{
"value": 3
},
{
"value": "four"
}
]
}

-- src.cel --
[
"one",
"two",
].map(k, state[k].map(e, {
k: e,
})).flatten().map(d, d.with({
"aye": state.aye,
"bee": state.bee,
}))
-- want.txt --
[
{
"aye": {
"value": 1
},
"bee": {
"value": "two"
},
"one": {
"value": 1
}
},
{
"aye": {
"value": 1
},
"bee": {
"value": "two"
},
"one": {
"value": "two"
}
},
{
"aye": {
"value": 1
},
"bee": {
"value": "two"
},
"two": {
"value": 3
}
},
{
"aye": {
"value": 1
},
"bee": {
"value": "two"
},
"two": {
"value": "four"
}
}
]

0 comments on commit f538a8d

Please sign in to comment.