Skip to content

Commit

Permalink
Fixed task and choice Validate()
Browse files Browse the repository at this point in the history
Parse choice: test passed
Fixed all tests
  • Loading branch information
redjack96 committed Aug 18, 2024
1 parent 2669ae7 commit 7fe4a23
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 44 deletions.
16 changes: 13 additions & 3 deletions internal/asl/choice_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package asl
import (
"fmt"
"github.com/grussorusso/serverledge/internal/types"
"strconv"
"strings"
)

Expand Down Expand Up @@ -84,20 +85,29 @@ func ParseDataTestExpr(jsonRule []byte) (*DataTestExpression, error) {
// comparator kind field
comparisonOperator.Kind = comparator
comparatorString := string(comparator)
// comparator datatype field
// comparator datatype and operand fields
if strings.HasPrefix(comparatorString, "String") {
comparisonOperator.DataType = StringComparator
comparisonOperator.Operand = comparatorValue
} else if strings.HasPrefix(comparatorString, "Numeric") {
comparisonOperator.DataType = NumericComparator
comparisonOperator.Operand, err = strconv.Atoi(comparatorValue)
if err != nil {
return nil, fmt.Errorf("failed to convert to int the value %s: %v", comparatorValue, err)
}
} else if strings.HasPrefix(comparatorString, "Timestamp") {
comparisonOperator.DataType = TimestampComparator
comparisonOperator.Operand = comparatorValue
} else if strings.HasPrefix(comparatorString, "Boolean") || strings.HasPrefix(comparatorString, "Is") {
comparisonOperator.DataType = BooleanComparator
comparisonOperator.Operand, err = strconv.ParseBool(comparatorValue)
if err != nil {
return nil, fmt.Errorf("failed to convert to bool the value %s: %v", comparatorValue, err)
}
} else {
return nil, fmt.Errorf("invalid comparator: %s", comparator)
}
// comparator operand field
comparisonOperator.Operand = comparatorValue

// we have found a valid comparator, so we exit the for loop
break
}
Expand Down
6 changes: 3 additions & 3 deletions internal/asl/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ func JsonExtractObjectOrDefault(json []byte, key string, def interface{}) interf
return value
}

