-
-
Notifications
You must be signed in to change notification settings - Fork 452
Closed
Description
func TestRun(t *testing.T) {
input := `ip(ctx) == "123"`
program, err := expr.Compile(input, expr.Function("ip", ip, new(func(context.Context) string)))
if err != nil {
t.Fatal(err)
}
t.Log(expr.Run(program, map[string]interface{}{
"ctx": context.Background(),
}))
t.Log(expr.Run(program, map[string]interface{}{
"ctx": context.WithValue(context.Background(), "ip", "123"),
}))
}
func ip(params ...any) (any, error) {
if len(params) != 1 {
return nil, fmt.Errorf("ip: expected 1 parameter, got %d", len(params))
}
fmt.Println(reflect.TypeOf(params[0]))
ctx, ok := params[0].(context.Context)
if !ok {
return nil, fmt.Errorf("ip: expected context as first parameter")
}
_ = ctx
//ret, ok := metadata.GetClientIpv4(ctx)
//if !ok {
// return nil, fmt.Errorf("ip: failed to get ip from context")
//}
//
//return ret.String(), nil
return "123", nil
}