Skip to content

Commit

Permalink
Change to ldvalue default type.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Mar 27, 2024
1 parent a54f2d5 commit 133d0e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
3 changes: 2 additions & 1 deletion internal/hooks/hook_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
"github.com/launchdarkly/go-sdk-common/v3/ldlog"
"github.com/launchdarkly/go-sdk-common/v3/ldreason"
"github.com/launchdarkly/go-sdk-common/v3/ldvalue"
"github.com/launchdarkly/go-server-sdk/v7/ldhooks"
)

Expand Down Expand Up @@ -68,7 +69,7 @@ func (h *HookRunner) getHooks() []ldhooks.Hook {
func (h *HookRunner) PrepareEvaluationSeries(
flagKey string,
evalContext ldcontext.Context,
defaultVal any,
defaultVal ldvalue.Value,
method string,
) EvaluationExecution {
hooksForEval := h.getHooks()
Expand Down
44 changes: 23 additions & 21 deletions internal/hooks/hook_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,29 @@ func newOrderTracker() *orderTracker {
}

func TestHookRunner(t *testing.T) {
falseValue := ldvalue.Bool(false)
ldContext := ldcontext.New("test-context")

t.Run("with no hooks", func(t *testing.T) {
runner := NewHookRunner(sharedtest.NewTestLoggers(), []ldhooks.Hook{})

t.Run("prepare evaluation series", func(t *testing.T) {
res := runner.PrepareEvaluationSeries("test-flag", ldContext, false, "testMethod")
res := runner.PrepareEvaluationSeries("test-flag", ldContext, falseValue, "testMethod")
emptyExecutionAssertions(t, res, ldContext)
})

t.Run("run before evaluation", func(t *testing.T) {
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, false,
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, falseValue,
"testMethod")
res := runner.BeforeEvaluation(context.Background(), execution)
emptyExecutionAssertions(t, res, ldContext)
})

t.Run("run after evaluation", func(t *testing.T) {
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, false,
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, falseValue,
"testMethod")
res := runner.AfterEvaluation(context.Background(), execution,
ldreason.NewEvaluationDetail(ldvalue.Bool(false), 0,
ldreason.NewEvaluationDetail(falseValue, 0,
ldreason.NewEvalReasonFallthrough()))
emptyExecutionAssertions(t, res, ldContext)
})
Expand All @@ -71,14 +73,14 @@ func TestHookRunner(t *testing.T) {
runner := NewHookRunner(sharedtest.NewTestLoggers(), []ldhooks.Hook{hookA, hookB})

ldContext := ldcontext.New("test-context")
res := runner.PrepareEvaluationSeries("test-flag", ldContext, false, "testMethod")
res := runner.PrepareEvaluationSeries("test-flag", ldContext, falseValue, "testMethod")

assert.Len(t, res.hooks, 2)
assert.Len(t, res.data, 2)
assert.Equal(t, ldContext, res.context.Context())
assert.Equal(t, "test-flag", res.context.FlagKey())
assert.Equal(t, "testMethod", res.context.Method())
assert.Equal(t, ldvalue.Bool(false), res.context.DefaultValue())
assert.Equal(t, falseValue, res.context.DefaultValue())
assert.Equal(t, res.data[0], ldhooks.EmptyEvaluationSeriesData())
assert.Equal(t, res.data[1], ldhooks.EmptyEvaluationSeriesData())
})
Expand All @@ -99,7 +101,7 @@ func TestHookRunner(t *testing.T) {
},
{name: "AfterEvaluation",
method: func(runner *HookRunner, execution EvaluationExecution) {
detail := ldreason.NewEvaluationDetail(ldvalue.Bool(false), 0,
detail := ldreason.NewEvaluationDetail(falseValue, 0,
ldreason.NewEvalReasonFallthrough())
_ = runner.AfterEvaluation(context.Background(), execution, detail)
},
Expand All @@ -115,7 +117,7 @@ func TestHookRunner(t *testing.T) {
hookB := createOrderTrackingHook("b", tracker)
runner := NewHookRunner(sharedtest.NewTestLoggers(), []ldhooks.Hook{hookA, hookB})

execution := runner.PrepareEvaluationSeries("test-flag", ldContext, false,
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, falseValue,
"testMethod")
testCase.method(runner, execution)

Expand All @@ -135,7 +137,7 @@ func TestHookRunner(t *testing.T) {
runner := NewHookRunner(sharedtest.NewTestLoggers(), []ldhooks.Hook{hookA})
runner.AddHooks(hookB)

execution := runner.PrepareEvaluationSeries("test-flag", ldContext, false,
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, falseValue,
"testMethod")
testCase.method(runner, execution)

Expand All @@ -152,15 +154,15 @@ func TestHookRunner(t *testing.T) {
hookB := sharedtest.NewTestHook("b")
runner := NewHookRunner(sharedtest.NewTestLoggers(), []ldhooks.Hook{hookA, hookB})

execution := runner.PrepareEvaluationSeries("test-flag", ldContext, false,
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, falseValue,
"testMethod")
_ = runner.BeforeEvaluation(context.Background(), execution)

hookA.Verify(t, sharedtest.HookExpectedCall{
HookStage: sharedtest.HookStageBeforeEvaluation,
EvalCapture: sharedtest.HookEvalCapture{
EvaluationSeriesContext: ldhooks.NewEvaluationSeriesContext("test-flag", ldContext,
false, "testMethod"),
falseValue, "testMethod"),
EvaluationSeriesData: ldhooks.EmptyEvaluationSeriesData(),
GoContext: context.Background(),
}})
Expand All @@ -169,7 +171,7 @@ func TestHookRunner(t *testing.T) {
HookStage: sharedtest.HookStageBeforeEvaluation,
EvalCapture: sharedtest.HookEvalCapture{
EvaluationSeriesContext: ldhooks.NewEvaluationSeriesContext("test-flag", ldContext,
false, "testMethod"),
falseValue, "testMethod"),
EvaluationSeriesData: ldhooks.EmptyEvaluationSeriesData(),
GoContext: context.Background(),
}})
Expand All @@ -180,17 +182,17 @@ func TestHookRunner(t *testing.T) {
hookB := sharedtest.NewTestHook("b")
runner := NewHookRunner(sharedtest.NewTestLoggers(), []ldhooks.Hook{hookA, hookB})

execution := runner.PrepareEvaluationSeries("test-flag", ldContext, false,
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, falseValue,
"testMethod")
detail := ldreason.NewEvaluationDetail(ldvalue.Bool(false), 0,
detail := ldreason.NewEvaluationDetail(falseValue, 0,
ldreason.NewEvalReasonFallthrough())
_ = runner.AfterEvaluation(context.Background(), execution, detail)

hookA.Verify(t, sharedtest.HookExpectedCall{
HookStage: sharedtest.HookStageAfterEvaluation,
EvalCapture: sharedtest.HookEvalCapture{
EvaluationSeriesContext: ldhooks.NewEvaluationSeriesContext("test-flag", ldContext,
false, "testMethod"),
falseValue, "testMethod"),
EvaluationSeriesData: ldhooks.EmptyEvaluationSeriesData(),
Detail: detail,
GoContext: context.Background(),
Expand All @@ -200,7 +202,7 @@ func TestHookRunner(t *testing.T) {
HookStage: sharedtest.HookStageAfterEvaluation,
EvalCapture: sharedtest.HookEvalCapture{
EvaluationSeriesContext: ldhooks.NewEvaluationSeriesContext("test-flag", ldContext,
false, "testMethod"),
falseValue, "testMethod"),
EvaluationSeriesData: ldhooks.EmptyEvaluationSeriesData(),
Detail: detail,
GoContext: context.Background(),
Expand Down Expand Up @@ -231,7 +233,7 @@ func TestHookRunner(t *testing.T) {
}

runner := NewHookRunner(mockLog.Loggers, []ldhooks.Hook{hookA, hookB})
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, false,
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, falseValue,
"testMethod")

res := runner.BeforeEvaluation(context.Background(), execution)
Expand All @@ -246,7 +248,7 @@ func TestHookRunner(t *testing.T) {
ldhooks.EmptyEvaluationSeriesData()).
Set("testB", "testB").
Build(), res.data[1])
assert.Equal(t, ldvalue.Bool(false), res.context.DefaultValue())
assert.Equal(t, falseValue, res.context.DefaultValue())

assert.Equal(t, []string{"During evaluation of flag \"test-flag\", an error was encountered in \"BeforeEvaluation\" of the \"a\" hook: something bad"},
mockLog.GetOutput(ldlog.Error))
Expand Down Expand Up @@ -280,9 +282,9 @@ func TestHookRunner(t *testing.T) {
}

runner := NewHookRunner(mockLog.Loggers, []ldhooks.Hook{hookA, hookB})
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, false,
execution := runner.PrepareEvaluationSeries("test-flag", ldContext, falseValue,
"testMethod")
detail := ldreason.NewEvaluationDetail(ldvalue.Bool(false), 0,
detail := ldreason.NewEvaluationDetail(falseValue, 0,
ldreason.NewEvalReasonFallthrough())

res := runner.AfterEvaluation(context.Background(), execution, detail)
Expand All @@ -297,7 +299,7 @@ func TestHookRunner(t *testing.T) {
ldhooks.EmptyEvaluationSeriesData()).
Set("testA", "testA").
Build(), res.data[0])
assert.Equal(t, ldvalue.Bool(false), res.context.DefaultValue())
assert.Equal(t, falseValue, res.context.DefaultValue())
assert.Equal(t, []string{"During evaluation of flag \"test-flag\", an error was encountered in \"AfterEvaluation\" of the \"b\" hook: something bad"},
mockLog.GetOutput(ldlog.Error))
})
Expand Down
4 changes: 2 additions & 2 deletions ldhooks/evaluation_series_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type EvaluationSeriesContext struct {
// NewEvaluationSeriesContext create a new EvaluationSeriesContext. Hook implementations do not need to use this
// function.
func NewEvaluationSeriesContext(flagKey string, evalContext ldcontext.Context,
defaultValue any, method string) EvaluationSeriesContext {
defaultValue ldvalue.Value, method string) EvaluationSeriesContext {
return EvaluationSeriesContext{
flagKey: flagKey,
context: evalContext,
defaultValue: ldvalue.CopyArbitraryValue(defaultValue),
defaultValue: defaultValue,
method: method,
}
}
Expand Down

0 comments on commit 133d0e3

Please sign in to comment.