Skip to content

Commit

Permalink
vam: Add typeof() function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnibs committed Sep 23, 2024
1 parent c1da204 commit fe442b0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 69 deletions.
2 changes: 2 additions & 0 deletions runtime/vam/expr/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func New(zctx *zed.Context, name string, narg int) (expr.Function, field.Path, e
f = &Split{zctx}
case "trim":
f = &Trim{zctx}
case "typeof":
f = &TypeOf{zctx}
case "upper":
f = &ToUpper{zctx}
default:
Expand Down
16 changes: 16 additions & 0 deletions runtime/vam/expr/function/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package function

import (
"github.com/brimdata/zed"
"github.com/brimdata/zed/vector"
)

// https://github.com/brimdata/zed/blob/main/docs/language/functions.md#typeof
type TypeOf struct {
zctx *zed.Context
}

func (t *TypeOf) Call(args ...vector.Any) vector.Any {
v := t.zctx.LookupTypeValue(args[0].Type())
return vector.NewConst(v, args[0].Len(), nil)
}
68 changes: 68 additions & 0 deletions runtime/ztests/expr/function/typeof.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
zed: yield typeof(this)

vector: true

input: |
{
info: "Connection Example",
src: {
addr: 10.1.1.2,
port: 80 (uint16)
} (=socket),
dst: {
addr: 10.0.1.2,
port: 20130
} (socket)
} (=conn)
{
info: "Connection Example 2",
src: {
addr: 10.1.1.8,
port: 80 (uint16)
} (=socket),
dst: {
addr: 10.1.2.88,
port: 19801
} (socket)
} (=conn)
null(conn)
{
info: "Access List Example",
nets: [
10.1.1.0/24,
10.1.2.0/24
]
} (=access_list)
{
metric: "A",
ts: 2020-11-24T16:44:09.586441Z,
value: 120
}
{
metric: "B",
ts: 2020-11-24T16:44:20.726057Z,
value: 0.86
}
{
metric: "A",
ts: 2020-11-24T16:44:32.201458Z,
value: 126
}
{
metric: "C",
ts: 2020-11-24T16:44:43.547506Z,
value: {
x: 10,
y: 101
}
}
output: |
<conn={info:string,src:socket={addr:ip,port:uint16},dst:socket}>
<conn={info:string,src:socket={addr:ip,port:uint16},dst:socket}>
<conn={info:string,src:socket={addr:ip,port:uint16},dst:socket}>
<access_list={info:string,nets:[net]}>
<{metric:string,ts:time,value:int64}>
<{metric:string,ts:time,value:float64}>
<{metric:string,ts:time,value:int64}>
<{metric:string,ts:time,value:{x:int64,y:int64}}>
69 changes: 0 additions & 69 deletions zson/ztests/typeof.yaml

This file was deleted.

0 comments on commit fe442b0

Please sign in to comment.