Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql/specutil: use interface instead of generic for ColumnByRef #3320

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions sql/internal/specutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,29 +1089,26 @@ 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
}
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)
}
}

Expand Down
Loading