Skip to content

parse context.Background() wrongly to emptyCtx instead of *emptyCtx #397

@StephenWuyun

Description

@StephenWuyun
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
}

image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions