From 889f036b6668ff9a4724387e652c678f6363d234 Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" Date: Thu, 16 Jan 2025 22:50:02 +0700 Subject: [PATCH] sql/specutil: use interface instead of generic for `ColumnByRef` --- sql/internal/specutil/convert.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/sql/internal/specutil/convert.go b/sql/internal/specutil/convert.go index 1348f58a8dc..def7fc110f4 100644 --- a/sql/internal/specutil/convert.go +++ b/sql/internal/specutil/convert.go @@ -1089,7 +1089,9 @@ func SchemaName(ref *schemahcl.Ref) (string, error) { } // ColumnByRef returns a column from the table by its reference. -func ColumnByRef[T *schema.View | *schema.Table](tv T, ref *schemahcl.Ref) (*schema.Column, error) { +func ColumnByRef(tv interface { + Column(string) (*schema.Column, bool) +}, ref *schemahcl.Ref) (*schema.Column, error) { vs, err := ref.ByType(typeColumn) if err != nil { return nil, err @@ -1097,21 +1099,16 @@ func ColumnByRef[T *schema.View | *schema.Table](tv T, ref *schemahcl.Ref) (*sch if len(vs) != 1 { return nil, fmt.Errorf("expected 1 column ref, got %d", len(vs)) } - switch tv := any(tv).(type) { - case *schema.Table: - c, ok := tv.Column(vs[0]) - if !ok { - return nil, fmt.Errorf("column %q was not found in table %s", vs[0], tv.Name) - } + if c, ok := tv.Column(vs[0]); ok { return c, nil + } + switch tv := tv.(type) { + case *schema.Table: + return nil, fmt.Errorf("column %q was not found in table %s", vs[0], tv.Name) case *schema.View: - c, ok := tv.Column(vs[0]) - if !ok { - return nil, fmt.Errorf("column %q was not found in view %s", vs[0], tv.Name) - } - return c, nil + return nil, fmt.Errorf("column %q was not found in view %s", vs[0], tv.Name) default: - return nil, fmt.Errorf("unreachable %T", tv) + return nil, fmt.Errorf("column %q was not found in %T", vs[0], tv) } }