diff --git a/model/states.go b/model/states.go index f005c24..42c7b48 100644 --- a/model/states.go +++ b/model/states.go @@ -104,7 +104,7 @@ type BaseState struct { func (b *BaseState) MarshalJSON() ([]byte, error) { type Alias BaseState if b == nil { - return []byte("null"), nil + return nil, nil } cus, err := json.Marshal(struct { *Alias @@ -148,7 +148,7 @@ type State struct { func (s *State) MarshalJSON() ([]byte, error) { if s == nil { - return []byte("null"), nil + return nil, nil } r := []byte("") var errs error diff --git a/model/switch_state.go b/model/switch_state.go index 9d05d7e..70f1b28 100644 --- a/model/switch_state.go +++ b/model/switch_state.go @@ -16,6 +16,7 @@ package model import ( "encoding/json" + "strings" ) // SwitchState is workflow's gateways: direct transitions onf a workflow based on certain conditions. @@ -36,6 +37,23 @@ type SwitchState struct { Timeouts *SwitchStateTimeout `json:"timeouts,omitempty"` } +func (s *SwitchState) MarshalJSON() ([]byte, error) { + type Alias SwitchState + custom, err := json.Marshal(&struct { + *Alias + Timeouts *SwitchStateTimeout `json:"timeouts,omitempty"` + }{ + Alias: (*Alias)(s), + Timeouts: s.Timeouts, + }) + + // Avoid marshal empty objects as null. + st := strings.Replace(string(custom), "\"eventConditions\":null,", "", 1) + st = strings.Replace(st, "\"dataConditions\":null,", "", 1) + st = strings.Replace(st, "\"end\":null,", "", -1) + return []byte(st), err +} + // DefaultCondition Can be either a transition or end definition type DefaultCondition struct { // Serverless workflow states can have one or more incoming and outgoing transitions (from/to other states). @@ -68,18 +86,6 @@ func (e *DefaultCondition) UnmarshalJSON(data []byte) error { return err } -func (s *SwitchState) MarshalJSON() ([]byte, error) { - type Alias SwitchState - custom, err := json.Marshal(&struct { - *Alias - Timeouts *SwitchStateTimeout `json:"timeouts,omitempty"` - }{ - Alias: (*Alias)(s), - Timeouts: s.Timeouts, - }) - return custom, err -} - // SwitchStateTimeout defines the specific timeout settings for switch state type SwitchStateTimeout struct { // Default workflow state execution timeout (ISO 8601 duration format) diff --git a/model/util.go b/model/util.go index 1048602..2ae4226 100644 --- a/model/util.go +++ b/model/util.go @@ -31,8 +31,8 @@ import ( "sigs.k8s.io/yaml" ) +// Kind ... // +k8s:deepcopy-gen=false - type Kind interface { KindValues() []string String() string @@ -41,6 +41,8 @@ type Kind interface { // TODO: Remove global variable var httpClient = http.Client{Timeout: time.Duration(1) * time.Second} +// UnmarshalError ... +// +k8s:deepcopy-gen=false type UnmarshalError struct { err error parameterName string diff --git a/model/zz_generated.deepcopy.go b/model/zz_generated.deepcopy.go index 4a3e961..d04a11b 100644 --- a/model/zz_generated.deepcopy.go +++ b/model/zz_generated.deepcopy.go @@ -105,28 +105,6 @@ func (in *Auth) DeepCopy() *Auth { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in Auths) DeepCopyInto(out *Auths) { - { - in := &in - *out = make(Auths, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - return - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Auths. -func (in Auths) DeepCopy() Auths { - if in == nil { - return nil - } - out := new(Auths) - in.DeepCopyInto(out) - return *out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *AuthProperties) DeepCopyInto(out *AuthProperties) { *out = *in @@ -158,6 +136,28 @@ func (in *AuthProperties) DeepCopy() *AuthProperties { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in Auths) DeepCopyInto(out *Auths) { + { + in := &in + *out = make(Auths, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + return + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Auths. +func (in Auths) DeepCopy() Auths { + if in == nil { + return nil + } + out := new(Auths) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *BaseState) DeepCopyInto(out *BaseState) { *out = *in @@ -223,7 +223,7 @@ func (in *BaseWorkflow) DeepCopyInto(out *BaseWorkflow) { if in.DataInputSchema != nil { in, out := &in.DataInputSchema, &out.DataInputSchema *out = new(DataInputSchema) - (*in).DeepCopyInto(*out) + **out = **in } if in.Secrets != nil { in, out := &in.Secrets, &out.Secrets @@ -242,7 +242,7 @@ func (in *BaseWorkflow) DeepCopyInto(out *BaseWorkflow) { } if in.Errors != nil { in, out := &in.Errors, &out.Errors - *out = make([]Error, len(*in)) + *out = make(Errors, len(*in)) copy(*out, *in) } if in.Metadata != nil { @@ -426,7 +426,7 @@ func (in *Constants) DeepCopyInto(out *Constants) { *out = *in if in.Data != nil { in, out := &in.Data, &out.Data - *out = make(map[string]json.RawMessage, len(*in)) + *out = make(ConstantsData, len(*in)) for key, val := range *in { var outVal []byte if val == nil { @@ -452,6 +452,36 @@ func (in *Constants) DeepCopy() *Constants { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in ConstantsData) DeepCopyInto(out *ConstantsData) { + { + in := &in + *out = make(ConstantsData, len(*in)) + for key, val := range *in { + var outVal []byte + if val == nil { + (*out)[key] = nil + } else { + in, out := &val, &outVal + *out = make(json.RawMessage, len(*in)) + copy(*out, *in) + } + (*out)[key] = outVal + } + return + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ConstantsData. +func (in ConstantsData) DeepCopy() ConstantsData { + if in == nil { + return nil + } + out := new(ConstantsData) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ContinueAs) DeepCopyInto(out *ContinueAs) { *out = *in @@ -637,6 +667,26 @@ func (in *Error) DeepCopy() *Error { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in Errors) DeepCopyInto(out *Errors) { + { + in := &in + *out = make(Errors, len(*in)) + copy(*out, *in) + return + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Errors. +func (in Errors) DeepCopy() Errors { + if in == nil { + return nil + } + out := new(Errors) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Event) DeepCopyInto(out *Event) { *out = *in @@ -790,6 +840,28 @@ func (in *EventStateTimeout) DeepCopy() *EventStateTimeout { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in Events) DeepCopyInto(out *Events) { + { + in := &in + *out = make(Events, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + return + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Events. +func (in Events) DeepCopy() Events { + if in == nil { + return nil + } + out := new(Events) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ForEachState) DeepCopyInto(out *ForEachState) { *out = *in @@ -884,6 +956,28 @@ func (in *FunctionRef) DeepCopy() *FunctionRef { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in Functions) DeepCopyInto(out *Functions) { + { + in := &in + *out = make(Functions, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + return + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Functions. +func (in Functions) DeepCopy() Functions { + if in == nil { + return nil + } + out := new(Functions) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *InjectState) DeepCopyInto(out *InjectState) { *out = *in @@ -1186,6 +1280,28 @@ func (in *ProduceEvent) DeepCopy() *ProduceEvent { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in Retries) DeepCopyInto(out *Retries) { + { + in := &in + *out = make(Retries, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + return + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Retries. +func (in Retries) DeepCopy() Retries { + if in == nil { + return nil + } + out := new(Retries) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Retry) DeepCopyInto(out *Retry) { *out = *in @@ -1423,6 +1539,28 @@ func (in *StateExecTimeout) DeepCopy() *StateExecTimeout { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in States) DeepCopyInto(out *States) { + { + in := &in + *out = make(States, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + return + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new States. +func (in States) DeepCopy() States { + if in == nil { + return nil + } + out := new(States) + in.DeepCopyInto(out) + return *out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SwitchState) DeepCopyInto(out *SwitchState) { *out = *in @@ -1542,21 +1680,21 @@ func (in *Workflow) DeepCopyInto(out *Workflow) { } if in.Events != nil { in, out := &in.Events, &out.Events - *out = make([]Event, len(*in)) + *out = make(Events, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } if in.Functions != nil { in, out := &in.Functions, &out.Functions - *out = make([]Function, len(*in)) + *out = make(Functions, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } } if in.Retries != nil { in, out := &in.Retries, &out.Retries - *out = make([]Retry, len(*in)) + *out = make(Retries, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } diff --git a/parser/parser_test.go b/parser/parser_test.go index 014ba2f..5913ea2 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -956,10 +956,10 @@ states: assert.True(t, strings.Contains(string(b), "{\"name\":\"ParallelExec\",\"type\":\"parallel\",\"transition\":{\"nextState\":\"CheckVisaStatusSwitchEventBased\"},\"branches\":[{\"name\":\"ShortDelayBranch\",\"actions\":[{\"subFlowRef\":{\"workflowId\":\"shortdelayworkflowid\",\"invoke\":\"sync\",\"onParentComplete\":\"terminate\"},\"actionDataFilter\":{\"useResults\":true}}],\"timeouts\":{\"actionExecTimeout\":\"PT5H\",\"branchExecTimeout\":\"PT6M\"}},{\"name\":\"LongDelayBranch\",\"actions\":[{\"subFlowRef\":{\"workflowId\":\"longdelayworkflowid\",\"invoke\":\"sync\",\"onParentComplete\":\"terminate\"},\"actionDataFilter\":{\"useResults\":true}}]}],\"completionType\":\"atLeast\",\"numCompleted\":13,\"timeouts\":{\"stateExecTimeout\":{\"single\":\"PT2S\",\"total\":\"PT1S\"},\"branchExecTimeout\":\"PT6M\"}}")) // Switch State - assert.True(t, strings.Contains(string(b), "{\"name\":\"CheckVisaStatusSwitchEventBased\",\"type\":\"switch\",\"defaultCondition\":{\"transition\":{\"nextState\":\"HelloStateWithDefaultConditionString\"}},\"eventConditions\":[{\"name\":\"visaApprovedEvent\",\"eventRef\":\"visaApprovedEventRef\",\"metadata\":{\"mastercard\":\"disallowed\",\"visa\":\"allowed\"},\"end\":null,\"transition\":{\"nextState\":\"HandleApprovedVisa\"}},{\"eventRef\":\"visaRejectedEvent\",\"metadata\":{\"test\":\"tested\"},\"end\":null,\"transition\":{\"nextState\":\"HandleRejectedVisa\"}}],\"dataConditions\":null,\"timeouts\":{\"stateExecTimeout\":{\"single\":\"PT20S\",\"total\":\"PT10S\"},\"eventTimeout\":\"PT10H\"}}")) + assert.True(t, strings.Contains(string(b), "{\"name\":\"CheckVisaStatusSwitchEventBased\",\"type\":\"switch\",\"defaultCondition\":{\"transition\":{\"nextState\":\"HelloStateWithDefaultConditionString\"}},\"eventConditions\":[{\"name\":\"visaApprovedEvent\",\"eventRef\":\"visaApprovedEventRef\",\"metadata\":{\"mastercard\":\"disallowed\",\"visa\":\"allowed\"},\"transition\":{\"nextState\":\"HandleApprovedVisa\"}},{\"eventRef\":\"visaRejectedEvent\",\"metadata\":{\"test\":\"tested\"},\"transition\":{\"nextState\":\"HandleRejectedVisa\"}}],\"timeouts\":{\"stateExecTimeout\":{\"single\":\"PT20S\",\"total\":\"PT10S\"},\"eventTimeout\":\"PT10H\"}}")) // Switch State with string DefaultCondition - assert.True(t, strings.Contains(string(b), "{\"name\":\"HelloStateWithDefaultConditionString\",\"type\":\"switch\",\"defaultCondition\":{\"transition\":{\"nextState\":\"SendTextForHighPriority\"}},\"eventConditions\":null,\"dataConditions\":[{\"condition\":\"${ true }\",\"end\":null,\"transition\":{\"nextState\":\"HandleApprovedVisa\"}},{\"condition\":\"${ false }\",\"end\":null,\"transition\":{\"nextState\":\"HandleRejectedVisa\"}}]}")) + assert.True(t, strings.Contains(string(b), "{\"name\":\"HelloStateWithDefaultConditionString\",\"type\":\"switch\",\"defaultCondition\":{\"transition\":{\"nextState\":\"SendTextForHighPriority\"}},\"dataConditions\":[{\"condition\":\"${ true }\",\"transition\":{\"nextState\":\"HandleApprovedVisa\"}},{\"condition\":\"${ false }\",\"transition\":{\"nextState\":\"HandleRejectedVisa\"}}]}")) // Foreach State assert.True(t, strings.Contains(string(b), "{\"name\":\"SendTextForHighPriority\",\"type\":\"foreach\",\"transition\":{\"nextState\":\"HelloInject\"},\"inputCollection\":\"${ .messages }\",\"outputCollection\":\"${ .outputMessages }\",\"iterationParam\":\"${ .this }\",\"batchSize\":45,\"actions\":[{\"name\":\"test\",\"functionRef\":{\"refName\":\"sendTextFunction\",\"arguments\":{\"message\":\"${ .singlemessage }\"},\"invoke\":\"sync\"},\"eventRef\":{\"triggerEventRef\":\"example1\",\"resultEventRef\":\"example2\",\"resultEventTimeout\":\"PT12H\",\"invoke\":\"sync\"},\"actionDataFilter\":{\"useResults\":true}}],\"mode\":\"sequential\",\"timeouts\":{\"stateExecTimeout\":{\"single\":\"PT22S\",\"total\":\"PT11S\"},\"actionExecTimeout\":\"PT11H\"}}"))