func JsonExtractRefPath(json []byte, key string) (Path, error) {
value, _, _, err := jsonparser.Get(json, key)
if err != nil {
func JsonTryExtractRefPath(json []byte, key string) (Path, error) {
value, d, _, err := jsonparser.Get(json, key)
if err != nil && d != jsonparser.NotExist {
return "", err
}
path, errO := NewReferencePath(string(value))
Expand Down
2 changes: 1 addition & 1 deletion internal/asl/state_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ParseFrom(name string, aslSrc []byte) (*StateMachine, error) {
return &sm, nil
}

func (sm *StateMachine) getAllStateNames() []string {
func (sm *StateMachine) GetAllStateNames() []string {
// getting all states names
definedStateNames := make([]string, 0, len(sm.States))
for stateName := range sm.States {
Expand Down
28 changes: 19 additions & 9 deletions internal/asl/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type TaskState struct {
}

func (t *TaskState) ParseFrom(jsonData []byte) (State, error) {

var err error
t.Resource = JsonExtractStringOrDefault(jsonData, "Resource", "")

t.End = JsonExtractBool(jsonData, "End")
Expand All @@ -42,11 +42,15 @@ func (t *TaskState) ParseFrom(jsonData []byte) (State, error) {
t.Catch = JsonExtractObjectOrDefault(jsonData, "Catch", &Catch{}).(*Catch)

t.TimeoutSeconds = uint32(JsonExtractIntOrDefault(jsonData, "TimeoutSeconds", 0))
t.TimeoutSecondsPath = JsonExtractRefPathOrDefault(jsonData, "TimeoutSecondsPath", "")

t.TimeoutSecondsPath, err = JsonTryExtractRefPath(jsonData, "TimeoutSecondsPath")
if err != nil {
return nil, err
}
t.HeartbeatSeconds = uint32(JsonExtractIntOrDefault(jsonData, "HeartbeatSeconds", 0))
t.HeartbeatSecondsPath = JsonExtractRefPathOrDefault(jsonData, "HeartbeatSecondsPath", "")

t.HeartbeatSecondsPath, err = JsonTryExtractRefPath(jsonData, "HeartbeatSecondsPath")
if err != nil {
return nil, err
}
return t, nil
}

Expand All @@ -64,8 +68,14 @@ func (t *TaskState) Validate(stateNames []string) error {
return fmt.Errorf("HeartbeatSeconds %d exceeds timeout %d", t.HeartbeatSeconds, t.TimeoutSeconds)
}

// TODO: If there are both TimeoutSeconds and TimeoutSecondsPath, return an error
// TODO: If there are both HeartbeatSeconds and HeartbeatSecondsPath, return an error
if t.TimeoutSecondsPath != "" && t.TimeoutSeconds != 0 {
return fmt.Errorf("TimeoutSecondsPath and TimeoutSeconds cannot be set at the same time")
}

if t.HeartbeatSecondsPath != "" && t.HeartbeatSeconds != 0 {
return fmt.Errorf("HeartbeatSecondsPath and HeartbeatSeconds cannot be set at the same time")
}

return nil
}

Expand Down Expand Up @@ -203,13 +213,13 @@ func (t *TaskState) String() string {
if t.TimeoutSeconds != 0 {
str += fmt.Sprintf("\t\t\tTimeoutSeconds: %d\n", t.TimeoutSeconds)
}
if t.TimeoutSeconds != 0 {
if t.TimeoutSecondsPath != "" {
str += fmt.Sprintf("\t\t\tTimeoutSecondsPath: %s\n", t.TimeoutSecondsPath)
}
if t.HeartbeatSeconds != 0 {
str += fmt.Sprintf("\t\t\tHeartbeatSeconds: %d\n", t.HeartbeatSeconds)
}
if t.HeartbeatSeconds != 0 {
if t.HeartbeatSecondsPath != "" {
str += fmt.Sprintf("\t\t\tHeartbeatSecondsPath: %s\n", t.HeartbeatSecondsPath)
}
if t.End != false {
Expand Down
6 changes: 3 additions & 3 deletions internal/test/parse_choice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// [Task][Task] [Task]
// ... is correctly parsed.
func TestParseChoiceWithThreeBranches(t *testing.T) {
t.Skip("WIP")
// t.Skip("WIP")
choice := []byte(`{
"Comment": "An example of the Amazon States Language using a choice state.",
"StartAt": "ChoiceState",
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestParseChoiceWithThreeBranches(t *testing.T) {
utils.AssertNilMsg(t, err, "failed to parse state machine")

smExpected := &asl.StateMachine{
StartAt: "FirstState",
StartAt: "ChoiceState",
Comment: "An example of the Amazon States Language using a choice state.",
Version: "1.0",
Name: "choice",
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestParseChoiceWithThreeBranches(t *testing.T) {
},
InputPath: "",
OutputPath: "",
Default: "DefaultSTate",
Default: "DefaultState",
},
"FirstMatchState": asl.NewNonTerminalTask("inc", "NextState"),
"SecondMatchState": asl.NewNonTerminalTask("double", "NextState"),
Expand Down
37 changes: 12 additions & 25 deletions internal/test/parse_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,28 +124,9 @@ func TestParseTaskWithoutEnd(t *testing.T) {
}
}`)

_, err := asl.ParseFrom("simple", simple)
utils.AssertNonNilMsg(t, err, "parsing should have failed")
fmt.Printf("Expected error: %v\n", err)
}

func TestParseTaskWithInvalidPath(t *testing.T) {
simple := []byte(`{
"Comment": "A simple state machine with 2 task state",
"StartAt": "FirstState",
"States": {
"FirstState": {
"Comment": "The first task",
"Resource": "inc"
"Type": "Task",
"TimeoutSeconds": 10,
"HeartbeatSecondsPath": "$.@<3heart"
"End": true
},
}
}`)

_, err := asl.ParseFrom("simple", simple)
sm, err1 := asl.ParseFrom("simple", simple)
utils.AssertNilMsg(t, err1, "parsing failed")
err := sm.Validate(sm.GetAllStateNames())
utils.AssertNonNilMsg(t, err, "parsing should have failed")
fmt.Printf("Expected error: %v\n", err)
}
Expand All @@ -166,7 +147,9 @@ func TestParseTaskWithInvalidHeartbeat(t *testing.T) {
}
}`)

_, err := asl.ParseFrom("simple", simple)
sm, err1 := asl.ParseFrom("simple", simple)
utils.AssertNilMsg(t, err1, "parsing failed")
err := sm.Validate(sm.GetAllStateNames())
utils.AssertNonNilMsg(t, err, "parsing should have failed")
fmt.Printf("Expected error: %v\n", err)
}
Expand All @@ -187,7 +170,9 @@ func TestParseTaskWithBothPathAndHeartbeat(t *testing.T) {
}
}`)

_, err := asl.ParseFrom("simple", simple)
sm, err1 := asl.ParseFrom("simple", simple)
utils.AssertNilMsg(t, err1, "parsing failed")
err := sm.Validate(sm.GetAllStateNames())
utils.AssertNonNilMsg(t, err, "parsing should have failed")
fmt.Printf("Expected error: %v\n", err)
}
Expand All @@ -207,7 +192,9 @@ func TestParseTaskWithOnlyHeartbeat(t *testing.T) {
}
}`)

_, err := asl.ParseFrom("simple", simple)
sm, err1 := asl.ParseFrom("simple", simple)
utils.AssertNilMsg(t, err1, "parsing failed")
err := sm.Validate(sm.GetAllStateNames())
utils.AssertNonNilMsg(t, err, "parsing should have failed")
fmt.Printf("Expected error: %v\n", err)
}

0 comments on commit 7fe4a23

Please sign in to comment.