Skip to content

Commit

Permalink
allow external functions to optionally act on evaluation result.
Browse files Browse the repository at this point in the history
Signed-off-by: Pushpalanka Jayawardhana <pushpalanka.jayawardhana@zalando.de>
  • Loading branch information
Pushpalanka authored and ashutosh-narkar committed Feb 9, 2024
1 parent 0d06ee7 commit 6cf8de2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
15 changes: 11 additions & 4 deletions envoyauth/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ type StopFunc = func()
type TransactionCloser func(ctx context.Context, err error) error

// NewEvalResult creates a new EvalResult and a StopFunc that is used to stop the timer for metrics
func NewEvalResult() (*EvalResult, StopFunc, error) {
func NewEvalResult(opts ...func(*EvalResult)) (*EvalResult, StopFunc, error) {
var err error

er := EvalResult{
er := &EvalResult{
Metrics: metrics.New(),
}
er.DecisionID, err = util.UUID4()

for _, opt := range opts {
opt(er)
}

if er.DecisionID == "" {
er.DecisionID, err = util.UUID4()
}

if err != nil {
return nil, nil, err
Expand All @@ -53,7 +60,7 @@ func NewEvalResult() (*EvalResult, StopFunc, error) {
_ = er.Metrics.Timer(metrics.ServerHandler).Stop()
}

return &er, stop, nil
return er, stop, nil
}

// GetTxn creates a read transaction suitable for the configured EvalResult object
Expand Down
20 changes: 20 additions & 0 deletions envoyauth/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,23 @@ func TestGetDynamicMetadataWithBooleanDecision(t *testing.T) {
t.Fatalf("Expected no result but got %v", result)
}
}

func TestNewEvalResultWithDecisionID(t *testing.T) {
type Opt func(*EvalResult)

withDecisionID := func(decisionID string) Opt {
return func(result *EvalResult) {
result.DecisionID = decisionID
}
}

expectedDecisionID := "some-decision-id"

er, _, err := NewEvalResult(withDecisionID(expectedDecisionID))
if err != nil {
t.Fatalf("NewEvalResult() error = %v, wantErr %v", err, false)
}
if er.DecisionID != expectedDecisionID {
t.Errorf("Expected DecisionID to be '%v', got '%v'", expectedDecisionID, er.DecisionID)
}
}

0 comments on commit 6cf8de2

Please sign in to comment.