Skip to content

Commit 17c5964

Browse files
committed
Add existing generatePackageID to the single import spec parsing
1 parent 4bf2159 commit 17c5964

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

internal/codegen/golang/opts/go_type.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,18 @@ func (gt GoType) parse() (*ParsedGoType, error) {
149149
if lastDot == -1 {
150150
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)
151151
}
152-
typename = input[lastSlash+1:]
152+
o.ImportPath = input[:lastDot]
153+
pkg, pkgNeedsAlias := generatePackageID(o.ImportPath)
154+
if pkgNeedsAlias {
155+
o.Package = pkg
156+
}
153157
// a package name beginning with "go-" will give syntax errors in
154158
// generated code. We should do the right thing and get the actual
155159
// import name, but in lieu of that, stripping the leading "go-" may get
156160
// us what we want.
157-
typename = strings.TrimPrefix(typename, "go-")
158-
typename = strings.TrimSuffix(typename, "-go")
159-
o.ImportPath = input[:lastDot]
161+
pkg = strings.TrimPrefix(pkg, "go-")
162+
pkg = strings.TrimSuffix(pkg, "-go")
163+
typename = pkg + "." + input[lastDot+1:]
160164
}
161165
o.TypeName = typename
162166
isPointer := input[0] == '*'

internal/codegen/golang/opts/override_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ func TestTypeOverrides(t *testing.T) {
5151
"time.Time",
5252
false,
5353
},
54+
{
55+
Override{
56+
DBType: "text",
57+
GoType: GoType{Spec: "github.com/disgoorg/snowflake/v2.ID"},
58+
},
59+
"github.com/disgoorg/snowflake/v2",
60+
"snowflake.ID",
61+
false,
62+
},
5463
} {
5564
tt := test
5665
t.Run(tt.override.GoType.Spec, func(t *testing.T) {

0 commit comments

Comments
 (0)