Skip to content

Commit

Permalink
vam: unflatten() (#5635)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnibs authored Feb 7, 2025
1 parent 8e86ac0 commit 90c1f36
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
22 changes: 22 additions & 0 deletions runtime/vam/expr/function/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,25 @@ func (f *flatten) Call(args ...vector.Any) vector.Any {
}
return builder.Build()
}

type unflatten struct {
fn *samfunc.Unflatten
}

func newUnflatten(zctx *super.Context) *unflatten {
return &unflatten{samfunc.NewUnflatten(zctx)}
}

func (u *unflatten) Call(args ...vector.Any) vector.Any {
vec := vector.Under(args[0])
typ := vec.Type()
builder := vector.NewDynamicBuilder()
var b zcode.Builder
for i := range vec.Len() {
b.Truncate()
vec.Serialize(&b, i)
val := u.fn.Call(nil, []super.Value{super.NewValue(typ, b.Bytes().Body())})
builder.Write(val)
}
return builder.Build()
}
6 changes: 4 additions & 2 deletions runtime/vam/expr/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ func New(zctx *super.Context, name string, narg int) (expr.Function, field.Path,
f = &TypeName{zctx: zctx}
case "typeof":
f = &TypeOf{zctx}
case "upper":
f = &ToUpper{zctx}
case "under":
f = &Under{zctx}
case "unflatten":
f = newUnflatten(zctx)
case "upper":
f = &ToUpper{zctx}
default:
return nil, nil, function.ErrNoSuchFunction
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
zed: "yield unflatten(this)"
zed: yield unflatten(this)

vector: true

input: |
[{key:["a","a"],value:1},{key:["a","b"],value:2},{key:["a","x","z"],value:"foo"},{key:["b"],value:2},{key:["c"],value:3}]
Expand Down

0 comments on commit 90c1f36

Please sign in to comment.