Skip to content

Commit

Permalink
Empty fields Marshaled as "field":null (#172)
Browse files Browse the repository at this point in the history
fixes #171

Signed-off-by: Spolti <filippespolti@gmail.com>
  • Loading branch information
spolti committed May 4, 2023
1 parent 2ee5933 commit 6278ce5
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 45 deletions.
4 changes: 2 additions & 2 deletions model/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 18 additions & 12 deletions model/switch_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package model

import (
"encoding/json"
"strings"
)

// SwitchState is workflow's gateways: direct transitions onf a workflow based on certain conditions.
Expand All @@ -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).
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion model/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
"sigs.k8s.io/yaml"
)

// Kind ...
// +k8s:deepcopy-gen=false

type Kind interface {
KindValues() []string
String() string
Expand All @@ -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
Expand Down
194 changes: 166 additions & 28 deletions model/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}}"))
Expand Down

0 comments on commit 6278ce5

Please sign in to comment.