Skip to content

Commit 87c315c

Browse files
authored
Generate Go bool discriminated unions (#1284)
1 parent 0c45d17 commit 87c315c

4 files changed

Lines changed: 296 additions & 66 deletions

File tree

go/rpc/generated_rpc_union_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,47 @@ func TestCommandsInvokeUnmarshalsSlashCommandInvocationResult(t *testing.T) {
185185
}
186186
}
187187

188+
func TestQueuedCommandResultBoolDiscriminatorJSONUnion(t *testing.T) {
189+
stopProcessingQueue := true
190+
var handled QueuedCommandResult = &QueuedCommandHandled{StopProcessingQueue: &stopProcessingQueue}
191+
raw, err := json.Marshal(handled)
192+
if err != nil {
193+
t.Fatalf("marshal handled result: %v", err)
194+
}
195+
if string(raw) != `{"handled":true,"stopProcessingQueue":true}` {
196+
t.Fatalf("marshal handled result = %s", raw)
197+
}
198+
199+
decodedHandled, err := unmarshalQueuedCommandResult([]byte(`{"handled":true,"stopProcessingQueue":true}`))
200+
if err != nil {
201+
t.Fatalf("unmarshal handled result: %v", err)
202+
}
203+
decodedHandledValue, ok := decodedHandled.(*QueuedCommandHandled)
204+
if !ok {
205+
t.Fatalf("unmarshal handled result = %T, want *QueuedCommandHandled", decodedHandled)
206+
}
207+
if decodedHandledValue.StopProcessingQueue == nil || !*decodedHandledValue.StopProcessingQueue {
208+
t.Fatalf("unmarshal handled stopProcessingQueue = %v, want true", decodedHandledValue.StopProcessingQueue)
209+
}
210+
211+
var notHandled QueuedCommandResult = &QueuedCommandNotHandled{}
212+
raw, err = json.Marshal(notHandled)
213+
if err != nil {
214+
t.Fatalf("marshal not handled result: %v", err)
215+
}
216+
if string(raw) != `{"handled":false}` {
217+
t.Fatalf("marshal not handled result = %s", raw)
218+
}
219+
220+
decodedNotHandled, err := unmarshalQueuedCommandResult([]byte(`{"handled":false}`))
221+
if err != nil {
222+
t.Fatalf("unmarshal not handled result: %v", err)
223+
}
224+
if _, ok := decodedNotHandled.(*QueuedCommandNotHandled); !ok {
225+
t.Fatalf("unmarshal not handled result = %T, want *QueuedCommandNotHandled", decodedNotHandled)
226+
}
227+
}
228+
188229
func TestUIElicitationFieldValueJSONUnion(t *testing.T) {
189230
raw, err := json.Marshal(UIElicitationBooleanValue(true))
190231
if err != nil {

go/rpc/zrpc.go

Lines changed: 14 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/rpc/zrpc_encoding.go

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)