From 10bcfba15bcf18ac66f8e6462aee7870dd0b7f07 Mon Sep 17 00:00:00 2001 From: Songlin Yang <511121939@qq.com> Date: Wed, 5 Oct 2022 20:30:10 +0800 Subject: [PATCH] feat(*): add InvokeKind enum (#97) Signed-off-by: lsytj0413 <511121939@qq.com> Signed-off-by: lsytj0413 <511121939@qq.com> --- model/action.go | 12 ++++++------ model/action_test.go | 14 +++++++------- model/event.go | 7 ++----- model/event_test.go | 4 ++-- model/workflow.go | 11 +++++++++++ 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/model/action.go b/model/action.go index af63044..1ec9a50 100644 --- a/model/action.go +++ b/model/action.go @@ -74,7 +74,7 @@ type FunctionRef struct { // Invoke specifies if the subflow should be invoked sync or async. // Defaults to sync. - Invoke string `json:"invoke,omitempty" validate:"required,oneof=async sync"` + Invoke InvokeKind `json:"invoke,omitempty" validate:"required,oneof=async sync"` } type functionRefForUnmarshal FunctionRef @@ -93,11 +93,11 @@ func (f *FunctionRef) UnmarshalJSON(data []byte) error { if err != nil { return err } - f.Invoke = "sync" + f.Invoke = InvokeKindSync return nil case '{': v := functionRefForUnmarshal{ - Invoke: "sync", + Invoke: InvokeKindSync, } err = json.Unmarshal(data, &v) if err != nil { @@ -120,7 +120,7 @@ type WorkflowRef struct { // Invoke specifies if the subflow should be invoked sync or async. // Defaults to sync. - Invoke string `json:"invoke,omitempty" validate:"required,oneof=async sync"` + Invoke InvokeKind `json:"invoke,omitempty" validate:"required,oneof=async sync"` // OnParantComplete specifies how subflow execution should behave when parent workflow completes if invoke is 'async'。 // Defaults to terminate. @@ -143,11 +143,11 @@ func (s *WorkflowRef) UnmarshalJSON(data []byte) error { if err != nil { return err } - s.Invoke, s.OnParentComplete = "sync", "terminate" + s.Invoke, s.OnParentComplete = InvokeKindSync, "terminate" return nil case '{': v := workflowRefForUnmarshal{ - Invoke: "sync", + Invoke: InvokeKindSync, OnParentComplete: "terminate", } err = json.Unmarshal(data, &v) diff --git a/model/action_test.go b/model/action_test.go index 7e022e9..73f7d32 100644 --- a/model/action_test.go +++ b/model/action_test.go @@ -37,7 +37,7 @@ func TestWorkflowRefUnmarshalJSON(t *testing.T) { expect: WorkflowRef{ WorkflowID: "1", Version: "2", - Invoke: "async", + Invoke: InvokeKindAsync, OnParentComplete: "continue", }, err: ``, @@ -48,7 +48,7 @@ func TestWorkflowRefUnmarshalJSON(t *testing.T) { expect: WorkflowRef{ WorkflowID: "1", Version: "", - Invoke: "sync", + Invoke: InvokeKindSync, OnParentComplete: "terminate", }, err: ``, @@ -59,7 +59,7 @@ func TestWorkflowRefUnmarshalJSON(t *testing.T) { expect: WorkflowRef{ WorkflowID: "1", Version: "", - Invoke: "sync", + Invoke: InvokeKindSync, OnParentComplete: "terminate", }, err: ``, @@ -118,7 +118,7 @@ func TestWorkflowRefValidate(t *testing.T) { workflowRef: WorkflowRef{ WorkflowID: "1", Version: "2", - Invoke: "sync", + Invoke: InvokeKindSync, OnParentComplete: "terminate", }, err: ``, @@ -128,7 +128,7 @@ func TestWorkflowRefValidate(t *testing.T) { workflowRef: WorkflowRef{ WorkflowID: "1", Version: "2", - Invoke: "async", + Invoke: InvokeKindAsync, OnParentComplete: "continue", }, err: ``, @@ -138,7 +138,7 @@ func TestWorkflowRefValidate(t *testing.T) { workflowRef: WorkflowRef{ WorkflowID: "", Version: "2", - Invoke: "sync", + Invoke: InvokeKindSync, OnParentComplete: "terminate", }, err: `Key: 'WorkflowRef.WorkflowID' Error:Field validation for 'WorkflowID' failed on the 'required' tag`, @@ -158,7 +158,7 @@ func TestWorkflowRefValidate(t *testing.T) { workflowRef: WorkflowRef{ WorkflowID: "1", Version: "2", - Invoke: "sync", + Invoke: InvokeKindSync, OnParentComplete: "terminate1", }, err: `Key: 'WorkflowRef.OnParentComplete' Error:Field validation for 'OnParentComplete' failed on the 'oneof' tag`, diff --git a/model/event.go b/model/event.go index 2845b8d..a97189f 100644 --- a/model/event.go +++ b/model/event.go @@ -102,7 +102,7 @@ type EventRef struct { // Invoke specifies if the subflow should be invoked sync or async. // Defaults to sync. - Invoke string `json:"invoke,omitempty" validate:"required,oneof=async sync"` + Invoke InvokeKind `json:"invoke,omitempty" validate:"required,oneof=async sync"` } type eventRefForUnmarshal EventRef @@ -110,7 +110,7 @@ type eventRefForUnmarshal EventRef // UnmarshalJSON implements json.Unmarshaler func (e *EventRef) UnmarshalJSON(data []byte) error { v := eventRefForUnmarshal{ - Invoke: "sync", + Invoke: InvokeKindSync, } err := json.Unmarshal(data, &v) if err != nil { @@ -120,6 +120,3 @@ func (e *EventRef) UnmarshalJSON(data []byte) error { *e = EventRef(v) return nil } - -// InvokeKind ... -type InvokeKind string diff --git a/model/event_test.go b/model/event_test.go index 6653344..d469898 100644 --- a/model/event_test.go +++ b/model/event_test.go @@ -36,7 +36,7 @@ func TestEventRefStructLevelValidation(t *testing.T) { TriggerEventRef: "example valid", ResultEventRef: "example valid", ResultEventTimeout: "PT1H", - Invoke: "sync", + Invoke: InvokeKindSync, }, err: ``, }, @@ -46,7 +46,7 @@ func TestEventRefStructLevelValidation(t *testing.T) { TriggerEventRef: "example invalid", ResultEventRef: "example invalid red", ResultEventTimeout: "10hs", - Invoke: "sync", + Invoke: InvokeKindSync, }, err: `Key: 'EventRef.ResultEventTimeout' Error:Field validation for 'ResultEventTimeout' failed on the 'reqiso8601duration' tag`, }, diff --git a/model/workflow.go b/model/workflow.go index ba86143..e785477 100644 --- a/model/workflow.go +++ b/model/workflow.go @@ -19,6 +19,17 @@ import ( "fmt" ) +// InvokeKind defines how the target is invoked. +type InvokeKind string + +const ( + // InvokeKindSync meaning that worfklow execution should wait until the target completes. + InvokeKindSync InvokeKind = "sync" + + // InvokeKindAsync meaning that workflow execution should just invoke the target and should not wait until its completion. + InvokeKindAsync InvokeKind = "async" +) + const ( // DefaultExpressionLang ... DefaultExpressionLang = "jq"