Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion internal/codegen/golang/opts/go_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ func (gt GoType) parse() (*ParsedGoType, error) {
o.BasicType = true
} else {
// assume the type lives in a Go package
if lastDot == -1 {
if lastDot == -1 || lastDot < lastSlash {
// Either no dot at all, or every dot is part of the import path
// (e.g. "github.com/foo/pkg/Type"). Without a dot separating
// package from type after the last slash, we cannot tell where
// the import path ends and the type name begins, and silently
// guessing produces a broken import like "github".
return nil, fmt.Errorf("Package override `go_type` specifier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", input)
}
typename = input[lastSlash+1:]
Expand Down
13 changes: 13 additions & 0 deletions internal/codegen/golang/opts/override_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ func TestTypeOverrides(t *testing.T) {
},
"Package override `go_type` specifier \"untyped rune\" is not a Go basic type e.g. 'string'",
},
{
// Regression for sqlc-dev/sqlc#4192: when the user writes the
// type name with a slash separator instead of a dot (i.e.
// "path/to/pkg/Type" rather than "path/to/pkg.Type"), the
// previous parser silently used the dot inside "github.com" and
// emitted a broken import like `"github"`. Reject these specs
// with a clear error instead.
Override{
DBType: "text",
GoType: GoType{Spec: "github.com/example/pkg/SomeType"},
},
"Package override `go_type` specifier \"github.com/example/pkg/SomeType\" is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'",
},
} {
tt := test
t.Run(tt.override.GoType.Spec, func(t *testing.T) {
Expand Down
Loading