Skip to content

Commit c53b7fc

Browse files
committed
fixup! fix(osgen): split unions out of ReqBodySiblings to fix empty-struct emission
Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
1 parent b9e2c6b commit c53b7fc

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

cmd/osgen/emit/frag_dispatch_helpers_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,109 @@ func TestUnionFromResponses(t *testing.T) {
541541
})
542542
}
543543
}
544+
545+
func TestBulkInnerItemType(t *testing.T) {
546+
t.Parallel()
547+
548+
tests := []struct {
549+
name string
550+
resp *ir.Type
551+
regBuild func(reg *ir.TypeRegistry)
552+
nilReg bool
553+
wantName string
554+
wantOK bool
555+
}{
556+
{
557+
name: "nil registry returns false",
558+
resp: newRespType("Bulk", ir.Field{GoName: "Items", GoType: "[]BulkItem"}),
559+
nilReg: true,
560+
},
561+
{
562+
name: "missing Items field returns false",
563+
resp: newRespType("Bulk", ir.Field{GoName: "Errors", GoType: "bool"}),
564+
},
565+
{
566+
name: "outer element type missing from registry returns false",
567+
resp: newRespType("Bulk", ir.Field{GoName: "Items", GoType: "[]MissingOuter"}),
568+
},
569+
{
570+
name: "outer with no pointer fields returns false",
571+
resp: newRespType("Bulk", ir.Field{GoName: "Items", GoType: "[]NoPointers"}),
572+
regBuild: func(reg *ir.TypeRegistry) {
573+
regType(reg, &ir.Type{
574+
Name: "NoPointers",
575+
Scope: ir.ScopeLocal,
576+
Fields: []ir.Field{{GoName: "Status", GoType: "int"}},
577+
})
578+
},
579+
},
580+
{
581+
name: "first pointer field's target wins",
582+
resp: newRespType("Bulk", ir.Field{GoName: "Items", GoType: "[]BulkItem"}),
583+
regBuild: func(reg *ir.TypeRegistry) {
584+
regType(reg, &ir.Type{
585+
Name: "BulkItem",
586+
Scope: ir.ScopeLocal,
587+
Fields: []ir.Field{
588+
{GoName: "Errors", GoType: "bool", IsPointer: false},
589+
{GoName: "Index", GoType: "*BulkRespItem", IsPointer: true},
590+
{GoName: "Update", GoType: "*BulkRespItem", IsPointer: true},
591+
},
592+
})
593+
},
594+
wantName: "BulkRespItem",
595+
wantOK: true,
596+
},
597+
}
598+
599+
for _, tt := range tests {
600+
t.Run(tt.name, func(t *testing.T) {
601+
t.Parallel()
602+
var reg *ir.TypeRegistry
603+
if !tt.nilReg {
604+
reg = newRegistry()
605+
if tt.regBuild != nil {
606+
tt.regBuild(reg)
607+
}
608+
}
609+
got, ok := emit.BulkInnerItemType(tt.resp, reg)
610+
require.Equal(t, tt.wantOK, ok)
611+
require.Equal(t, tt.wantName, got)
612+
})
613+
}
614+
}
615+
616+
func TestUnionFromResponses_FallbackBranches(t *testing.T) {
617+
t.Parallel()
618+
619+
reg := newRegistry()
620+
regType(reg, &ir.Type{
621+
Name: "ItemNotUnion",
622+
Scope: ir.ScopeLocal,
623+
Fields: []ir.Field{{GoName: "Status", GoType: "int"}},
624+
})
625+
626+
tests := []struct {
627+
name string
628+
resp *ir.Type
629+
}{
630+
{
631+
name: "Responses element type missing from registry",
632+
resp: newRespType("X", ir.Field{GoName: "Responses", GoType: "[]MissingType"}),
633+
},
634+
{
635+
name: "Responses element type is registered but not a union",
636+
resp: newRespType("X", ir.Field{GoName: "Responses", GoType: "[]ItemNotUnion"}),
637+
},
638+
}
639+
640+
for _, tt := range tests {
641+
t.Run(tt.name, func(t *testing.T) {
642+
t.Parallel()
643+
unionName, success, errBranch := emit.UnionFromResponses(tt.resp, reg)
644+
require.Empty(t, unionName)
645+
require.Empty(t, success)
646+
require.Empty(t, errBranch)
647+
})
648+
}
649+
}

0 commit comments

Comments
 (0)