Skip to content

Commit

Permalink
Added and tested IsTimestamp (RFC3339)
Browse files Browse the repository at this point in the history
  • Loading branch information
redjack96 committed Sep 3, 2024
1 parent 7dcb1cb commit 2bbf85b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/fc/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/grussorusso/serverledge/utils"
"regexp"
"strings"
"time"
)

type Predicate struct {
Expand Down Expand Up @@ -121,6 +122,8 @@ func (c Condition) String() string {
return fmt.Sprintf("IsString(%v)", c.Op[0])
case IsBoolean:
return fmt.Sprintf("IsBoolean(%v)", c.Op[0])
case IsTimestamp:
return fmt.Sprintf("IsTimestamp(%v)", c.Op[0])
case StringMatches:
return fmt.Sprintf("StringMatches(%s,%s)", c.Op[0], c.Op[1])
default:
Expand Down Expand Up @@ -344,6 +347,20 @@ func (c Condition) Test(input map[string]interface{}) (bool, error) {
}
_, ok := ops[0].(bool)
return ok, nil
case IsTimestamp:
ops, err := c.findInputs(input)
if err != nil {
return false, err
}
maybeTimestamp, ok := ops[0].(string)
if !ok {
return false, nil
}
_, errTimestamp := time.Parse(time.RFC3339, maybeTimestamp)
if errTimestamp != nil {
return false, nil
}
return true, nil
case StringMatches:
ops, err := c.findInputs(input)
inputString, okString := ops[0].(string)
Expand Down Expand Up @@ -686,6 +703,14 @@ func NewIsBooleanParamCondition(param1 *ParamOrValue) Condition {
}
}

func NewIsTimestampParamCondition(param1 *ParamOrValue) Condition {
return Condition{
Type: IsTimestamp,
Find: []bool{param1.IsParam},
Op: []interface{}{param1.GetOperand()},
}
}

func NewStringMatchesParamCondition(param1 *ParamOrValue, param2 *ParamOrValue) Condition {
return Condition{
Type: StringMatches,
Expand Down
26 changes: 26 additions & 0 deletions internal/test/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,29 @@ func TestIsBoolean(t *testing.T) {
utils.AssertEqualsMsg(t, ok, test.shouldBeBool, fmt.Sprintf("test %d: expected IsBool(%v) to be %v", i+1, test.paramOrValue, test.shouldBeBool))
}
}

func TestIsTimestamp(t *testing.T) {
tests := []struct {
paramOrValue *fc.ParamOrValue
shouldBeTimestamp bool
}{
{fc.NewParam("timestamp"), true},
{fc.NewValue("2024-09-03T14:07:42Z"), true},
{fc.NewParam("not_a_timestamp"), false},
{fc.NewParam("not_a_timestamp2"), false},
{fc.NewParam("invalid_timestamp"), false},
{fc.NewParam("not-existent-field"), false},
}
testMap := make(map[string]interface{})
testMap["timestamp"] = "2024-09-03T14:07:42Z"
testMap["invalid_timestamp"] = "2024-09-03 14:07:42"
testMap["not_a_timestamp"] = "random_string"
testMap["not_a_timestamp2"] = 123

for i, test := range tests {
cond := fc.NewIsTimestampParamCondition(test.paramOrValue)
ok, err := cond.Test(testMap)
utils.AssertNil(t, err)
utils.AssertEqualsMsg(t, ok, test.shouldBeTimestamp, fmt.Sprintf("test %d: expected IsTimestamp(%v) to be %v", i+1, test.paramOrValue.GetOperand(), test.shouldBeTimestamp))
}
}

0 comments on commit 2bbf85b

Please sign in to comment.