Skip to content

Commit a1b04e7

Browse files
Craig Pastrobeeme1mrtoddbaert
authored
feat: add $flagd.timestamp to json evaluator (#958)
<!-- Please use this template for your pull request. --> <!-- Please use the sections that you need and delete other sections --> ## This PR <!-- add the description of the PR here --> Adds "timestamp" to the json evaluation context. ### Related Issues <!-- add here the GitHub issue that this PR resolves if applicable --> Related to #851. I am not sure we want to say that it closes the issue though. --------- Signed-off-by: Craig Pastro <pastro.craig@gmail.com> Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com> Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
1 parent fee1558 commit a1b04e7

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

core/pkg/eval/json_evaluator.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"regexp"
1010
"strconv"
1111
"strings"
12+
"time"
1213

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

4041
type flagdProperties struct {
41-
FlagKey string `json:"flagKey"`
42+
FlagKey string `json:"flagKey"`
43+
Timestamp int64 `json:"timestamp"`
4244
}
4345

4446
func init() {
@@ -321,7 +323,8 @@ func (je *JSONEvaluator) evaluateVariant(reqID string, flagKey string, context m
321323
}
322324

323325
context = je.setFlagdProperties(context, flagdProperties{
324-
FlagKey: flagKey,
326+
FlagKey: flagKey,
327+
Timestamp: time.Now().Unix(),
325328
})
326329

327330
b, err := json.Marshal(context)

core/pkg/eval/json_evaluator_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,4 +1248,36 @@ func TestFlagdAmbientProperties(t *testing.T) {
12481248
t.Fatalf("expected %s, got %s", model.TargetingMatchReason, reason)
12491249
}
12501250
})
1251+
1252+
t.Run("timestampIsInTheContext", func(t *testing.T) {
1253+
evaluator := eval.NewJSONEvaluator(logger.NewLogger(nil, false), store.NewFlags())
1254+
1255+
_, _, err := evaluator.SetState(sync.DataSync{FlagData: `{
1256+
"flags": {
1257+
"welcome-banner": {
1258+
"state": "ENABLED",
1259+
"variants": {
1260+
"true": true,
1261+
"false": false
1262+
},
1263+
"defaultVariant": "false",
1264+
"targeting": {
1265+
"<": [ 1696904426, { "var": "$flagd.timestamp" } ]
1266+
}
1267+
}
1268+
}
1269+
}`})
1270+
if err != nil {
1271+
t.Fatal(err)
1272+
}
1273+
1274+
value, variant, reason, _, err := evaluator.ResolveBooleanValue(context.Background(), "default", "welcome-banner", nil)
1275+
if err != nil {
1276+
t.Fatal(err)
1277+
}
1278+
1279+
if !value || variant != "true" || reason != model.TargetingMatchReason {
1280+
t.Fatal("timestamp was not in the context")
1281+
}
1282+
})
12511283
}

0 commit comments

Comments
 (0)