44 "context"
55 "encoding/json"
66 "fmt"
7+ "reflect"
78 "testing"
89 "time"
910
@@ -26,7 +27,17 @@ func testAllSerializationPaths[T any](
2627) {
2728 t .Helper ()
2829
29- isNilExpected := isNilOrZeroValue (input )
30+ // Check if input is nil (for pointer types, slice, map, etc.)
31+ val := reflect .ValueOf (input )
32+ isNilExpected := false
33+ if ! val .IsValid () {
34+ isNilExpected = true
35+ } else {
36+ switch val .Kind () {
37+ case reflect .Pointer , reflect .Slice , reflect .Map , reflect .Chan , reflect .Func :
38+ isNilExpected = val .IsNil ()
39+ }
40+ }
3041
3142 // Setup events for recovery
3243 startEvent := NewEvent ()
@@ -98,17 +109,23 @@ func testAllSerializationPaths[T any](
98109 lastStep := steps [len (steps )- 1 ]
99110 if isNilExpected {
100111 // Should be an empty string
101- assert .Equal (t , "" , lastStep .Output , "Step output should be an empty string " )
112+ assert .Nil (t , lastStep .Output , "Step output should be nil " )
102113 } else {
103114 require .NotNil (t , lastStep .Output )
104115 // GetWorkflowSteps returns a string (base64-decoded JSON)
105116 // Unmarshal the JSON string into type T
106117 strValue , ok := lastStep .Output .(string )
107118 require .True (t , ok , "Step output should be a string" )
108- var decodedOutput T
109- err := json .Unmarshal ([]byte (strValue ), & decodedOutput )
110- require .NoError (t , err , "Failed to unmarshal step output to type T" )
111- assert .Equal (t , expectedOutput , decodedOutput , "Step output should match expected output" )
119+ // We encode zero values as empty strings. End users are expected to handle this.
120+ if strValue == "" {
121+ var zero T
122+ assert .Equal (t , zero , expectedOutput , "Step output should be the zero value of type T" )
123+ } else {
124+ var decodedOutput T
125+ err := json .Unmarshal ([]byte (strValue ), & decodedOutput )
126+ require .NoError (t , err , "Failed to unmarshal step output to type T" )
127+ assert .Equal (t , expectedOutput , decodedOutput , "Step output should match expected output" )
128+ }
112129 }
113130 assert .Nil (t , lastStep .Error )
114131 }
@@ -124,8 +141,8 @@ func testAllSerializationPaths[T any](
124141 wf := wfs [0 ]
125142 if isNilExpected {
126143 // Should be an empty string
127- assert .Equal (t , "" , wf .Input , "Workflow input should be an empty string " )
128- assert .Equal (t , "" , wf .Output , "Workflow output should be an empty string " )
144+ assert .Nil (t , wf .Input , "Workflow input should be nil " )
145+ assert .Nil (t , wf .Output , "Workflow output should be nil " )
129146 } else {
130147 require .NotNil (t , wf .Input )
131148 require .NotNil (t , wf .Output )
@@ -137,16 +154,25 @@ func testAllSerializationPaths[T any](
137154 outputStr , ok := wf .Output .(string )
138155 require .True (t , ok , "Workflow output should be a string" )
139156
140- var decodedInput T
141- err := json .Unmarshal ([]byte (inputStr ), & decodedInput )
142- require .NoError (t , err , "Failed to unmarshal workflow input to type T" )
143-
144- var decodedOutput T
145- err = json .Unmarshal ([]byte (outputStr ), & decodedOutput )
146- require .NoError (t , err , "Failed to unmarshal workflow output to type T" )
157+ if inputStr == "" {
158+ var zero T
159+ assert .Equal (t , zero , input , "Workflow input should be the zero value of type T" )
160+ } else {
161+ var decodedInput T
162+ err := json .Unmarshal ([]byte (inputStr ), & decodedInput )
163+ require .NoError (t , err , "Failed to unmarshal workflow input to type T" )
164+ assert .Equal (t , input , decodedInput , "Workflow input should match input" )
165+ }
147166
148- assert .Equal (t , input , decodedInput , "Workflow input should match input" )
149- assert .Equal (t , expectedOutput , decodedOutput , "Workflow output should match expected output" )
167+ if outputStr == "" {
168+ var zero T
169+ assert .Equal (t , zero , expectedOutput , "Workflow output should be the zero value of type T" )
170+ } else {
171+ var decodedOutput T
172+ err = json .Unmarshal ([]byte (outputStr ), & decodedOutput )
173+ require .NoError (t , err , "Failed to unmarshal workflow output to type T" )
174+ assert .Equal (t , expectedOutput , decodedOutput , "Workflow output should match expected output" )
175+ }
150176 }
151177 })
152178}
@@ -781,13 +807,6 @@ func TestSerializer(t *testing.T) {
781807 testSendRecv (t , executor , serializerIntPtrSenderWorkflow , serializerIntPtrReceiverWorkflow , input , "typed-intptr-set-sender-wf" )
782808 testSetGetEvent (t , executor , serializerIntPtrSetEventWorkflow , serializerIntPtrGetEventWorkflow , input , "typed-intptr-set-setevent-wf" , "typed-intptr-set-getevent-wf" )
783809 })
784-
785- // Test *int (pointer type, nil)
786- t .Run ("IntPtrNil" , func (t * testing.T ) {
787- var input * int = nil
788- testSendRecv (t , executor , serializerIntPtrSenderWorkflow , serializerIntPtrReceiverWorkflow , input , "typed-intptr-nil-sender-wf" )
789- testSetGetEvent (t , executor , serializerIntPtrSetEventWorkflow , serializerIntPtrGetEventWorkflow , input , "typed-intptr-nil-setevent-wf" , "typed-intptr-nil-getevent-wf" )
790- })
791810 })
792811
793812 // Test queued workflow with TestWorkflowData type
0 commit comments