Skip to content

Commit 15f7d1b

Browse files
authored
fix: Small improvement to PK checking in codegen (#432)
1 parent de0735a commit 15f7d1b

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

codegen/table.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/cloudquery/plugin-sdk/schema"
88
"github.com/rs/zerolog"
9+
"github.com/thoas/go-funk"
910
"golang.org/x/exp/slices"
1011
)
1112

@@ -105,7 +106,7 @@ func NewTableFromStruct(name string, obj interface{}, opts ...TableOption) (*Tab
105106
columns = append(columns, column)
106107
}
107108
if len(t.extraPKColumns) > 0 {
108-
return nil, fmt.Errorf("%s table definition has %d extra PK keys", t.Name, len(t.extraPKColumns))
109+
return nil, fmt.Errorf("%s table definition has %d extra PK keys: %v", t.Name, len(t.extraPKColumns), funk.Keys(t.extraPKColumns))
109110
}
110111
t.Columns = columns
111112

codegen/table_test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,10 @@ func TestTableFromGoStruct(t *testing.T) {
245245
}
246246

247247
tests := []struct {
248-
name string
249-
args args
250-
want TableDefinition
248+
name string
249+
args args
250+
want TableDefinition
251+
wantErr bool
251252
}{
252253
{
253254
name: "should generate table from struct with default options",
@@ -355,13 +356,24 @@ func TestTableFromGoStruct(t *testing.T) {
355356
},
356357
want: expectedTestSliceStruct,
357358
},
359+
{
360+
name: "should error if there are undefined pk columns",
361+
args: args{
362+
testStruct: testSliceStruct{},
363+
options: []TableOption{WithPKColumns("int_col", "some_col")},
364+
},
365+
wantErr: true,
366+
},
358367
}
359368

360369
for _, tt := range tests {
361370
t.Run(tt.name, func(t *testing.T) {
362371
table, err := NewTableFromStruct("test_struct", tt.args.testStruct, tt.args.options...)
363-
if err != nil {
364-
t.Fatal(err)
372+
if (err != nil) != tt.wantErr {
373+
t.Fatalf("error = %v, wantErr %v", err, tt.wantErr)
374+
}
375+
if tt.wantErr {
376+
return
365377
}
366378
if diff := cmp.Diff(&tt.want, table,
367379
cmpopts.IgnoreUnexported(TableDefinition{})); diff != "" {

0 commit comments

Comments
 (0)