Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add $flagd.timestamp to json evaluator #958

Merged
merged 4 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions core/pkg/eval/json_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"
"strconv"
"strings"
"time"

"github.com/diegoholiveira/jsonlogic/v3"
"github.com/open-feature/flagd/core/pkg/logger"
Expand Down Expand Up @@ -38,7 +39,8 @@ const (
var regBrace *regexp.Regexp

type flagdProperties struct {
FlagKey string `json:"flagKey"`
FlagKey string `json:"flagKey"`
Timestamp time.Time `json:"timestamp"`
}

func init() {
Expand Down Expand Up @@ -321,7 +323,8 @@ func (je *JSONEvaluator) evaluateVariant(reqID string, flagKey string, context m
}

context = je.setFlagdProperties(context, flagdProperties{
FlagKey: flagKey,
FlagKey: flagKey,
Timestamp: time.Now(),
})

b, err := json.Marshal(context)
Expand Down
32 changes: 32 additions & 0 deletions core/pkg/eval/json_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1248,4 +1248,36 @@ func TestFlagdAmbientProperties(t *testing.T) {
t.Fatalf("expected %s, got %s", model.TargetingMatchReason, reason)
}
})

t.Run("timestampIsInTheContext", func(t *testing.T) {
evaluator := eval.NewJSONEvaluator(logger.NewLogger(nil, false), store.NewFlags())

_, _, err := evaluator.SetState(sync.DataSync{FlagData: `{
"flags": {
"welcome-banner": {
"state": "ENABLED",
"variants": {
"true": true,
"false": false
},
"defaultVariant": "false",
"targeting": {
"if": [ { "var": ["$flagd.timestamp", false] }, true, false ]
}
}
}
}`})
if err != nil {
t.Fatal(err)
}

value, variant, reason, _, err := evaluator.ResolveBooleanValue(context.Background(), "default", "welcome-banner", nil)
if err != nil {
t.Fatal(err)
}

if !value || variant != "true" || reason != model.TargetingMatchReason {
t.Fatal("timestamp was not in the context")
}
})
}