Skip to content

Commit 6e8c527

Browse files
kevinburke1kyleconroy
authored andcommitted
internal/dinosql: strip leading "go-" or trailing "-go" from import (#262)
If I try to import from a package named "go-x" or "x-go" it will generate a syntax error. We should do the right thing and determine what the package name is and set it to that, but in lieu of that, a good guess for the import name is just stripping that prefix or suffix. Updates #261.
1 parent d372abd commit 6e8c527

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ Each override document has the following keys:
342342
- `go_type`:
343343
- A fully qualified name to a Go type to use in the generated code.
344344
- `null`:
345-
- If true, use this type when a column in nullable. Defaults to `false`.
345+
- If true, use this type when a column is nullable. Defaults to `false`.
346346

347347
### Per-Column Type Overrides
348348

internal/dinosql/config.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,24 @@ func (o *Override) Parse() error {
102102
// validate GoType
103103
lastDot := strings.LastIndex(o.GoType, ".")
104104
if lastDot == -1 {
105-
return fmt.Errorf("Package override `go_type` specificier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", o.GoType)
105+
return fmt.Errorf("Package override `go_type` specifier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", o.GoType)
106106
}
107107
lastSlash := strings.LastIndex(o.GoType, "/")
108108
if lastSlash == -1 {
109-
return fmt.Errorf("Package override `go_type` specificier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", o.GoType)
109+
return fmt.Errorf("Package override `go_type` specifier %q is not the proper format, expected 'package.type', e.g. 'github.com/segmentio/ksuid.KSUID'", o.GoType)
110110
}
111-
o.goTypeName = o.GoType[lastSlash+1:]
111+
typename := o.GoType[lastSlash+1:]
112+
if strings.HasPrefix(typename, "go-") {
113+
// a package name beginning with "go-" will give syntax errors in
114+
// generated code. We should do the right thing and get the actual
115+
// import name, but in lieu of that, stripping the leading "go-" may get
116+
// us what we want.
117+
typename = typename[len("go-"):]
118+
}
119+
if strings.HasSuffix(typename, "-go") {
120+
typename = typename[:len(typename)-len("-go")]
121+
}
122+
o.goTypeName = typename
112123
o.goPackage = o.GoType[:lastDot]
113124
isPointer := o.GoType[0] == '*'
114125
if isPointer {

0 commit comments

Comments
 (0)