Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

date operator support for feature flags local eval #11

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
date operator support for feature flags local eval
  • Loading branch information
liyiy committed Sep 28, 2022
commit 2d91957f9bbeb355a41acef3c7e6817b59749023
53 changes: 53 additions & 0 deletions feature_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"strings"
"time"

"testing"
)
Expand Down Expand Up @@ -224,6 +225,58 @@ func TestMatchPropertyContains(t *testing.T) {
}
}

func TestMatchPropertyDate(t *testing.T) {
shouldMatchA := []interface{}{"2022-04-30T00:00:00+00:00", "2022-04-30T00:00:00+02:00", time.Date(2022, 3, 20, 20, 34, 58, 651387237, time.UTC)}
shouldNotMatchA := NewProperties().Set("key", "2022-05-30T00:00:00+00:00")
propertyA := Property{
Key: "key",
Value: "2022-05-01",
Operator: "is_date_before",
}
for _, val := range shouldMatchA {
isMatch, err := matchProperty(propertyA, NewProperties().Set("key", val))
if err != nil {
t.Error(err)
}

if !isMatch {
t.Error(("Value is not a match"))
}
}
isMatchA, errA := matchProperty(propertyA, shouldNotMatchA)
if errA != nil {
t.Error(errA)
}
if isMatchA {
t.Error("Value is not a match")
}

shouldMatchB := []interface{}{"2022-05-30T00:00:00+00:00", time.Date(2022, 5, 30, 20, 34, 58, 651387237, time.UTC)}
shouldNotMatchB := NewProperties().Set("key", "2022-04-29T00:00:00+00:00")
propertyB := Property{
Key: "key",
Value: "2022-05-01T00:00:00+00:00",
Operator: "is_date_after",
}
for _, val := range shouldMatchB {
isMatch, err := matchProperty(propertyB, NewProperties().Set("key", val))
if err != nil {
t.Error(err)
}

if !isMatch {
t.Error(("Value is not a match"))
}
}
isMatchB, errB := matchProperty(propertyB, shouldNotMatchB)
if errB != nil {
t.Error(errB)
}
if isMatchB {
t.Error("Value is not a match")
}
}

func TestFlagPersonProperty(t *testing.T) {

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
29 changes: 29 additions & 0 deletions featureflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,39 @@ func matchProperty(property Property, properties Properties) (bool, error) {
return overrideValueOrderable <= valueOrderable, nil
}

if (operator == "is_date_before") || (operator == "is_date_after") {
valueDate, valueDateErr := convertToDateTime(value)
if valueDateErr != nil {
return false, valueDateErr
}
overrideDate, err := convertToDateTime(override_value)
if err != nil {
return false, err
}
if operator == "is_date_before" {
return overrideDate.Before(valueDate), nil
}
return overrideDate.After(valueDate), nil
}

return false, nil

}

func convertToDateTime(value interface{}) (time.Time, error) {
if valueDate, ok := value.(time.Time); ok {
return valueDate, nil
} else if valueString, ok := value.(string); ok {
stringToDate, err := time.Parse(time.RFC3339, valueString)
if err == nil {
return stringToDate, nil
}
return time.Now(), &InconclusiveMatchError{fmt.Sprintf("Value %d is not in a valid ISO8601 string format", value)}
} else {
return time.Now(), &InconclusiveMatchError{fmt.Sprintf("Value %d must be in string or date format", value)}
}
}

func validateOrderable(firstValue interface{}, secondValue interface{}) (float64, float64, error) {
convertedFirstValue, err := interfaceToFloat(firstValue)

Expand Down